@loadmill/executer 0.1.52 → 0.1.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/sampler.ts DELETED
@@ -1,133 +0,0 @@
1
- import log from '@loadmill/universal/dist/log';
2
- import * as mathUtils from '@loadmill/universal/dist/math-utils';
3
- import * as promiseUtils from '@loadmill/universal/dist/promise-utils';
4
- import { minimalRemainingDuration, reportDelay } from '@loadmill/core/dist/conf';
5
- import { runSingleIteration } from './single-runner';
6
- import { Failures, mergeFailures } from './failures';
7
- import { PerRequestStats, setReqStats } from './request-stats';
8
- import { Work } from './work';
9
- import { messageCreators } from './message-creators';
10
- import { DEFAULT_DURATION, DEFAULT_ITERATION_DELAY } from '@loadmill/core/dist/conf/defaults';
11
-
12
- // 5 minutes:
13
- const RES_KEEP_TIME = 5 * 60 * 1000;
14
-
15
- export interface SamplerFaja {
16
- cancelWork(finished: boolean);
17
- send(obj: string);
18
- reportIntervalId;
19
- isStopped: boolean;
20
- keeper;
21
- }
22
-
23
- export class Sampler {
24
- avgResTime = 0;
25
- expired = false;
26
- successfulHits = 0;
27
- failedIterations = 0;
28
- startedIterations = 0;
29
- failures: Failures = {};
30
- successfulIterations = 0;
31
- requestStats: PerRequestStats = {};
32
-
33
- constructor(private samplerMill: SamplerFaja, private work: Work) {}
34
-
35
- startSampling = () => {
36
- this.samplerMill.cancelWork(false);
37
-
38
- promiseUtils.delay(this.work.duration || DEFAULT_DURATION).then(
39
- () => {
40
- if (!this.samplerMill.isStopped) {
41
- log.debug('Time\'s up!');
42
- this.expired = true;
43
- this.sendReport();
44
- }
45
- }
46
- );
47
-
48
- const requests = this.work.requests;
49
- const iterationDelay = this.work.iterationDelay || DEFAULT_ITERATION_DELAY;
50
-
51
- const reportDelayy = reportDelay(iterationDelay, requests);
52
- this.samplerMill.reportIntervalId = setInterval(
53
- this.sendReport,
54
- reportDelayy
55
- );
56
-
57
- this.samplerMill.isStopped = false;
58
- this.runAll();
59
- };
60
-
61
- runAll = async () => {
62
- while (!this.isDone()) {
63
- ++this.startedIterations;
64
- log.trace('Executing iteration: ', this.startedIterations);
65
-
66
- const res = await runSingleIteration(this.work);
67
- this.samplerMill.keeper.keepIfNeeded(res, RES_KEEP_TIME);
68
-
69
- const resFailures = res.failures;
70
-
71
- if (!resFailures || Object.keys(resFailures).length === 0) {
72
- ++this.successfulIterations;
73
- } else {
74
- ++this.failedIterations;
75
- mergeFailures(this.failures, resFailures);
76
- }
77
-
78
- this.avgResTime = mathUtils.calcAvg(
79
- this.avgResTime,
80
- this.successfulHits,
81
- res.avgResTime,
82
- res.successfulHits
83
- );
84
- this.successfulHits += res.successfulHits;
85
-
86
- Object.keys(res.requestStats).forEach(index => {
87
- setReqStats(this.requestStats, index, ...res.requestStats[index].resTimes);
88
- });
89
-
90
-
91
- if (!this.isDone()) {
92
- // We count only the delay of the requests that were NOT ATTEMPTED.
93
- // Note that the first request delay is always ignored:
94
- const skipCount = res.lastStartedIndex + 1;
95
- await promiseUtils.delay(
96
- minimalRemainingDuration(
97
- this.work.iterationDelay || DEFAULT_ITERATION_DELAY,
98
- this.work.requests,
99
- skipCount
100
- )
101
- );
102
- }
103
- }
104
-
105
- if (!this.samplerMill.isStopped) {
106
- log.debug('Finished last iteration:', this.startedIterations);
107
- this.sendReport();
108
- this.samplerMill.cancelWork(true);
109
- }
110
- };
111
-
112
- isDone = () => {
113
- return this.samplerMill.isStopped || this.expired || this.startedIterations >= this.work.iterations;
114
- };
115
-
116
- sendReport = () => {
117
- const report = {
118
- failures: this.failures,
119
- hits: this.successfulHits,
120
- avgResTime: this.avgResTime,
121
- requestStats: this.requestStats,
122
- failedIterations: this.failedIterations,
123
- successfulIterations: this.successfulIterations,
124
- };
125
-
126
- log.trace('Sending report:', report);
127
- this.samplerMill.send(messageCreators.report(report));
128
-
129
- this.failures = {};
130
- this.requestStats = {};
131
- this.successfulIterations = this.successfulHits = this.failedIterations = this.avgResTime = 0;
132
- };
133
- }