-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrunTest.js
More file actions
executable file
·157 lines (135 loc) · 4.25 KB
/
runTest.js
File metadata and controls
executable file
·157 lines (135 loc) · 4.25 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
#!/usr/bin/env node --require dotenv/config
/**
* See settings in ds_configuration.js
*/
const dsConfig = require('./ds_configuration.js').config
, fse = require('fs-extra')
, path = require('path')
, rp = require('request-promise-native')
, testOutputDirName = dsConfig.testOutputDirName
, testOutputDir = path.join(path.normalize("."), testOutputDirName)
, moment = require('moment')
, sleep = (seconds) => {
return new Promise(resolve => setTimeout(resolve, 1000 * seconds))}
, log = msg => {console.log(`${new Date().toUTCString()} ${msg}`)}
;
let timeStart
, timeChecks = []
, timeCheckNumber = 0 // 0..6
, enqueueErrors = 0
, dequeueErrors = 0
, successes = 0
, mode // help, many or few
, testsSent = [] // test values sent that should also be receieved
, foundAll = false
;
async function startTest() {
timeStart = moment()
for (let i = 0; i <= 7; i++) {
timeChecks[i] = moment(timeStart).add(i + 1, 'h')
}
log("Starting");
await doTests();
log("Done.\n");
}
async function doTests() {
while (timeCheckNumber <= 7) {
while (moment().isBefore(timeChecks[timeCheckNumber])) {
await doTest();
if (mode == "few") {
await sleep(moment.duration(moment().diff(timeChecks[timeCheckNumber])).asSeconds() + 2)
}
}
showStats();
timeCheckNumber ++;
}
showStats();
}
function showStats() {
const rate = Math.round((100.0 * successes) / (enqueueErrors + dequeueErrors + successes));
log (`##### Test statistics: ${successes} (${rate}%) successes, ${enqueueErrors} enqueue errors, ${dequeueErrors} dequeue errors.`)
}
async function doTest() {
await send(); // sets testsSent
const endTime = moment().add(3, 'minutes');
foundAll = false;
const tests = testsSent.length,
successesStart = successes;
while (!foundAll && moment().isBefore(endTime)){
await sleep(1);
await checkResults(); // sets foundAll and updates testsSent
}
if (!foundAll) {
dequeueErrors += testsSent.length;
}
log (`Test: ${tests} sent. ${successes - successesStart } successes, ${testsSent.length} failures.`)
}
/**
* Look for the reception of the testsSent values
*/
async function checkResults(){
let testsReceived = [];
for (let i = 1; i <= 20; i++) {
let fileData = null;
try {fileData = await fse.readFile(path.join(testOutputDir, `test${i}.txt`))} catch(e){}
if (fileData) {testsReceived.push(fileData.toString())}
}
// Create a private copy of testsSent (testsSentOrig) and reset testsSent
// Then, for each element in testsSentOrig not found, add back to testsSent.
let testsSentOrig = testsSent;
testsSent = [];
testsSentOrig.forEach(testValue => {
const found = testsReceived.includes(testValue);
if (found) {successes ++}
else {testsSent.push(testValue)}
})
// Update foundAll
foundAll = testsSent.length == 0
}
async function send() {
testsSent = [];
for (let i = 0 ; i < 5; i++) {
try {
const testValue = Date.now().toString();
await send1(testValue);
testsSent.push(testValue)
} catch (e) {
enqueueErrors ++;
log (`Enqueue error: ${e}`);
await sleep(30);
}
}
}
/**
* Send one enqueue request. Errors will be caught by caller
* @param {string} test The test value
*/
async function send1(test){
let options = {url: `${dsConfig.testEnqueueUrl}?test=${test}`, method: 'POST', body: ''}
, auth = authObject();
if (auth) {options.auth = auth};
return await rp(options)
}
/**
* Returns an auth object for the request library or false
* if Basic Auth is not being used
*/
function authObject() {
if (dsConfig.basicAuthName && dsConfig.basicAuthName != '{BASIC_AUTH_NAME}') {
return {user: dsConfig.basicAuthName, pass: dsConfig.basicAuthPW}
} else {
return false
}
}
////////////////////////////////////
//
// Mainline
if (process.argv.length < 3) {mode = 'help'}
else {mode = process.argv[2]}
if (mode === 'help') {
console.log(`
./runTest.js many # send many tests
./runTest.js few # send five tests, wait an hour, repeat
Tests run for 8 hours, with interim reports every hour.\n`)
} else if (mode === 'many' || mode === 'few') {startTest()
} else {console.log(`\nProblem: unrecogpnized mode '${mode}'\n`)}