forked from alexfernandez/loadtest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.js
159 lines (146 loc) · 4.87 KB
/
result.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
/**
* Result of a load test.
*/
export class Result {
constructor() {
this.url = null
this.cores = 0
this.maxRequests = 0
this.maxSeconds = 0
this.concurrency = 0
this.agent = null
this.requestsPerSecond = 0
this.startTimeMs = Number.MAX_SAFE_INTEGER
this.endTimeMs = 0
this.elapsedSeconds = 0
this.totalRequests = 0
this.totalErrors = 0
this.totalTimeSeconds = 0
this.accumulatedMs = 0
this.maxLatencyMs = 0
this.minLatencyMs = Number.MAX_SAFE_INTEGER
this.errorCodes = {}
this.histogramMs = {}
}
compute(options, latency) {
// configuration
this.url = options.url
this.cores = options.cores
this.maxRequests = parseInt(options.maxRequests)
this.maxSeconds = parseInt(options.maxSeconds)
this.concurrency = parseInt(options.concurrency)
this.agent = options.agentKeepAlive ? 'keepalive' : 'none';
this.requestsPerSecond = parseInt(options.requestsPerSecond)
// result
this.startTimeMs = Number(latency.startTimeNs / 1000000n)
this.endTimeMs = Number(latency.endTimeNs / 1000000n)
this.totalRequests = latency.totalRequests
this.totalErrors = latency.totalErrors
this.accumulatedMs = latency.totalTime
this.maxLatencyMs = latency.maxLatencyMs
this.minLatencyMs = latency.minLatencyMs
this.errorCodes = latency.errorCodes
this.histogramMs = latency.histogramMs
this.computeDerived()
}
computeDerived() {
this.elapsedSeconds = (this.endTimeMs - this.startTimeMs) / 1000
this.totalTimeSeconds = this.elapsedSeconds // backwards compatibility
const meanTime = this.accumulatedMs / this.totalRequests
this.meanLatencyMs = Math.round(meanTime * 10) / 10
this.effectiveRps = Math.round(this.totalRequests / this.elapsedSeconds)
this.rps = this.effectiveRps // backwards compatibility
this.computePercentiles()
}
computePercentiles() {
this.percentiles = {
50: false,
90: false,
95: false,
99: false
};
let counted = 0;
for (let ms = 0; ms <= this.maxLatencyMs; ms++) {
if (!this.histogramMs[ms]) {
continue;
}
counted += this.histogramMs[ms];
const percent = counted / this.totalRequests * 100;
Object.keys(this.percentiles).forEach(percentile => {
if (!this.percentiles[percentile] && percent > percentile) {
this.percentiles[percentile] = ms;
}
});
}
}
combine(result) {
// configuration
this.url = this.url || result.url
this.cores += 1
this.maxRequests += result.maxRequests
this.maxSeconds = this.maxSeconds || result.maxSeconds
this.concurrency = this.concurrency || result.concurrency
this.agent = this.agent || result.agent
this.requestsPerSecond += result.requestsPerSecond || 0
// result
this.startTimeMs = Math.min(this.startTimeMs, result.startTimeMs)
this.endTimeMs = Math.max(this.endTimeMs, result.endTimeMs)
this.totalRequests += result.totalRequests
this.totalErrors += result.totalErrors
this.accumulatedMs += result.accumulatedMs
this.maxLatencyMs = Math.max(this.maxLatencyMs, result.maxLatencyMs)
this.minLatencyMs = Math.min(this.minLatencyMs, result.minLatencyMs)
this.combineMap(this.errorCodes, result.errorCodes)
this.combineMap(this.histogramMs, result.histogramMs)
this.computeDerived()
}
combineMap(originalMap, addedMap) {
for (const key in {...originalMap, ...addedMap}) {
if (!originalMap[key]) {
originalMap[key] = 0
}
if (addedMap[key]) {
originalMap[key] += addedMap[key]
}
}
}
/**
* Show result of a load test.
*/
show() {
console.info('');
console.info('Target URL: %s', this.url);
if (this.maxRequests) {
console.info('Max requests: %s', this.maxRequests);
} else if (this.maxSeconds) {
console.info('Max time (s): %s', this.maxSeconds);
}
console.info('Concurrency level: %s', this.concurrency);
if (this.cores) {
console.info('Running on cores: %s', this.cores);
}
console.info('Agent: %s', this.agent);
if (this.requestsPerSecond) {
console.info('Target rps: %s', this.requestsPerSecond);
}
console.info('');
console.info('Completed requests: %s', this.totalRequests);
console.info('Total errors: %s', this.totalErrors);
console.info('Total time: %s s', this.elapsedSeconds);
console.info('Mean latency: %s ms', this.meanLatencyMs);
console.info('Effective rps: %s', this.effectiveRps);
console.info('');
console.info('Percentage of the requests served within a certain time');
Object.keys(this.percentiles).forEach(percentile => {
console.info(' %s% %s ms', percentile, this.percentiles[percentile]);
});
console.info(' 100% %s ms (longest request)', this.maxLatencyMs);
if (this.totalErrors) {
console.info('');
Object.keys(this.errorCodes).forEach(errorCode => {
const padding = ' '.repeat(errorCode.length < 4 ? 4 - errorCode.length : 1);
console.info(' %s%s: %s errors', padding, errorCode, this.errorCodes[errorCode]);
});
}
}
}