Click here to Skip to main content
Licence CPOL
First Posted 14 Dec 2008
Views 8,816
Downloads 20
Bookmarked 17 times

Dynamic method selection based on performance

By | 14 Dec 2008 | Article
A model to automatically select the best performing method from a set of methods with the same functionallity.

AdaptiveMethodSelector

Introduction

Often times, we find more than a single way of doing things. Even when we create the most sophisticated methods to obtain information from a source, we still rely on external dependencies, such as database or network latency; or physical factors such as hard drive or computer failures that are non-deterministic and make our lives more difficult when it comes to fine tuning these systems. In such situations, a good approach is to have a second or third alternative where if data can’t be obtained the usual way, we could try it a different way, and here is where we fall in this try/catch mode that never seems to have an end.

Approach

The approach presented here helps your system in proactively identifying a method, from a set of methods, that has the best response time and no exceptions at any given moment. The way this works is as follows: based on a set of methods with the same signature, the same functionality, but different implementations, the algorithm will test each method first and measure response times; then, it’ll choose the fastest method that does not throw exceptions; then, if the method response time is above an threshold or an exception is thrown, it’ll revaluate all the methods again and chooses the fastest one. At the same time, it will continually adjust to adapt in case the performance is degraded.

Sample

Let’s suppose we have two different implementations to concatenate two strings. The first method uses String.Concat, the second uses StringBuilder. As expected, the latter should perform better as you can see when running the example.

The following is a way you can subscribe all the methods in the adaptive method selector:

private AdaptiveMethodSelector<string, ParamsMethods> _methodOptimizer;
private AdaptiveMethodSelector<string, ParamsMethods> MethodOptimizer
{
    get
    {
       if (_methodOptimizer != null)
         return _methodOptimizer;

       Collection<AdaptiveMethodSelector<string, ParamsMethods>.EvaluationMethodDelegate>
            functionsToEvaluate = new Collection<AdaptiveMethodSelector<string, 
                                  ParamsMethods>.EvaluationMethodDelegate>();
            functionsToEvaluate.Add(new AdaptiveMethodSelector<string, 
                                    ParamsMethods>.EvaluationMethodDelegate(Method1));
            functionsToEvaluate.Add(new AdaptiveMethodSelector<string, 
                                    ParamsMethods>.EvaluationMethodDelegate(Method2));

       _methodOptimizer = new AdaptiveMethodSelector<string, 
                                ParamsMethods>(functionsToEvaluate);
       return _methodOptimizer;
    }
}

The AdaptiveMethodSelector takes two types, the first one is the return type of the methods, the second is a type for the parameters being passed. When you need to pass multiple parameters to the methods, you can define a structure as in this example; otherwise, you can indicate the type such as string, int, etc.

The methods signature in the example is as follows:

public string Method1(ParamsMethods parameters)
public string Method2(ParamsMethods parameters)

where ParamsMethods is:

public struct ParamsMethods
{
    public ParamsMethods(string param1, string param2)
    {
       Param1 = param1;
       Param2 = param2;
    }

    public string Param1;
    public string Param2;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

luis_botero

Software Developer (Senior)

United States United States

Member

Born and built for Software Development. Eager to develop systems that drive business and human intelligence to the next level. Love Artificial Intelligence and have abundant experience in developing systems with a large user base. I know more than just how to write code that compiles. I can produce software that is fast, reliable, well-tested, secure, maintainable, globalizable, and on down the list of attributes of high-quality code.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralOnly Useful During Devlopment PinmvpJohn Simmons / outlaw programmer2:28 15 Dec '08  
GeneralRe: Only Useful During Devlopment PinmemberGuido_d4:04 15 Dec '08  
GeneralNice Article Luis PinmemberRazanPaul19:09 14 Dec '08  
GeneralPerformance Pinmemberntr9h14:46 14 Dec '08  
GeneralRe: Performance PinmemberSeishin#3:27 15 Dec '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 14 Dec 2008
Article Copyright 2008 by luis_botero
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid