-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbasics-performance-timers.js
109 lines (83 loc) · 2.52 KB
/
basics-performance-timers.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// for aws, azure, nodejs - performance timers
// time & timeEnd
let startTimeArray = undefined;
let endTimeArray = undefined;
let strMessage = undefined;
// =========
// If you are working on micrsoft azure functions
// then replace all
// context
// to
// context
// =========
module.exports = {
time: time,
timeEnd: timeEnd
};
function time(strMessage1, context) {
if (!context) {
context = console;
}
if (process.env.CS_TIME_LOGS !== '1') {
return;
}
if (startTimeArray !== undefined) {
context.log.error('Error: timeEnd() was not called, resetting');
startTimeArray = undefined; // clear previous timer
}
startTimeArray = process.hrtime(); // from current time // start new timer
strMessage = strMessage1;
}
function timeEnd(strMessage1, context) {
if (process.env.CS_TIME_LOGS !== '1') {
return;
}
if (!context) {
context = console;
}
if (startTimeArray === undefined) {
context.log.error('Error: time() was not called, resetting');
startTimeArray = undefined; // clear previous timer
endTimeArray = undefined; // clear previous timer
return;
}
if (strMessage !== strMessage1) {
context.log.error('Error: time() & timeEnd() messages don\'t match, resetting');
startTimeArray = undefined; // clear previous timer
endTimeArray = undefined; // clear previous timer
return;
}
endTimeArray = process.hrtime(startTimeArray); // from start time
// [seconds, nanoseconds]
// context.log(strMessage1 + " " + (endTimeArray[0] + (endTimeArray[1] / 1e9)).toFixed(3) + ' seconds');
context.log(strMessage1 + " " + (endTimeArray[0] * 1e6 + (endTimeArray[1] / 1e3)).toFixed(3) + ' microseconds');
// context.log(strMessage1 + " " + (endTimeArray[0] * 1e9 + (endTimeArray[1])) + ' nanoseconds');
startTimeArray = undefined; // clear previous timer
endTimeArray = undefined; // clear previous timer
}
/*
function doMain() {
context.log('=======================');
context.log('testing positive cases');
context.log('=======================');
// case 1: 1 second
time();
for (let i = 0; i < 1e9; i++) {
}
timeEnd();
// case 2: 8 second
time();
for (let i = 0; i < 3 * 1e9; i++) {
}
timeEnd();
context.log('=======================');
context.log('testing negative cases');
context.log('=======================');
// case 3: error scenarios
timeEnd();
time();
time();
context.log('=======================');
}
doMain();
*/