Created
May 30, 2024 11:14
-
-
Save jfwberg/08ae86748889db940f4a2cd8d269477c to your computer and use it in GitHub Desktop.
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
/** | |
* Method that creates 4 6MB strings and calculates the time it takes | |
*/ | |
// Keep track of the total usage | |
Integer totalCpu = 0; | |
Integer totalHeap= 0; | |
// Specify the number of strings | |
Integer numberOfStrings = 4; | |
// Generate the strings one by one | |
for(Integer i =0; i<numberOfStrings;i++){ | |
// Start measuring limites | |
Integer heapS = Limits.getHeapSize(); | |
Integer cpuS = Limits.getCpuTime(); | |
// Example of how a string generation method is used directly in a method, like getting the size of the blob | |
System.debug('Blob size: ' + Blob.valueOf( | |
generateJoinedListString() | |
).size() + ' - Iteration ' + (i+1) + '/' + numberOfStrings); | |
// Finish measuring limites | |
Integer cpuE = Limits.getCpuTime(); | |
Integer heapE = Limits.getHeapSize(); | |
// Output results | |
System.debug('CPU Time: ' + (cpuE - cpuS) ); | |
System.debug('Heap Size: ' + (heapE - heapS)); | |
// Calculate totals | |
totalCpu +=Integer.valueOf(cpuE - cpuS ); | |
totalHeap+=Integer.valueOf(heapE - heapS); | |
} | |
// Output results | |
System.debug('TOTAL CPU Time: ' + totalCpu ); | |
System.debug('TOTAL Heap Size: ' + totalHeap); | |
System.debug('CURRENT CPU Time: ' + Limits.getCpuTime()); | |
System.debug('CURRENT Heap Size: ' + Limits.getHeapSize()); | |
/** | |
* Method that generates a 6MB string | |
*/ | |
public static String generateJoinedListString(){ | |
final String testString = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ01234'; | |
// Create a list to hold all the lines in our CSV | |
// I add an empty line else the file size is 5.999.999 | |
// It's also easier to debug... | |
String[] lines = new String[]{''}; | |
for(Integer rowI=0; rowI < 7500; rowI++){ | |
// Create a list that holds all the columns, representing a single line | |
String[] line = new String[]{}; | |
for(Integer colI=0; colI < 25; colI++){ | |
// Instead of concatenating strings, we add them to a list | |
line.add(testString.escapeCsv()); | |
} | |
// Let's add all column values into a list representing a line and join them with a comma | |
lines.add(String.join(line, ',')); | |
} | |
// Directly return all the joined columns (lines) and join each full line with a return | |
return String.join(lines, '\n'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment