8

In this example code i am using a simple StopWatch to test time that it takes to complete a given task/action

StopWatch SW1 = new StopWatch();
SW1.Start();
SomeAction();
SW1.Stop();
sting results = SW1.Elapsed.ToString();
MessageBox.Show(resutls);

i would like to have a class that i will instantiate to use with tests

public Class PerformanceTests
{
   public StopWatch SW1 = new StopWatch();
   public StopWatch SW2 = new StopWatch();
   public string results1 = "", results2 = "";
   ....
   ....
   //some other variables to use 
}

though when instantiating the class and trying to use SW1 is not letting me use its methods. What am i doing wrong ?

PerformanceTests Ptst = new PerformanceTests();
Ptst.SW1. ... Start() is not accessible

Update

For rest of answers, don't copy the code from me, as I miss capitalized stopwatch. Instead of instantiating the Stopwatch class i accidentally didn't pay attention Visual Studio asking if i want to create a class for my so called stopwatch instead of .NET's real Stopwatch.

So my advice, pay attention to the suggested actions of Visual Studio intellisense even though it should be same all the time . Just make sure till you're really experienced and know all the classes by heart.

Cœur
  • 37,241
  • 25
  • 195
  • 267
LoneXcoder
  • 2,121
  • 6
  • 38
  • 76
  • `intelliSense` sounds like an Apple product. Edited that, and many – nawfal Apr 22 '13 at 12:21
  • I wrote an answer here: http://stackoverflow.com/questions/969290/exact-time-measurement-for-performance-testing/16157458#16157458 Also see http://stackoverflow.com/questions/232848/wrapping-stopwatch-timing-with-a-delegate-or-lambda – nawfal Apr 22 '13 at 21:59

3 Answers3

26

Here is simple class, which can help you for measuring time of code block execution:

public class PerformanceTester : IDisposable
{
    private Stopwatch _stopwatch = new Stopwatch();
    private Action<TimeSpan> _callback;

    public PerformanceTester()
    {
        _stopwatch.Start();
    }

    public PerformanceTester(Action<TimeSpan> callback) : this()
    {
        _callback = callback;            
    }

    public static PerformanceTester Start(Action<TimeSpan> callback)
    {
        return new PerformanceTester(callback);
    }

    public void Dispose()
    {
        _stopwatch.Stop();
        if (_callback != null)
            _callback(Result);
    }

    public TimeSpan Result
    {
        get { return _stopwatch.Elapsed; }
    }
}

Usage (just wrap code block with using of PerformanceTester):

using (var tester = new PerformanceTester())
{
    // code to test
    MessageBox.Show(tester.Results.ToString());
}

If you declare tester variable before using block, then stopwatch will stop automatically when you exit using block, and results will be available for you:

PerformanceTester tester;

using (tester = new PerformanceTester())    
    SomeAction();

MessageBox.Show(tester.Results.ToString());

If you pass callback action to PerformanceTester, then this action will be called at the end of using statement, and elapsed time will be passed to callback:

using (PerformanceTester.Start(ts => MessageBox.Show(ts.ToString())))
     SomeAction();

You can declare method, which will accept TimeSpan and process results:

private void ProcessResult(TimeSpan span)
{
   // log, show, etc
   MessageBox.Show(span.ToString());
}

Usage becomes very clean:

using (PerformanceTester.Start(ProcessResult))
     SomeAction();
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • it was strangley made while i was taking the intelliSence Suggestion instead of using statment i was creating a class , becuse i missSpelled (capitalisation) *i think it should BE! `StopWatch()` !! (: thanks. cause without this mistake , i wouldn't have chance to see your neat ext' method ! +1 for cool class – LoneXcoder Dec 03 '12 at 11:11
  • by the way it is also letting you avoid the use of un necessary imports of other classes of dot.net in the application webform/winform . does it help in any way i mean is the fact that i try to have as less as i can `using` statements, means less class imports , does it have any effect , or while i instanciate the class i am then using the .net class (System.diagnostics) so it does not matter atleast in the aspect of resulting size of aplication . – LoneXcoder Dec 03 '12 at 11:18
  • how to declare tester out of the using (before) can u give another example as in your comment below your answer "if you declare tester variable before using `block`..." can you broad a little – LoneXcoder Dec 03 '12 at 11:48
  • @LoneXcoder Sorry, was AFK, I've added sample with declaring tester before using block – Sergey Berezovskiy Dec 03 '12 at 12:00
  • ..tested your class, very nice one , thanks , i used a declared string before the usage and assigned it the `Results.ToString();` then after using block, i've accessed the string i have declared before the using statement – LoneXcoder Dec 03 '12 at 12:00
  • @LoneXcoder I've added another, more handy version of performance tester. Take a look on it :) – Sergey Berezovskiy Dec 03 '12 at 12:52
  • this looks more advanced version of your initial one though this time it uses a task start. gonna run some tests on those, Cheers, thanks for your time , i am doing lot's of investigations latlely, about new technologies (for me) you can have a look on my latests post regarding partial data (segmets) downloading to increase downlod speed (it's rewarding 100 points(: for right answer) so feel free to take part! – LoneXcoder Dec 04 '12 at 00:56
1

make them public instead of private:

 public class PerformanceTests
    {
        public StopWatch SW1 { get; set; }

        public StopWatch SW2 { get; set; }

        public string Results1 { get; set; }

        public string Results2 { get; set; }

        public PerformanceTests()
        {
            this.SW1 = new StopWatch();
            this.SW2 = new StopWatch();
        }
    }
gkmiec
  • 41
  • 3
0

Unless you are using a custom class StopWatch is not the correct class name try Stopwatch under the namespace System.Diagnostics

Try:

public Class PerformanceTests
{
   public Stopwatch SW1 = new Stopwatch();
   public Stopwatch SW2 = new Stopwatch();
   public string results1 = "", results2 = "";
   ....
   ....
   //some other variables to use 
}
nawfal
  • 70,104
  • 56
  • 326
  • 368
heads5150
  • 7,263
  • 3
  • 26
  • 34