-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeasure-time-execution.js
More file actions
86 lines (69 loc) · 3.49 KB
/
measure-time-execution.js
File metadata and controls
86 lines (69 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//Script which can be executed as a Background Script to measure time of execution of specified script
var ITERATIONS = 1000; //Number of iterations that script should be executed for better avg time results
var scriptExecution = Class.create(); //Class for every script execution
scriptExecution.prototype = {
initialize: function(scriptFunction) {
this.executionTimePerIteration = []; // Array to keep execution time of every iteration
this.timerStartTime, this.timerEndTime, this.elapsedTimeMs = null; // Initialize timer variables with null value
this.executeFunction = scriptFunction;
},
//Return array of execution time per iteration
getExecutionTimePerIteration: function() {
return this.executionTimePerIteration.toString();
},
//Return total execution time of all iterations
getTotalElapsedTime: function() {
return this.executionTimePerIteration.reduce(function(acc, val) {
return acc + val;
}, 0);
},
//Return average execution time per iteration
getAVG: function() {
return this.getTotalElapsedTime() / ITERATIONS;
},
//Return max execution time
getMax: function() {
return this.executionTimePerIteration.reduce(function(a, b) {
return Math.max(a, b);
});
},
//Return min execution time
getMin: function() {
return this.executionTimePerIteration.reduce(function(a, b) {
return Math.min(a, b);
});
},
type: 'scriptExecution'
};
var scriptsArray = []; //Array for every script instance to perform time measurements
//================================================ SCRIPT #0
scriptsArray.push(new scriptExecution(function() {
//=======================
//Put your script #0 code
//=======================
}));
//================================================ SCRIPT #1
scriptsArray.push(new scriptExecution(function() {
//=======================
//Put your script #1 code
//=======================
}));
//Calculation time execution of every script instance in scriptsArray
for (var i = 0; i < ITERATIONS; i++) {
for (iter in scriptsArray) {
scriptsArray[iter].timerStartTime = new Date(); //Start time of Script execution
scriptsArray[iter].executeFunction();
scriptsArray[iter].timerEndTime = new Date(); //End time of Script execution
scriptsArray[iter].elapsedTimeMs = scriptsArray[iter].timerEndTime - scriptsArray[iter].timerStartTime; //Calculate elapsed time by subtract starting value from final value
scriptsArray[iter].executionTimePerIteration.push(scriptsArray[iter].elapsedTimeMs);
}
}
for (iter in scriptsArray) {
//Log message with final execution time and statistics
gs.info('[Time Measure] - SCRIPT#' + iter + ' Array with all iteration times: ' + scriptsArray[iter].getExecutionTimePerIteration());
gs.info('[Time Measure] - SCRIPT#' + iter + ' Execution time of all iterations: ' + scriptsArray[iter].getTotalElapsedTime() + 'ms.');
gs.info('[Time Measure] - SCRIPT#' + iter + ' Average execution time of one iteration: ' + scriptsArray[iter].getAVG() + 'ms.');
gs.info('[Time Measure] - SCRIPT#' + iter + ' MAX execution time of iteration: ' + scriptsArray[iter].getMax() + 'ms.');
gs.info('[Time Measure] - SCRIPT#' + iter + ' MIN execution time of iteration: ' + scriptsArray[iter].getMin() + 'ms.');
gs.info('--------------------------------------------------------------------------------------------------------------');
}