I have a situation where I need to test whether the apex classes written in my org does not hit governors limit / anyother issues when bulk data is loaded.

I decided to use test class to check this. I created around 200 records using a for loop and inserted. This is from the test class. Then I updated some of the field of each records which resulted some trigger running because of that. At my update command I get - System.LimitException: Apex CPU time limit exceeded.

I am assuming I would get the same error when I update the records using a Dataloader. Is that correct? Will updating using test class and dataloader would give me same error - System.LimitException: Apex CPU time limit exceeded?

  • Default data loader process records in 200 size, but you increase that limit from setting and try to again. May 19, 2017 at 5:14
  • Actually my question is will the behavior of getting System.LimitException: Apex CPU time limit exceeded will be same when I insert records using dataload / test class?
    – SfdcBat
    May 19, 2017 at 5:15
  • It's all depend on how many records you process in one transaction, you are getting Limit Exception because in test class you are inserting as well as updating records and might be that few other triggers invoked and it takes time to process all logic. You can increase batch size in dataloader and verify. May 19, 2017 at 5:28

1 Answer 1



You might be artificially hitting the limit if you forgot to reset the limits before actually inserting the records. Keep in mind that the act of building your test records requires CPU time itself beyond what the data loader would use.

CPU time in a unit test can be "reset" once to simulate closer to real time. Your unit test should prepare the records, then start the test, then do the DML operation.

Here's the correct way to bulk test:

SObject[] records = new SObject[0];
for(Integer i = 0; i < 200; i++) {
  records.add(newRecord()); // newRecord does not do DML.
Test.startTest(); // reset governor limits
insert records;
Integer cpuUsed = Limits.getCpuTime();
Test.stopTest(); // go back to test limits
System.assert(cpuUsed <= 9000, 'It\'s over 9,000! CPU Time was '+cpuUsed);

By making sure your test took less than 9 seconds, you give yourself some breathing room. Or you might choose another limit you'd prefer to hit/target. You can always change this later, but this canary test can warn you of impending performance issues.

  • Thanks. My DMl is inside test.start and stop method.I do get the CPU time limit exceeded error. I have yet to put limits.getCputitme() to see how much time it took.
    – SfdcBat
    May 20, 2017 at 3:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .