-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (132 loc) · 4.77 KB
/
index.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
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
const {
calculateTotalTime,
generateArtilleryScript,
generateLoadPhases,
generateSoakPhases,
generateSpikePhases,
generateStressPhases
} = require("./generateArtilleryConfig");
const exec = require("child_process").exec;
const JSONreplacer = null;
const JSONspacer = 2;
const saveScriptToFile = (script, fileName) => {
const scriptData = JSON.stringify(script, JSONreplacer, JSONspacer);
const fs = require("fs");
fs.writeFileSync(fileName, scriptData);
};
class Whirlwind {
constructor(artilleryCommand) {
this.artilleryCommand = artilleryCommand || 'artillery';
this.runTest = this.runTest.bind(this);
this.runTestAsync = this.runTestAsync.bind(this);
this.prepareAndSaveScript = this.prepareAndSaveScript.bind(this);
}
executeArtillery(fileName, local = false) {
if (process.env.TARGET_HOST) {
console.log("Starting test on target:", process.env.TARGET_HOST);
}
let child;
const fullFilePath = `${__dirname}/../../${fileName}`;
if (local) {
child = exec(`cd ${__dirname} && ${this.artilleryCommand} run ${fullFilePath}`);
} else {
child = exec(`cd ${__dirname} && slsart invoke -p ${fullFilePath}`);
}
child.stdout.on("data", function(data) {
console.log(data.toString());
});
child.stderr.on("data", function(data) {
console.log(data.toString());
});
}
async executeArtilleryAsync(fileName, local = false) {
if (process.env.TARGET_HOST) {
console.log("Starting test on target:", process.env.TARGET_HOST);
}
const fullFilePath = `${__dirname}/../../${fileName}`;
const processCommand = local ? `cd ${__dirname} && ${this.artilleryCommand} run ${fullFilePath}` : `cd ${__dirname} && slsart invoke -p ${fullFilePath}`;
return new Promise(async resolve => {
exec(processCommand, (err, stout, sterr) => {
resolve(err ? sterr : stout)
});
});
}
generatePhases(testParams) {
switch (testParams.testType) {
case "load":
this.phases = generateLoadPhases(
testParams.startLoad,
testParams.endLoad,
testParams.rampUpTime
);
break;
case "stress":
this.phases = generateStressPhases(
testParams.startLoad,
testParams.endLoad,
testParams.rampUpTimePerStep,
testParams.flatDurationPerStep,
testParams.numberOfSteps,
testParams.rampUpType
);
break;
case "soak":
this.phases = generateSoakPhases(testParams.load, testParams.duration);
break;
case "spike":
this.phases = generateSpikePhases(
testParams.consistentLoad,
testParams.spikeLoad,
testParams.loadTestDuration,
testParams.spikeDuration,
testParams.numberOfSpikes
);
break;
default:
throw "You can start one of: load, stress, soak, or spike test.";
}
}
// TODO config should be a separate class and this should be responsibility of that class
// variables: array of { name: string, values: string[] } objects
generateVariables(variables) {
this.variables = {};
variables.forEach(variable => {
this.variables[variable.name] = variable.values;
});
}
prepareAndSaveScript(scenarios, processorFilename, disableSslCertificateChecking, optionOverrides) {
if (!this.phases) {
throw "You need to generate phases by running whirlwind.generatePhases()";
}
const script = generateArtilleryScript(optionOverrides);
script.config.phases = this.phases;
if (this.variables) {
script.config.variables = this.variables;
}
script.scenarios = scenarios;
if (disableSslCertificateChecking) {
script.config.tls = {
"rejectUnauthorized": false
}
}
if (processorFilename) {
script.config.processor = processorFilename;
}
// script.config.http = {};
// script.config.http.timeout = 60;
const totalTime = calculateTotalTime(this.phases);
console.log("Total load test duration: ", totalTime);
const fileName = "scriptLoadTest.json";
saveScriptToFile(script, fileName);
return fileName;
}
runTest(scenarios, processorFilename = false, local = false, disableSslCertificateChecking = false, optionOverrides = {}) {
const fileName = this.prepareAndSaveScript(scenarios, processorFilename, disableSslCertificateChecking, optionOverrides);
this.executeArtillery(fileName, local);
}
async runTestAsync(scenarios, processorFilename = false, local = false, disableSslCertificateChecking = false, optionOverrides = {}) {
const fileName = this.prepareAndSaveScript(scenarios, processorFilename, disableSslCertificateChecking, optionOverrides);
return (await this.executeArtilleryAsync(fileName, local));
}
}
module.exports = Whirlwind;