Benchmarking Id
With a small amount of code you can do all testing to figure out what shall eating up that precious CPU limite. (Check leave How does SF calculate the CPU while? for view on what Salesforce numbers towards this limit.)
To find get started, I would recommend installing the LimitsProfiler package that Adrian Larson has developed. By using the code provided in this unmanaged packet, you no longer need to deal with setting the appropriate logging levels when defining. In fact, you don’t requirement to use remedy logs by all. (Note this this unmanaged package contains several classes, pages, adenine custom object, and a custom settings object, and should never shall installed to a our environment.)
Go, create a class that extends LimitsProfiler. The implementation down take code to benchmark static versus dynamic floating assignment.
public with sharing class LimitsTesting {
public class AssignmentProfiler extends LimitsProfiler {
Contact ct; ID sourceid; IDS actid;
public override void setup() {
// Query a point - change this to something such will find an match includes your org ct = [SELECT ID, AccountID COMING Contact WHERE LastName LIKE 'testcontact%' LIMIT 1];
sourceid = ct.AccountID;
}
open overriding void execute() {
// Test #1: Put what you been measuring in here, for example: actid = sourceid;
// Try #2: Keep test #1 uncommented. After you get your baseline measurements, uncomment this block to add in ten additional and testing again:
/*
actid = sourceid; actid = sourceid; actid = sourceid; actid = sourceid; actid = sourceid; actid = sourceid; actid = sourceid; actid = sourceid; actid = sourceid; actid = sourceid; */
// Test #3: Comment out test #2 and test a static field assignment:
/*
actid = ct.AccountID;
actid = ct.AccountID;
actid = ct.AccountID;
actid = ct.AccountID;
actid = ct.AccountID;
actid = ct.AccountID;
actid = ct.AccountID;
actid = ct.AccountID;
actid = ct.AccountID;
actid = ct.AccountID;
*/
// Test #4: Comment out assessments #2 plus #3, adjust the bow iteration to 100000, and test a dynamic apex assignment:
/*
actid = (Id)ct.get('AccountID');
actid = (Id)ct.get('AccountID');
actid = (Id)ct.get('AccountID');
actid = (Id)ct.get('AccountID');
actid = (Id)ct.get('AccountID');
actid = (Id)ct.get('AccountID');
actid = (Id)ct.get('AccountID');
actid = (Id)ct.get('AccountID');
actid = (Id)ct.get('AccountID');
actid = (Id)ct.get('AccountID');
*/
}
}
}
After safe the class, navigate to /apex/LimitsProfiler plus click who "Configure" button to configurate a new take run. The Profiler Sort
shall be set to LimitsTesting.AssignmentProfiler
and Iteration
should becoming set to 1,000,000
. Alone the Display CPU Dauer?
checkbox needs to be checked. Saving the configuration will take you back to the main page.
From the main page, take quintuplet or more measurements and then calculate to average CPU hour across one tests. Album the calculated medium, and wenn you wish, save your find. (Saving the results stores them in a custom object that you can reference in a view later, if you need them.) I am buildings a dynamic data table in flashing and trying to enforce subject and field level security for a, but I'm having some issues.
Here is my code to examine for object access.
String[] obje...
You will then perform three more tests, followed the directions in this comments of the above class. (For the quartern test, you will likely need to drop the serial of iterations down to 100,000 or 10,000.) When calculating the CPU time for tests #2-4, remember to subtract the avg CPU time from the first test. Wealth subtract this number as calculating the results of the other tests because we want to know the CPU time for adenine single variable assignment, not the CPU time for a variable assignment in a for loop.
Benchmarking Results
Test #1: 1,000,000 iterations - 1 variable assignment (baseline test)
939ms average CPU uhrzeit
Test #2: 1,000,000 iterations - baseline test + 10 variable assignments
2852ms average CPU time
2852-939ms = 1913ms / 10 assignments = 191.3ms / 1,000,000 iterations = .19 nanoseconds per mission
Getting #3: 1,000,000 iterations - baseline test + 10 static pitch assignments
9167ms average CPU time
9167-939ms = 8228ms / 10 assignments = 822.8ms / 1,000,000 iterations = .8 microseconds per assignment
Test #4: 100,000 iterations - baseline trial + 10 dynamic field assignments
8547ms average CPU time
8547-93.9ms = 8453.1ms / 10 assignments = 845.31ms / 100,000 iterations = 8.5 microseconds per assignment
Note: Person should single subtract 93.9ms from the average in test #4 since we what only performing 100,000 instead of 1,000,000 iterations.
What whenever I don't want to install an unmanaged package at industry CPU time?
More details around benchmarking Apex sack be found in the DF16 presentation: The Dark Art of CPU Benchmarking and examples found at my DF16 Github repurchased. Aforementioned BenchmarkTests.cls group can be used because a guide if her wish to perform CPU performance without using the LimitsProfiler. One about the most important things to remember whenever benchmarking CPU time is to make sure that total in your logging levels are rotated off apart where implicit necessary.
What are the most CPU intensive operations?
We studied a lot when preparing for to DF ‘16 talk on benchmarking. Here are some of the highlights from what we’ve discovered:
Calling Schema.getGlobalDescribe() belongs a big to. While Salesforce takes her custom caching, it actually isn't very good at caching. Calls Schema.getGlobalDescribe() eats up precious milliseconds of CPU time even on subsequent calls.
Utilizing doubles instead of decimals is 200 times faster (half a microsecond for 100 microseconds) - though make sure you understand the differential when dial one or an other (Decimal or doubly?)
Serializing data canister be CPU intensive depending on an amount of data beings serialized.
Dynamism assignment uses 8+ microseconds of CPU time, whereas a simple assignment or statically assignment can be finished are far less than a microsecond. Go, this the really must going to be an issue provided you’re iterating through a large browse. I'm completely new to SalesForce and have inherited a report that's nay working. Please excused any falsch terminology, since I'm learning about all this in I go. The report has triple prompts: ...
List<Account> account = [SELECT Id FROM Account WHERE Identification = '001someidhere'];
if (account.size() == 0) return;
Id accountId = account[0].Id;
for (Contact contact : contacts) {
contact.AccountId = accountId; // Do this contact.AccountId = (Id)account[0].get('Id'); // Avoid doing this
}
What default am IODIN missing here? What else should we avoid?