-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
140 lines (114 loc) · 3.91 KB
/
index.js
File metadata and controls
140 lines (114 loc) · 3.91 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
const converter = require('json-2-csv');
const axios = require('axios');
const fs = require('fs');
require('dotenv').config();
// Define your test duration
var duration = 30000; // in ms, ie. 60 seconds
var buffer = 5000; // in ms, i.e. 5 second buffer (I found this to be a good buffer)
// Core functions -- do not change anything below!
var url = 'https://api.loader.io/v2/tests'
function LoaderIOTests() {
this._tests = {};
this._testResults = [];
}
LoaderIOTests.prototype.getTests = async function() {
const config = {
url: url,
headers: { 'loaderio-auth': process.env.LOADER_API_KEY }
}
try {
let { data } = await axios(config);
let extractedData = this.extractNameAndTotal(data);
this._tests = extractedData;
} catch (error) {
console.log(`Something went wrong with fetching test results for resultidx: ${resultidx}`);
console.error(error);
}
}
LoaderIOTests.prototype.extractNameAndTotal = function(testData) {
var results = {};
for (var i = 0; i < testData.length; i++) {
var testid = testData[i].test_id;
var details = {};
details.name = testData[i].name;
details.total_clients = testData[i].total;
details.duration = testData[i].duration;
details.testidx = testid;
results[testid] = details;
}
return results;
}
LoaderIOTests.prototype.runTestAndSaveResultId = async function(testidx, name) {
console.log(`${name} is running...`);
const config = {
method: 'put',
url: `${url}/${testidx}/run`,
headers: { 'loaderio-auth': process.env.LOADER_API_KEY }
};
try {
let { data } = await axios(config);
this._tests[testidx].resultidx = data.result_id;
} catch (error) {
if (error.response.status === 422) {
console.log('Error 422 received. Try increasing the buffer time between tests. This error is most likely due to running more than one test at once or could be some lag between requests and responses; however, the script will continue to run.');
} else {
console.log(`Something went wrong with running testidx: ${testidx}`);
console.error(error);
}
}
}
LoaderIOTests.prototype.getTestResults = async function(testidx, resultidx, name, duration, totalClients) {
const config = {
url: `${url}/${testidx}/results/${resultidx}`,
headers: {
'loaderio-auth': process.env.LOADER_API_KEY
}
}
try {
let { data } = await axios(config);
data.name = name;
data.duration = duration;
data.total_clients = totalClients;
this._testResults.push(data);
} catch (error) {
console.log(`Something went wrong with fetching test results for resultidx: ${resultidx}`);
console.error(error);
}
}
LoaderIOTests.prototype.write2CSV = async function(datetime) {
const writeCSVStream = fs.createWriteStream(`results_${datetime}.csv`);
await converter.json2csv(this._testResults, (err, csv) => {
if (err) {
console.log('Writing from json2csv has an error: ', err);
return;
}
writeCSVStream.write(csv);
})
}
function sleep(fn) {
return new Promise(resolve => setTimeout(resolve, duration + buffer));
}
async function run() {
var newRun = new LoaderIOTests;
try {
console.log('Retrieving your test data...');
await newRun.getTests();
console.log('Starting to run tests...');
for (var key in newRun._tests) {
await sleep(newRun.runTestAndSaveResultId(key, newRun._tests[key].name));
}
console.log('All tests ran...');
console.log('Fetching data and writing to CSV file...');
for (var key in newRun._tests) {
var test = newRun._tests[key];
await newRun.getTestResults(key, test.resultidx, test.name, test.duration, test.total_clients)
}
const datetime = new Date();
newRun.write2CSV(datetime);
console.log('results.csv file should be created / updated soon');
} catch (error) {
console.log('Double check the duration + buffer is accurate for your test settings');
console.error(error);
}
}
run();