How to measure the performance of actions?

Best practices, code snippets for common functionality, examples, and guidelines.
esac
Posts: 3
Joined: Sat Sep 25, 2010 12:59 am

How to measure the performance of actions?

Post by esac » Sat Sep 25, 2010 2:24 am

I am looking for a way to collect performance statistics on how long each action took. Ideally I would like to remove from that time any time spent in Ranorex itself (searching for forms, windows, images, etc..). Any idea if this can be done? How could I also do this in user code?

Thanks

User avatar
sdaly
Posts: 238
Joined: Mon May 10, 2010 11:04 am
Location: Dundee, Scotland

Re: How to measure the performance of actions?

Post by sdaly » Sun Sep 26, 2010 4:56 pm

You could try an approach or similar like this.......

Add the following class....
Public Class perfLogger
	
	Dim startT As System.DateTime 
	Dim endT As System.DateTime 
	Dim span as System.TimeSpan
	
	Public Sub New
	End Sub
	
	Public Sub startLog
		me.startT = now
	End Sub
	
	Public Sub pauseLog
		me.endT = now
		me.span += me.endT - me.startT
	End Sub
	
	Public Function StopLog As system.TimeSpan
		me.pauseLog
		Return me.span
	End Function
	
End Class
Then use as below to start and stop the logger -
Public Sub timeTest
		'create a new logger
		Dim logger as New perfLogger
		'start the logger
		logger.startLog
		
		'code you are measuring here
		
		'pause the logger for Ranorex search time
		logger.pauseLog
		Dim listitem As Ranorex.ListItem = "/desktop[@processname='explorer']/container/list/listitem[@text='Windows Media Player']"
		'start the logger again
		logger.startLog
		listitem.MoveTo
		'report the time
		report.Info("Time",logger.StopLog.totalseconds.tostring())
		
	End Sub

Hope this helps!
Scott

atom
Posts: 357
Joined: Sun Dec 07, 2008 11:14 pm
Location: Dublin, Ireland

Re: How to measure the performance of actions?

Post by atom » Mon Oct 11, 2010 1:24 pm

A more complicated example is to use a Command design pattern, and in the Invoker/Sender class time the command length

EugeneF
Posts: 63
Joined: Fri Jul 29, 2011 12:26 am

Re: How to measure the performance of actions?

Post by EugeneF » Sat Jan 21, 2012 1:50 am

You may also use "Stopwatch" function in C# :

System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

sw.Reset(); // Reset stopwatch
sw.Start(); // Stopwatch start

you may specify here some condition(s)

for instance -
while (sCell_2_RT == "»")
{
sCell_2_RT = repo.FormFSC_Rater___California_A.RowN2.Cell_2_RT.Text;
}

sw.Stop(); // Stopwatch stop

Thanks.

Eugene

argray
Posts: 4
Joined: Thu Jan 05, 2012 11:30 pm

Re: How to measure the performance of actions?

Post by argray » Mon Jul 09, 2012 5:22 pm

I prefer to be able to inject actions for performance testing, like so:
public static void ITestModule.Run()
{
    MeasureTest(() => {
        Mouse.MoveTo(repo.FormA.ButtonClose);
        Mouse.Click(repo.FormA.ButtonClose);
    });
}

public static TimeSpan MeasureTest(Action<> action)
{
    Stopwatch sw = Stopwatch.StartNew();

    action();
    sw.Stop();

    return sw.Elapsed;
}
Hope this helps :) Lambda expressions/first-class functions and actions are a powerful benefit!

EDIT: When using Stopwatch, you want to return/use the Elapsed, since it's a TimeSpan. TimeSpans contained Ticks, Milliseconds, and various other fun properties!

ep3681
Posts: 1
Joined: Tue Oct 02, 2012 2:56 pm

Re: How to measure the performance of actions?

Post by ep3681 » Tue Oct 02, 2012 2:59 pm

Hallo,
is there also an example in C# of this VB code?
Re: How to measure the performance of actions?

Postby sdaly » Sun Sep 26, 2010 5:56 pm
You could try an approach or similar like this.......

Add the following class....

view plaincopy to clipboardprint

Public Class perfLogger

Dim startT As System.DateTime
....

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: How to measure the performance of actions?

Post by Support Team » Thu Oct 04, 2012 3:31 pm

Hello,

The following code written in C# is quite similar to the code from Scott.
public class perfLogger
{
	System.DateTime startT;
	System.DateTime endT;
	System.TimeSpan span;
	
	public perfLogger()
	{
	
	}
	
	public void startLog()
	{
		this.startT = System.DateTime.Now;
	}

	public void pauseLog()
	{
		this.endT = System.DateTime.Now;
		this.span += this.endT - this.startT;
	}

	public System.TimeSpan StopLog()
	{
		this.pauseLog();
		return this.span;
	}
}
public void timeTest()
{
	//create a new logger
	perfLogger logger = new perfLogger();
	//start the logger
	logger.startLog();

	//code you are measuring here

	//pause the logger for Ranorex search time
	logger.pauseLog();
	Ranorex.ListItem listitem = "/desktop[@processname='explorer']/container/list/listitem[@text='Windows Media Player']";
	//start the logger again
	logger.startLog();
	listitem.MoveTo();
	//report the time
	Report.Info("Time", logger.StopLog().TotalSeconds.ToString());

}
Regards,
Bernhard
Ranorex Support Team
.
Image