MCPcopy Index your code
hub / github.com/googleworkspace/apps-script-samples / latency

Function latency

wasm/image-add-on/src/test.js:28–62  ·  view source on GitHub ↗
(func, iterations, argsFunc = () => [])

Source from the content-addressed store, hash-verified

26}
27
28async function latency(func, iterations, argsFunc = () => []) {
29 const executionTimes = [];
30
31 for (let i = 0; i < iterations; i++) {
32 const args = argsFunc();
33
34 const startTime = Date.now();
35 let endTime;
36
37 try {
38 await func(...args);
39 endTime = Date.now();
40 } catch (e) {
41 endTime = Number.POSITIVE_INFINITY;
42 console.error(e);
43 continue;
44 }
45
46 executionTimes.push(endTime - startTime);
47 }
48
49 // Calculate statistics
50 const min = Math.min(...executionTimes);
51 const max = Math.max(...executionTimes);
52 const totalTime = executionTimes.reduce((sum, time) => sum + time, 0);
53 const average = totalTime / iterations;
54
55 return {
56 min: min,
57 max: max,
58 average: average,
59 totalTime,
60 // times: executionTimes // Array of all execution times
61 };
62}
63
64async function benchmark() {
65 await hello_("world"); // Warmup

Callers 1

benchmarkFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected