Last active
October 18, 2023 13:02
-
-
Save itsmebasti/efb9b48168d737bbd2b41ada845ac194 to your computer and use it in GitHub Desktop.
Apex SObject Describes performance testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String objectName = 'Account'; | |
SObjectType sObjecType = Account.SObjectType; | |
Account record = new Account(); | |
List<String> names = new List<String> { objectName }; | |
Integer iterations = 50000; | |
static Long logTime = Limits.getCpuTime(); | |
static void log(String text) { | |
System.debug(LoggingLevel.ERROR, (Limits.getCpuTime() - logTime) + ' - ' + text); | |
logTime = Limits.getCpuTime(); | |
} | |
log('start'); | |
for (Integer i = 0; i < iterations; i++) { | |
DescribeSObjectResult describeResult = sObjecType.getDescribe(); | |
} | |
log('sObjecType.getDescribe()'); | |
Map<String, SObjectType> types = Schema.getGlobalDescribe(); | |
for (Integer i = 0; i < iterations; i++) { | |
DescribeSObjectResult describeResult = types.get(objectName).getDescribe(); | |
} | |
log('Global Describe (stored in anonymous Apex)'); | |
for (Integer i = 0; i < iterations; i++) { | |
DescribeSObjectResult describeResult = GlobalDescribe.TYPES.get(objectName).getDescribe(); | |
} | |
log('Global Describe (stored in a class)'); | |
for (Integer i = 0; i < iterations; i++) { | |
DescribeSObjectResult describeResult = record.getSObjectType().getDescribe(); | |
} | |
log('record.getSObjectType().getDescribe()'); | |
for (Integer i = 0; i < iterations; i++) { | |
DescribeSObjectResult describeResult = GlobalDescribe.get(objectName); | |
} | |
log('Cached DescribeResult (stored in class after first request)'); | |
for (Integer i = 0; i < iterations; i++) { | |
DescribeSObjectResult describeResult = Account.SObjectType.getDescribe(); | |
} | |
log('Account.SObjectType.getDescribe()'); | |
for (Integer i = 0; i < iterations; i++) { | |
DescribeSObjectResult describeResult = SObjectType.Account; | |
} | |
log('SObjectType.Account'); | |
for (Integer i = 0; i < iterations; i++) { | |
DescribeSObjectResult describeResult = GlobalDescribe.uncached(objectName); | |
} | |
log('Global Describe (stored in a class, describe not cached)'); | |
for (Integer i = 0; i < iterations; i++) { | |
DescribeSObjectResult describeResult = types.get(''+Account.SObjectType).getDescribe(); | |
} | |
log('stringified Account.SObjectType with anonymous Global Describe'); | |
for (Integer i = 0; i < iterations; i++) { | |
DescribeSObjectResult describeResult = GlobalDescribe.type(objectName).getDescribe(); | |
} | |
log('Cached SObjectType retrieved by accessor method, then call .getDescribe()'); | |
for (Integer i = 0; i < iterations; i++) { | |
DescribeSObjectResult describeResult = fflib_SObjectDescribe.getDescribe(objectName).getDescribe(); | |
} | |
log('fflib_SObjectDescribe.getDescribe(objectName).getDescribe()'); | |
for (Integer i = 0; i < iterations; i++) { | |
DescribeSObjectResult describeResult = Schema.describeSObjects(names)[0]; | |
} | |
log('Schema.describeSObjects(objectName)'); | |
for (Integer i = 0; i < iterations; i++) { | |
DescribeSObjectResult d = ((SObject) Type.forName('Schema', objectName).newInstance()).getSObjectType().getDescribe(); | |
} | |
log('Type.forName(\'Schema\', objectName)'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class GlobalDescribe { | |
public static Map<String, SObjectType> TYPES = Schema.getGlobalDescribe(); | |
public static Map<String, DescribeSObjectResult> DESCRIBES = new Map<String, DescribeSObjectResult>(); | |
public static SObjectType type(String name) { | |
return TYPES.get(name); | |
} | |
public static DescribeSObjectResult get(String name) { | |
if(!DESCRIBES.containsKey(name)) { | |
DESCRIBES.put(name, type(name).getDescribe()); // consider using SObjectDescribeOptions.FULL if needed | |
} | |
return DESCRIBES.get(name); | |
} | |
public static DescribeSObjectResult uncached(String name) { | |
return TYPES.get(name).getDescribe(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | |
<apiVersion>53.0</apiVersion> | |
<status>Active</status> | |
</ApexClass> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment