Skip to content

Commit 9d67064

Browse files
authored
Merge pull request #2 from godfreydoo/get-test-data-auto
Test data is now automatically retrieved and ran
2 parents 108cdce + e3e6199 commit 9d67064

2 files changed

Lines changed: 87 additions & 73 deletions

File tree

README.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# retrieve-loaderio-test-data
22

3-
This repo helps you perform your load testing on [Loader.io](https://loader.io/) by
3+
This application helps you perform your load testing on [Loader.io](https://loader.io/) by
44
1. sequentially executing prepared tests with the same duration,
55
2. extracting test results from current run, and
66
3. writing to a CSV file for further analysis.
@@ -9,7 +9,6 @@ This repo helps you perform your load testing on [Loader.io](https://loader.io/)
99
## Limitations
1010
- assumes all tests have the same duration
1111
- includes a buffer time between running the next test (you could change the core functions to check for the status before running subsequent tests)
12-
- test names must be unique in the `testSuite` object
1312

1413
## Installation
1514
1. Fork and clone this repository.
@@ -21,15 +20,8 @@ $ npm install
2120
4. Update `index.js` file with your test configurations.
2221
- `duration` is the duration of your test cases.
2322
- `buffer` is a buffer time between when the next test case runs. This is important, as Loader.io does not allow for more than one running test at once.
24-
- `testSuite` is an object with the key as the name of your test, and value as your test id. Test names must be unique.
25-
26-
To find the test id for your tests, you can run this command. Replace `API_KEY` with your API key.
27-
```bash
28-
curl -H 'loaderio-auth: API_KEY' https://api.loader.io/v2/tests | json_pp
29-
```
30-
3123
5. Ensure your application is running as Loader.io will need to verify your token.
32-
6. Run to start tests
24+
6. Run to start tests. Application will retrieve all your tests for you so you don't have to input your test name and test id.
3325
```
3426
node index.js
3527
```

index.js

Lines changed: 85 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@ require('dotenv').config();
55

66

77

8-
// Define your test ids, test names, and test parameters
9-
var duration = 30000; // in ms, ie. 30 seconds
10-
var buffer = 5000; // in ms, i.e. 5 second buffer (I found this to be a good buffer)
11-
var url = 'https://api.loader.io/v2/tests'
12-
var testSuite = {
13-
'<your test name>': '<your test id>',
14-
'<your test name>': '<your test id>'
15-
};
8+
// Define your test duration
169

10+
var duration = 30000; // in ms, ie. 60 seconds
11+
var buffer = 5000; // in ms, i.e. 5 second buffer (I found this to be a good buffer)
1712

1813

1914

@@ -22,65 +17,88 @@ var testSuite = {
2217

2318

2419

20+
// Core functions -- do not change anything below!
2521

22+
var url = 'https://api.loader.io/v2/tests'
2623

27-
// Core functions -- do not change anything below!
28-
function LoaderTests() {
24+
function LoaderIOTests() {
25+
this._tests = {};
2926
this._testResults = [];
30-
this._testSuite = [];
3127
}
3228

33-
LoaderTests.prototype.runTest = function (testName, testidx){
34-
console.log(`${testName} test is running...`);
35-
return new Promise((resolve, reject) => {
36-
const config = {
37-
method: 'put',
38-
url: `${url}/${testidx}/run`,
39-
headers: {
40-
'loaderio-auth': process.env.LOADER_API_KEY
41-
}
42-
};
43-
axios(config)
44-
.then(response => {
45-
const obj =
46-
this._testSuite.push({
47-
testName: testName,
48-
testidx: testidx,
49-
resultidx: response.data.result_id
50-
});
51-
resolve();
52-
})
53-
.catch(error => {
54-
console.log(`Something went wrong with running testidx: ${testidx}`);
55-
console.log(error);
56-
reject();
57-
})
58-
})
29+
LoaderIOTests.prototype.getTests = async function() {
30+
const config = {
31+
url: url,
32+
headers: { 'loaderio-auth': process.env.LOADER_API_KEY }
33+
}
34+
try {
35+
let { data } = await axios(config);
36+
let extractedData = this.extractNameAndTotal(data);
37+
this._tests = extractedData;
38+
} catch (error) {
39+
console.log(`Something went wrong with fetching test results for resultidx: ${resultidx}`);
40+
console.error(error);
41+
}
42+
}
43+
44+
45+
LoaderIOTests.prototype.extractNameAndTotal = function(testData) {
46+
var results = {};
47+
for (var i = 0; i < testData.length; i++) {
48+
var testid = testData[i].test_id;
49+
var details = {};
50+
details.name = testData[i].name;
51+
details.total_clients = testData[i].total;
52+
details.duration = testData[i].duration;
53+
details.testidx = testid;
54+
55+
results[testid] = details;
56+
}
57+
return results;
58+
}
59+
60+
LoaderIOTests.prototype.runTestAndSaveResultId = async function(testidx, name) {
61+
console.log(`${name} is running...`);
62+
63+
const config = {
64+
method: 'put',
65+
url: `${url}/${testidx}/run`,
66+
headers: { 'loaderio-auth': process.env.LOADER_API_KEY }
67+
};
68+
69+
try {
70+
let { data } = await axios(config);
71+
this._tests[testidx].resultidx = data.result_id;
72+
} catch (error) {
73+
if (error.response.status === 422) {
74+
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.');
75+
} else {
76+
console.log(`Something went wrong with running testidx: ${testidx}`);
77+
console.error(error);
78+
}
79+
}
5980
}
6081

61-
LoaderTests.prototype.getTestResults = async function (testName, testidx, resultidx, datetime){
62-
var testName = testName;
63-
var resultidx = resultidx;
82+
LoaderIOTests.prototype.getTestResults = async function(testidx, resultidx, name, duration, totalClients) {
6483
const config = {
6584
url: `${url}/${testidx}/results/${resultidx}`,
6685
headers: {
6786
'loaderio-auth': process.env.LOADER_API_KEY
6887
}
6988
}
70-
71-
await axios(config)
72-
.then(response => {
73-
response.data['test_name'] = testName;
74-
this.write2CSV(datetime);
75-
this._testResults.push(response.data);
76-
})
77-
.catch(error => {
78-
console.log(`Something went wrong with fetching test results for resultidx: ${resultidx}`);
79-
console.log(error);
80-
});
89+
try {
90+
let { data } = await axios(config);
91+
data.name = name;
92+
data.duration = duration;
93+
data.total_clients = totalClients;
94+
this._testResults.push(data);
95+
} catch (error) {
96+
console.log(`Something went wrong with fetching test results for resultidx: ${resultidx}`);
97+
console.error(error);
98+
}
8199
}
82100

83-
LoaderTests.prototype.write2CSV = async function (datetime) {
101+
LoaderIOTests.prototype.write2CSV = async function(datetime) {
84102
const writeCSVStream = fs.createWriteStream(`results_${datetime}.csv`);
85103
await converter.json2csv(this._testResults, (err, csv) => {
86104
if (err) {
@@ -91,27 +109,31 @@ LoaderTests.prototype.write2CSV = async function (datetime) {
91109
})
92110
}
93111

94-
function sleep(ms) {
112+
function sleep(fn) {
95113
return new Promise(resolve => setTimeout(resolve, duration + buffer));
96114
}
97115

98116
async function run() {
99-
var newRun = new LoaderTests;
117+
var newRun = new LoaderIOTests;
100118
try {
119+
console.log('Retrieving your test data...');
120+
await newRun.getTests();
101121
console.log('Starting to run tests...');
102-
for (var key in testSuite) {
103-
await sleep(newRun.runTest(key, testSuite[key]))
122+
for (var key in newRun._tests) {
123+
await sleep(newRun.runTestAndSaveResultId(key, newRun._tests[key].name));
104124
}
105125
console.log('All tests ran...');
106126
console.log('Fetching data and writing to CSV file...');
107-
const datetime = Date.now();
108-
await newRun._testSuite.forEach(value => {
109-
newRun.getTestResults(value.testName, value.testidx, value.resultidx, datetime)
110-
});
127+
for (var key in newRun._tests) {
128+
var test = newRun._tests[key];
129+
await newRun.getTestResults(key, test.resultidx, test.name, test.duration, test.total_clients)
130+
}
131+
const datetime = new Date();
132+
newRun.write2CSV(datetime);
111133
console.log('results.csv file should be created / updated soon');
112-
} catch (err) {
113-
console.log('Double check testSuite has right data, and that the duration + buffer is accurate for your test settings');
114-
console.log(err);
134+
} catch (error) {
135+
console.log('Double check the duration + buffer is accurate for your test settings');
136+
console.error(error);
115137
}
116138
}
117139

0 commit comments

Comments
 (0)