Code Monkey home page Code Monkey logo

limitsprofiler's Introduction

LimitsProfiler

Purpose

While it may be true that premature optimization is the root of all evil, at a certain point it becomes necessary. Especially if we aim to write streamlined utility classes that will be referenced hundreds or even thousands of times, we should strive for code that performs optimally.

One problem with using scripts in Execute Anonymous to assess performance is that logging levels themselves affect performance! Because of that, any robust solution must include a method to persist the data, so that profiling can be run with logging turned off completely.

Usage

Version 1.0

At the most basic level, this version allows you to see current limits usage with a LimitsSnapshot.

system.debug(LimitsSnapshot.getInstance());

The above script will yield output something like the following:

LimitsSnapshot:[aggregateQueries=0, asyncCalls=0, callouts=0, cpuTime=2, dmlRows=0, dmlStatements=0, emailInvocations=0, futureCalls=0, heapSize=1682, mobilePushApexCalls=0, queries=0, queryLocatorRows=0, queryRows=0, queueableJobs=0, rawTime=1484534149238, soslQueries=0]

This version introduces a basic framework (the LimitsProfiler class) which can be used in Execute Anonymous scripts. Here is a basic example:

static List<Integer> data = new List<Integer>();
for (Integer i = 0; i < 1000000; i++) data.add(i);

class IsEmptyProfiler extends LimitsProfiler
{
    public override void execute()
    {
        Boolean isEmpty = data.isEmpty();
    }
}
class SizeProfiler extends LimitsProfiler
{
    public override void execute()
    {
        Boolean isEmpty = data.size() == 0;
    }
}

system.debug(new IsEmptyProfiler().measure(1000));
system.debug(new SizeProfiler().measure(1000));

Version 1.1

This version introduces a configurable Visualforce UI that can run arbitrary extensions of the LimitsProfiler class. By default, it displays CPU Time, Heap Size, and Raw Time. As an example of how to use it, you can set ProfilerType__c to Profilers.SerializationProfiler and define the following class:

public with sharing class Profilers
{
    public class SerializationProfiler extends LimitsProfiler
    {
        final List<String> data;
        public SerializationProfiler()
        {
            data = new List<String>();
            for (Integer i = 0; i < 1000; i++)
                data.add('a'.repeat(1000));
        }
        public override void execute()
        {
            String payload = JSON.serialize(data);
        }
    }
}

Try running this profiler with various logging levels and you will see just how much performance can vary based on your settings. Even with the conservative example above there can be a difference of over 3x from the lowest to the highest logging level.

More significant than logging levels, however, is that each run constitutes a separate transaction. This benefit has important implications, such as allowing you to run trials which in aggregate consume more than 10s CPU Time. It also means that when you profile triggers which access lazy loaded data (for example), the code will re-initialize each time. To be able to provide a consistent measurement without context switching was not possible using Execute Anonymous alone.

Version 1.2

This version introduces a Limits_Snapshot__c object which allows you to persist data from profiling runs. It tracks all limits usage and can be generated directly from a LimitsSnapshot instance in Apex using the newSObject() method:

Limits_Snaphsot__c record = LimitsSnapshot.getInstance().newSObject();

The Visualforce UI also now includes a Save button, which saves the diffs already measured.

This version also introduces the LimitsProfilerConfig__c hierarchy setting to the repository so it can be pulled directly into an org without requiring any package installation.

Install Links

Production

  • Version 1.2 - Persistent objects make it possible to store and track performance.
  • Version 1.1 - Configurable Visualforce UI makes profiling possible with logging turned off.
  • Version 1.0 - LimitsSnapshot and LimitsProfiler classes enable basic profiling via anonymous scripts

Sandbox

  • Version 1.2 - Persistent objects make it possible to store and track performance.
  • Version 1.1 - Configurable Visualforce UI makes profiling possible with logging turned off.
  • Version 1.0 - LimitsSnapshot and LimitsProfiler classes enable basic profiling via anonymous scripts

limitsprofiler's People

Contributors

apexlarson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.