@artilleryio/int-core 2.0.4 → 2.0.6

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.
Files changed (2) hide show
  1. package/lib/phases.js +32 -14
  2. package/package.json +2 -2
package/lib/phases.js CHANGED
@@ -107,14 +107,20 @@ function createRamp(spec, ee) {
107
107
 
108
108
  const difference = rampTo - arrivalRate;
109
109
  const periods = duration;
110
- debug(`worker ${worker} totalWorkers ${totalWorkers} arrivalRate ${arrivalRate} rampTo ${rampTo} difference ${difference} periods ${periods}`);
110
+ debug(
111
+ `worker ${worker} totalWorkers ${totalWorkers} arrivalRate ${arrivalRate} rampTo ${rampTo} difference ${difference} periods ${periods}`
112
+ );
111
113
 
112
114
  const periodArrivals = [];
113
115
  const periodTick = [];
114
116
  // if there is only one peridod we generate mean arrivals
115
117
  if (periods === 1) {
116
- periodArrivals[0] = Math.floor((rampTo + arrivalRate) / 2);
117
- periodTick[0] = 1000 / periodArrivals;
118
+ const rawPeriodArrivals = (rampTo + arrivalRate) / 2;
119
+ periodArrivals[0] = adjustArrivalsByWorker(
120
+ rawPeriodArrivals,
121
+ totalWorkers,
122
+ worker
123
+ );
118
124
  } else {
119
125
  // for each period we calculate the corresponding arrivals:
120
126
  // knowing that arrivals(0) = arrivalRate and arrivals(duration -1) = rampTo
@@ -122,18 +128,15 @@ function createRamp(spec, ee) {
122
128
  for (let i = 0; i < periods; i++) {
123
129
  const rawPeriodArrivals = (difference / (duration - 1)) * i + arrivalRate;
124
130
 
125
- // We use the floor of the expected arrivals, then we add up all decimal digits
126
- // and evaluate if one or more workers should bump their arrivalRate.
127
- periodArrivals[i] = Math.floor(rawPeriodArrivals);
128
-
129
- // Think of fractionalPart * workers as the amount of arrivals that could not be
130
- // shared evenly across all workers.
131
- if (Math.round((rawPeriodArrivals % 1) * totalWorkers) >= worker) {
132
- periodArrivals[i] = periodArrivals[i] + 1;
133
- }
131
+ // take into account added decimals and bump worker arrivals if needed
132
+ periodArrivals[i] = adjustArrivalsByWorker(
133
+ rawPeriodArrivals,
134
+ totalWorkers,
135
+ worker
136
+ );
134
137
 
135
138
  // Needed ticks to get to periodArrivals in 1000ms
136
- periodTick[i] = periodArrivals[i] > 0 ? Math.floor(1000 / periodArrivals[i]) : 1000;
139
+ periodTick[i] = Math.min(Math.floor(1000 / periodArrivals[i]), 1000);
137
140
  }
138
141
  }
139
142
 
@@ -148,9 +151,24 @@ function createRamp(spec, ee) {
148
151
  }
149
152
 
150
153
  ee.emit('phaseCompleted', spec);
154
+ };
155
+
156
+ function adjustArrivalsByWorker(rawPeriodArrivals, totalWorkers, worker) {
157
+ // We use the floor of the expected arrivals, then we add up all decimal digits
158
+ // and evaluate if one or more workers should bump their arrivalRate.
159
+ let arrivals = Math.floor(rawPeriodArrivals);
160
+
161
+ // Think of fractionalPart * workers as the amount of arrivals that could not be
162
+ // shared evenly across all workers.
163
+ if (Math.round((rawPeriodArrivals % 1) * totalWorkers) >= worker) {
164
+ arrivals = arrivals + 1;
165
+ }
166
+ return arrivals;
151
167
  }
152
168
 
153
169
  function ticker(currentPeriod) {
170
+ // ensure we don't go past 1s
171
+ const delay = Math.min(periodTick[currentPeriod], 1000);
154
172
  let currentArrivals = 0;
155
173
  let arrivalTimer = driftless.setDriftlessInterval(function arrivals() {
156
174
  if (currentArrivals < periodArrivals[currentPeriod]) {
@@ -160,7 +178,7 @@ function createRamp(spec, ee) {
160
178
  currentPeriod++;
161
179
  driftless.clearDriftless(arrivalTimer);
162
180
  }
163
- }, periodTick[currentPeriod]);
181
+ }, delay);
164
182
  return;
165
183
  }
166
184
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@artilleryio/int-core",
3
- "version": "2.0.4",
3
+ "version": "2.0.6",
4
4
  "main": "./index.js",
5
5
  "dependencies": {
6
6
  "@artilleryio/int-commons": "latest",
@@ -46,6 +46,6 @@
46
46
  "nock": "^11.8.2",
47
47
  "rewiremock": "^3.14.3",
48
48
  "sinon": "^4.5.0",
49
- "tap": "15.3.2"
49
+ "tap": "15.2.3"
50
50
  }
51
51
  }