-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadTestRunner.ts
More file actions
124 lines (106 loc) · 3.9 KB
/
LoadTestRunner.ts
File metadata and controls
124 lines (106 loc) · 3.9 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
import Q = require('q');
import assert = require('assert');
import I = require('./Interfaces');
let logger = require('./logger');
import PlayerRunner = require('./PlayerRunner');
class LoadTestRunner {
private readonly playerRunner: PlayerRunner;
private readonly players: Array<I.Player>;
private readonly duration: number;
private readonly rampDuration: number;
private readonly runnerCleanup: I.RunnerCleanup;
private scheduleRampUpTimer: NodeJS.Timer;
private scheduleRampDownTimer: NodeJS.Timer;
private scheduleRunnerFinishedTimer: NodeJS.Timer;
private startTimers: Array<NodeJS.Timer> = new Array<NodeJS.Timer>();
private stopTimers: Array<NodeJS.Timer> = new Array<NodeJS.Timer>();
constructor(runnerCleanup: I.RunnerCleanup, playerRunner: PlayerRunner, players: Array<I.Player>, duration: number, rampDuration: number) {
this.runnerCleanup = runnerCleanup;
this.playerRunner = playerRunner;
this.players = players;
this.duration = duration;
this.rampDuration = rampDuration;
}
private assertIsInt(thing: any, nameOfField: string) {
assert(typeof thing === 'string', "Missing " + nameOfField);
let maybeInt = parseInt(thing, 10);
assert(!isNaN(maybeInt), "Missing " + nameOfField);
}
private scheduleRampUp(): void {
for (let i: number = 0; i < this.players.length; i++) {
let delayTime: number = i * (this.rampDuration / this.players.length);
let timer: NodeJS.Timer = setTimeout(() => {
this.playerRunner.start(this.players[i]);
}, delayTime);
this.startTimers.push(timer);
}
}
private scheduleRampDown(): void {
for (let i: number = 0; i < this.players.length; i++) {
let delayTime: number = i * (this.rampDuration / this.players.length);
let timer: NodeJS.Timer = setTimeout(() => {
this.playerRunner.stop(this.players[i]);
}, delayTime);
this.stopTimers.push(timer);
}
}
public run(): Q.Promise<I.RunResponse> {
return Q.fcall(() => {
this.scheduleRampUpTimer = setTimeout(() => {
this.scheduleRampUp();
}, 1000);
this.scheduleRampDownTimer = setTimeout(() => {
this.scheduleRampDown();
}, this.duration - this.rampDuration - 1000);
this.scheduleRunnerFinishedTimer = setTimeout(() => {
this.players.forEach((player) => {
let playerReport: I.PlayerReport = this.generatePlayerReport(player);
logger.info("PlayerReport", playerReport);
});
let loadTestReport: I.LoadTestReport = this.generateLoadTestReport();
logger.info("LoadTestReport", loadTestReport);
this.runnerCleanup.runnerFinished();
}, this.duration);
}).then(() => {
return { success: true };
});
}
public stop(): Q.Promise<I.StopResponse> {
return Q.fcall(() => {
clearTimeout(this.scheduleRampUpTimer);
clearTimeout(this.scheduleRampDownTimer);
clearTimeout(this.scheduleRunnerFinishedTimer);
for (let i: number = 0; i < this.startTimers.length; i++) {
clearTimeout(this.startTimers[i]);
}
for (let i: number = 0; i < this.stopTimers.length; i++) {
clearTimeout(this.stopTimers[i]);
}
setTimeout(() => {
this.scheduleRampDown();
}, 1000);
setTimeout(() => {
this.runnerCleanup.runnerFinished();
}, this.rampDuration + 1000);
}).then(() => {
return { success: true };
});
}
public generateLoadTestReport(): I.LoadTestReport {
let loopsEndedSuccessfully: number = this.players.reduce((total, currentPlayer) => total + currentPlayer.loopsEndedSuccessfully, 0);
let loopsStarted: number = this.players.reduce((total, currentPlayer) => total + currentPlayer.loopsStarted, 0);
return {
loopsEndedSuccessfully: loopsEndedSuccessfully,
loopsStarted: loopsStarted
};
}
public generatePlayerReport(player: I.Player): I.PlayerReport {
return {
isHost: player.isHost ? "true" : "false",
loopsEndedSuccessfully: player.loopsEndedSuccessfully,
loopsStarted: player.loopsStarted,
playerName: player.playerName
};
}
}
export = LoadTestRunner;