Writing too much to debug log would affect APEX trigger or Visualforce page performance?

In order to debug we usually write chunks of data to the debug log using System.debug statements. Will writing too many debug logs affect visualforce page or Apex performance in any way?

Answer

I created the simplest page in the world, with a custom controller, that looked like this:

simple page

Generated by

<apex:page controller="spinspinspin" action="{!spin}">
    <h1>Congratulations</h1>
    This is your new Page
</apex:page>

And in the controller I simply had

public class spinspinspin {
    public void spin() {
        for(Integer i = 0; i < 100000; i++) {

        }
    }

}

I loaded this page up and inspected the source for the generation time, which is in a HTML comment at the bottom:

generated

Here we see the simple page took 80ms.

I loaded it 10 times and took an average of the load times, it was 78ms.

I then added System.debug to the for loop

for(Integer i = 0; i < 100000; i++) {
    System.debug(i);
}

and loaded the page 10 more times.

I averaged it out and the page now took 4140ms !!

I also then opened the developer console and checked, there were a bunch of 1.72Mb log files, so it was doing what one might expect, and the logging (allbeit 100,000 of them) was significantly slowing down the page load.

developer console

I then took the debug out of the loop (but left the empty loop as per the control) and added a single debug statement with a 3,500 character string in it. The average load for this page was 86ms indicating a single long debug statement does also slow the page.

(after this I was going to go on to a couple of other tests, but when I returned to the control state, I quickly re-tested my base level and they were up into the 100’s of ms so I think either my DE on eu2 was being hammered, or I had simply worn a bit of it out temporarily! Therefore no future results would be fair 😉 )

Update!!

Following other answers surrounding making the debug optional based on a boolean switch, I changed the controller to be this and ran the test again (I also re-ran my control a couple of times and the org was back on 75 – 85ms for standard page load)

private static final boolean logIt = false;

public void spin() {
    for(Integer i = 0; i < 100000; i++) {
        if(logIt) {
            System.debug(i);
        }
    }
}

The page load time was now an average (just 5 runs this time) of 112ms so the boolean flag does make a big difference. Putting that into a custom setting/label etc. and pumping it into the controller etc. might slow you down, but the concept of switchable logging has potential! (I was suspicious, so I changed the flag to true and loaded the page, low and behold, 4 seconds).

Attribution
Source : Link , Question Author : Bforce , Answer Author : Simon Lawrence

Leave a Comment