Measuring Salesforce Performance

It can be challenging to estimate performance in Salesforce, in part, because there's no good way to know what the load is on your server, and with a platform as a service, there are many elements that are out of your control.

Rather than focus on absolute metrics, it's more practical to look at relative metrics. I.e. of two or more approaches, which is fastest.

It's also very important to consider performance sooner rather than later. When you start hitting Salesforce governor limits, you might needs to make a very large number of changes simply because you did follow good practices early on.

Here's a basic test structure I often use to measure performance.

// track our start time
Long startTime = Datetime.now().getTime();

Integer i = 0;
for (i = 0; i < 15; i++) {
    // do stuff to test
}
// track our end time
Long endTime = Datetime.now().getTime();

// write debug statements as metrics
System.debug('Total Milliseconds: ' + (endTime - startTime));
System.debug('CPU Time: ' + Limits.getCpuTime());
System.debug('DML Rows: ' + Limits.getDmlRows());
System.debug('DML Statements: ' + Limits.getDmlStatements());
System.debug('SOQL Queries: ' + Limits.getQueries());
System.debug('HeapSize: '  + Limits.getHeapSize());
System.debug('DatabaseTime: '  + Limits.getDatabaseTime());

Put the code to be measured on the line with the "do stuff" comment

The test is done inside of a loop, to hopefully have a large enough time span to be reasonably measurable.

One should run the test multiple times to get an average, dropping any numbers that are far from the median as they may be the result of how busy the server is.

 

 

© 2001 – 2023 Object Factory Inc
marker marker marker marker marker