Microsoft Dynamics AX : Stopwatch tip

You are here: >

Microsoft Dynamics AX : Stopwatch tip

Welcome to our eBECS technical blog. A blog to spread the knowledge based on real life projects and hands-on experience from our technical gurus.

In this post I will talk about a simple yet powerful tool used in performance measurements.

The stopwatch: is a utility class built on .NET core types to measure the duration between start and stop methods

It can be used in different scenarios such as:

1- Troubleshooting and performance measurements

2- User output

3- Logging

Here is an example of the first scenario

Performance measurement:

Let’s say you have a performance issue and want to measure before and after effect, by comparing the time consumed.

In this example a job was created to update customer accounts using a regular update (While select update)

static void EBCStopwatch_Example1_Before(Args _args)

{

    CustTable custTable;

    Counter counter = 0;

    EBCStopwatch stopwatch = new EBCStopwatch();

   

    stopwatch.start();   

   

    ttsBegin;

    while select forupdate AccountNum from custTable

    {

        custTable.AccountNum = “OLD_” + custTable.AccountNum;

        custTable.update();

        counter++;

    }

    ttsCommit;

   

    stopwatch.stop(strFmt(“%1 customers updated”, counter), EBCTimePart::Millisecond);

}

Microsoft Dynamics AX: Stopwatch

A better approach was found to use UPDATE_RECORDSET which hits the database once.

static void EBCStopwatch_Example1_After(Args _args)

{

    CustTable custTable;

    EBCStopwatch stopwatch = new EBCStopwatch();

   

    stopwatch.start();   

   

    custTable.skipDataMethods(true);   

    update_recordSet custTable

        setting AccountNum = “OLD_” + custTable.AccountNum;

   

    stopwatch.stop(strFmt(“%1 customers updated”, custTable.RowCount()), EBCTimePart::Millisecond);

}

A comparison is shown below, which shows that the old code is 6 times slower than the new code:

 

Before

After

Duration

2679 ms (3 seconds)

517 ms (0.5 second)

You can use the stop() method to show the time in Hours, Minutes, Seconds or Milliseconds. Also it returns the duration to be stored in a variable for later use.

The XPO is included in this post for the class and examples.

Merry Christmas and happy coding till the next post…

Looking for a Microsoft Dynamics Partner then contact ebecs.

Author: 
eBECS

Get in Touch

Email or call us now to
discuss how Microsoft
Business Solutions can
improve your business

Upcoming Events - Register Now

Join our list

eBECS will invite you to webinars, events and keep you up to date with relevant news. You can unsubscribe at any time.

UK: +44 (0) 8455 441 441
Ireland: +353 (0)1 893 4831
USA: +1 (678) 701 5856
Saudi Arabia: +966 (11)920 007299

© 2022 eBECS Limited. All rights reserved.
Registered office: Royal Pavilion, Wellesley Road, Aldershot, Hampshire, England, GU11 1PZ