forked from zenorocha/tracking.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.js
More file actions
208 lines (177 loc) · 5 KB
/
Copy pathbenchmark.js
File metadata and controls
208 lines (177 loc) · 5 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
var fs = require('fs');
var PNG = require('png-js');
var BENCHMARK_FILENAME = 'benchmark.json';
var PATH_PERF_TESTS = 'test/perf';
var PATH_BENCHMARK_FILE = 'test/assets/' + BENCHMARK_FILENAME;
var TEST_TIMES_RUN = 5;
var TIME_THRESHOLD = 30;
var Benchmark = {
/**
* Calls the setUp function for all performance tests.
* @param {function} done Function to be called when the set up is done.
*/
setUpAll: function(done) {
readTestFiles(function(files) {
Benchmark.files = files;
runAllSetUps(done);
});
},
/**
* Runs all the performance tests.
* @param {function} callback Function to be called when all tests finish running.
*/
runAll: function(callback) {
var results;
getBenchmarkContents(function(benchmark) {
results = runAllTests(benchmark);
fs.writeFile(PATH_BENCHMARK_FILE, JSON.stringify(benchmark), function() {
callback(results);
});
});
},
/**
* Creates a failure message for the given results.
* @param {array} results An array with the results of the performance tests.
*/
createFailureMessage: function(results) {
var message = '';
for(var i = 0; i < results.length; i++) {
if (results[i].failedTests.length > 0) {
message += '\n' + results[i].failedTests.length + ' failures for ' + results[i].filename + ':';
for (var j = 0; j < results[i].failedTests.length; j++) {
message += '\n\t' + results[i].failedTests[j];
}
}
}
return message;
}
};
/**
* Gets the contents of the benchmark json file.
* @param {function} callback Function to be called with the contents.
*/
function getBenchmarkContents(callback){
fs.exists(PATH_BENCHMARK_FILE, function (exists) {
if (exists) {
fs.readFile(PATH_BENCHMARK_FILE, 'utf8', function(error, data) {
callback(JSON.parse(data));
});
} else {
callback({});
}
});
}
/**
* Reads all performance test files.
* @param {function} callback Function to be called when all tests are read.
*/
function readTestFiles(callback) {
fs.readdir(PATH_PERF_TESTS, function(error, files_list) {
var files = [];
for (var i = 0; i < files_list.length; i++) {
var filename = files_list[i];
if (filename.match('.*.js$')) {
files.push({
filename: filename,
test: require('../../' + PATH_PERF_TESTS + '/' + filename)
});
}
}
callback(files);
});
}
/**
* Runs the setUp function for each performance test.
* @param {function} callback Function to be called when all set ups are done.
*/
function runAllSetUps(callback) {
Benchmark.totalDone = 0;
for (var i = 0; i < Benchmark.files.length; i++) {
test = Benchmark.files[i].test;
if (test.setUp) {
test.setUp(function() {
setUpDone(callback);
});
} else {
setUpDone(callback);
}
}
}
/**
* Runs all the performance tests.
* @param {object} benchmark Information about duration of previous test runs.
*/
function runAllTests(benchmark) {
var allResults = [];
var benchmarkTime;
var benchmarkTimes;
var currentResults;
var duration;
var filename;
var passed = true;
var test;
for (var i = 0; i < Benchmark.files.length; i++) {
filename = Benchmark.files[i].filename;
test = Benchmark.files[i].test;
if (!benchmark[filename]) {
benchmark[filename] = {};
}
currentResults = {
filename: filename,
failedTests: [],
passedTests: []
};
for (var key in test) {
if (test.hasOwnProperty(key) && (typeof test[key] === 'function') && key !== 'setUp') {
duration = runTest(test[key]);
benchmarkTimes = benchmark[filename][key];
if (benchmarkTimes && benchmarkTimes.length) {
benchmarkTime = benchmarkTimes[benchmarkTimes.length - 1];
if (duration > benchmarkTime + TIME_THRESHOLD) {
currentResults.failedTests.push(key);
passed = false;
}
else {
currentResults.passedTests.push(key);
benchmark[filename][key].push(duration);
}
}
else {
currentResults.passedTests.push(key);
benchmark[filename][key] = [duration];
}
}
}
allResults.push(currentResults);
}
return {
passed: passed,
resultDetails: allResults
};
}
/**
* Runs a single test.
* @param {object} test The test to be run.
*/
function runTest(test) {
var timeBefore;
var timeTotal = 0;
for (var i = 0; i < TEST_TIMES_RUN; i++) {
timeBefore = new Date().getTime();
test();
timeTotal += new Date().getTime() - timeBefore;
}
return timeTotal / TEST_TIMES_RUN;
}
/**
* Increments the number of setUp functions that are done, and invokes the callback when all
* have been finished.
* @param {function} callback The function to be called when the last setUp function is done
*/
function setUpDone(callback) {
Benchmark.totalDone++;
if (Benchmark.totalDone === Benchmark.files.length) {
callback();
}
}
module.exports = Benchmark;