Skip to content

Instantly share code, notes, and snippets.

@peralta
Created June 23, 2023 16:55
Show Gist options
  • Save peralta/aff3a937d95305a2d7c66b1b2121534f to your computer and use it in GitHub Desktop.
Save peralta/aff3a937d95305a2d7c66b1b2121534f to your computer and use it in GitHub Desktop.
function testSession() {
benchmark(() => {
var x = Session.getActiveUser().getEmail();
},
"testGetUserSession",
100);
}
function testLog() {
var stmt = myFunction();
benchmark(() => {
console.log("testK");
},
"testLog",
100);
}
function runBenchmark() {
runs = 150;
benchmark(() => {
var x = PropertiesService.getScriptProperties().setProperty("testK", "lala");
},
"PropertiesService.getScriptProperties().setProperty",
runs);
benchmark(() => {
var x = PropertiesService.getScriptProperties().getProperty("testK");
},
"PropertiesService.getScriptProperties().getProperty",
runs);
benchmark(() => {
var x = PropertiesService.getUserProperties().setProperty("testK", "lala");
},
"PropertiesService.getUserProperties().setProperty",
runs);
benchmark(() => {
var x = PropertiesService.getUserProperties().getProperty("testK");
},
"PropertiesService.getUserProperties().getProperty",
runs);
benchmark(() => {
var x = CacheService.getScriptCache().put("testK", "lala");;
},
"CacheService.getScriptCache().put",
runs);
benchmark(() => {
var x = CacheService.getScriptCache().get("testK");
},
"CacheService.getScriptCache().get",
runs);
benchmark(() => {
var x = CacheService.getUserCache().put("testK", "lala");;
},
"CacheService.getUserCache().put",
runs);
benchmark(() => {
var x = CacheService.getUserCache().get("testK");
},
"CacheService.getUserCache().get",
runs);
}
function benchmark(func, benchName, count) {
var measures = [];
for (var i = 0; i < count; i++) {
var startTime = new Date();
func();
var endTime = new Date();
measures.push(endTime - startTime);
//if (i % 20 == 0) { console.log(endTime - startTime); }
}
var avg = parseInt(measures.reduce((prev, current) => prev + current) / measures.length);
var dev = parseInt(Math.sqrt(measures.map(x => Math.pow(x - avg, 2)).reduce((a, b) => a + b) / measures.length));
console.log(`${benchName}: min/avg/max/stdev: ${Math.min(...measures)}/${avg}/${Math.max(...measures)}/${dev}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment