-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoadTestOrchestrator.ts
206 lines (171 loc) · 6.44 KB
/
LoadTestOrchestrator.ts
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
import I = require('./Interfaces');
import Q = require('q');
import assert = require('assert');
import request = require('request');
let logger = require('./logger');
let settings: I.Settings = require('./config/settings');
import PlayerSessionFactory = require('./PlayerSessionFactory');
import LoadTestRunner = require('./LoadTestRunner');
import PlayerRunner = require('./PlayerRunner');
class LoadTestOrchestrator implements I.RunnerCleanup {
private readonly playerRunner: PlayerRunner;
private loadTestRunner: LoadTestRunner;
private readonly playerSessionFactory: PlayerSessionFactory;
constructor(playerRunner: PlayerRunner, playerSessionFactory: PlayerSessionFactory) {
this.playerRunner = playerRunner;
this.playerSessionFactory = playerSessionFactory;
}
private assertIsInt(thing: any, nameOfField: string) {
assert(typeof thing === 'string', "Missing " + nameOfField);
let maybeInt = parseInt(thing, 10);
assert(!isNaN(maybeInt), "Missing " + nameOfField);
}
public start(reqBody: any): Q.Promise<I.StartResponse> {
return Q.fcall(() => {
assert(reqBody.numPlayers !== null, "reqBody is missing numPlayers");
this.assertIsInt(reqBody.numPlayers, "numPlayers");
assert(reqBody.duration !== null, "reqBody is missing duration");
this.assertIsInt(reqBody.duration, "duration");
assert(reqBody.rampDuration !== null, "reqBody is missing rampDuration");
this.assertIsInt(reqBody.rampDuration, "rampDuration");
let startRequest: I.StartRequest = {
numPlayers: parseInt(reqBody.numPlayers, 10),
duration: parseInt(reqBody.duration, 10),
rampDuration: parseInt(reqBody.rampDuration, 10)
};
logger.info("Starting Load Test with...");
logger.info(JSON.stringify(startRequest));
let loadTestRunPromises: Array<Q.Promise<any>> = new Array<Q.Promise<any>>();
for (let i: number = 0; i < settings.loadTestUris.length; i++) {
let deferred: Q.Deferred<any> = Q.defer<any>();
let uri: string = settings.loadTestUris[i] + ":" + settings.httpPort + "/run";
request.post({
uri: uri,
timeout: settings.timeoutForRequestPost,
json: true,
form: {
numPlayers: Math.round(startRequest.numPlayers / settings.loadTestUris.length),
duration: startRequest.duration,
rampDuration: startRequest.rampDuration
}
}, (error: any, response: any, body: any) => {
if (error) {
logger.error("Error in LoadTestOrchestrator on " + uri + "\n" + JSON.stringify(error));
deferred.reject(error);
return;
}
logger.info("Body of LoadTestOrchestrator response on " + uri + "\n" + JSON.stringify(body));
deferred.resolve(body);
});
loadTestRunPromises.push(deferred.promise);
}
return Q.all(loadTestRunPromises);
}).then((responses: any) => {
for (let i: number = 0; i < responses.length; i++) {
if (!responses[i].success) {
return { success: false };
}
}
return { success: true };
});
}
public stop(reqBody: any): Q.Promise<I.StopResponse> {
return Q.fcall(() => {
logger.info("Forcing a stop on Load Test");
let loadTestRunPromises: Array<Q.Promise<any>> = new Array<Q.Promise<any>>();
for (let i: number = 0; i < settings.loadTestUris.length; i++) {
let deferred: Q.Deferred<any> = Q.defer<any>();
let uri: string = settings.loadTestUris[i] + ":" + settings.httpPort + "/stopRun";
request.post({
uri: uri,
timeout: settings.timeoutForRequestPost,
}, (error: any, response: any, body: any) => {
if (error) {
logger.error("Error in LoadTestOrchestrator on " + uri + "\n" + JSON.stringify(error));
deferred.reject(error);
return;
}
logger.info("Body of LoadTestOrchestrator response on " + uri + "\n" + JSON.stringify(body));
deferred.resolve(body);
});
loadTestRunPromises.push(deferred.promise);
}
return Q.all(loadTestRunPromises);
}).then((responses: any) => {
for (let i: number = 0; i < responses.length; i++) {
if (!responses[i].success) {
return { success: false };
}
}
return { success: true };
});
}
public run(reqBody: any): Q.Promise<I.RunResponse> {
return Q.fcall(() => {
assert(reqBody.numPlayers !== null, "reqBody is missing numPlayers");
this.assertIsInt(reqBody.numPlayers, "numPlayers");
assert(reqBody.duration !== null, "reqBody is missing duration");
this.assertIsInt(reqBody.duration, "duration");
assert(reqBody.rampDuration !== null, "reqBody is missing rampDuration");
this.assertIsInt(reqBody.rampDuration, "rampDuration");
let runRequest: I.RunRequest = {
numPlayers: parseInt(reqBody.numPlayers, 10),
duration: parseInt(reqBody.duration, 10),
rampDuration: parseInt(reqBody.rampDuration, 10)
};
if (this.loadTestRunner) {
return { success: false };
}
logger.info("Running Load Test with...");
logger.info(JSON.stringify(runRequest));
let players: Array<I.Player> = this.generatePlayers(runRequest.numPlayers);
this.loadTestRunner = new LoadTestRunner(this, this.playerRunner, players, runRequest.duration, runRequest.rampDuration);
this.loadTestRunner.run();
return { success: true };
});
}
public stopRun(reqBody: any): Q.Promise<I.StopRunResponse> {
return Q.fcall(() => {
if (this.loadTestRunner == null) {
return { success: false };
}
logger.info("Stopping Load Test run with...");
this.loadTestRunner.stop();
return { success: true };
});
}
public runnerFinished(): void {
logger.info("Finished Load Test");
delete this.loadTestRunner;
}
public generatePlayers(playerNumber: number): Array<I.Player> {
let players: Array<I.Player> = new Array<I.Player>();
for (let i: number = 0; i < playerNumber; i++) {
let player: I.Player = this.generatePlayerFromIndex(i);
players.push(player);
}
return players;
}
public generatePlayerFromIndex(i: number): I.Player {
let randomNumber: number = Math.round(Math.random() * 1000000000);
let playerName: string = "Player" + randomNumber;
let password: string = "letmein" + randomNumber;
let email: string = "email" + randomNumber + "@example.com";
let isHost: boolean = i % 6 == 0;
return {
playerName: playerName,
uniquePlayerName: playerName.toUpperCase(),
password: password,
email: email,
accountCreated: false,
loopTimer: null,
shouldExit: false,
gamesPlayed: -69,
loopsStarted: 0,
loopsEndedSuccessfully: 0,
isHost: isHost,
session: this.playerSessionFactory.generateSession()
};
}
}
export = LoadTestOrchestrator;