@artilleryio/int-core 2.0.0 → 2.0.2

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.
@@ -8,6 +8,7 @@ const async = require('async');
8
8
  const _ = require('lodash');
9
9
 
10
10
  const io = require('socket.io-client');
11
+ const wildcardPatch = require('socketio-wildcard')(io.Manager);
11
12
 
12
13
  const deepEqual = require('fast-deep-equal');
13
14
  const debug = require('debug')('socketio');
@@ -302,7 +303,9 @@ SocketIoEngine.prototype.loadContextSocket = function (namespace, context, cb) {
302
303
  const socket = io(target, options);
303
304
  context.sockets[namespace] = socket;
304
305
 
305
- socket.onAny(() => {
306
+ wildcardPatch(socket);
307
+
308
+ socket.on('*', function () {
306
309
  context.__receivedMessageCount++;
307
310
  });
308
311
 
package/lib/phases.js CHANGED
@@ -15,6 +15,12 @@ const driftless = require('driftless');
15
15
 
16
16
  module.exports = phaser;
17
17
 
18
+ async function sleep(ms) {
19
+ return new Promise((resolve) => {
20
+ setTimeout(resolve, ms);
21
+ });
22
+ }
23
+
18
24
  function phaser(phaseSpecs) {
19
25
  let ee = new EventEmitter();
20
26
 
@@ -96,50 +102,67 @@ function createRamp(spec, ee) {
96
102
  const duration = spec.duration || 1;
97
103
  const arrivalRate = spec.arrivalRate;
98
104
  const rampTo = spec.rampTo;
105
+ const worker = spec.worker;
106
+ const totalWorkers = spec.totalWorkers;
99
107
 
100
- // smallest tick we can get away with. Both arrivalRate and rampTo
101
- // can be zero. So in that case we use 1s ticks even tho no
102
- // arrivals will be generated
103
- let tick = 1000 / Math.max(Math.max(arrivalRate, rampTo), 1);
104
108
  const difference = rampTo - arrivalRate;
105
- const periods = duration * 1000 / tick;
109
+ const periods = duration;
110
+ debug(`worker ${worker} totalWorkers ${totalWorkers} arrivalRate ${arrivalRate} rampTo ${rampTo} difference ${difference} periods ${periods}`);
111
+
112
+ const periodArrivals = [];
113
+ const periodTick = [];
114
+ // if there is only one peridod we generate mean arrivals
115
+ if (periods === 1) {
116
+ periodArrivals[0] = Math.floor((rampTo + arrivalRate) / 2);
117
+ periodTick[0] = 1000 / periodArrivals;
118
+ } else {
119
+ // for each period we calculate the corresponding arrivals:
120
+ // knowing that arrivals(0) = arrivalRate and arrivals(duration -1) = rampTo
121
+ // then: arrivals(t) = difference / (duration-1) * t + arrivalRate
122
+ for (let i = 0; i < periods; i++) {
123
+ const rawPeriodArrivals = (difference / (duration - 1)) * i + arrivalRate;
124
+
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
+ }
106
134
 
107
- function arrivalProbability(currentStep) {
108
- // linear function ax + b
109
- // normalized to 0 <= f(t) <= 1
110
- // Anything under function value should be an arrival
135
+ // Needed ticks to get to periodArrivals in 1000ms
136
+ periodTick[i] = periodArrivals[i] > 0 ? Math.floor(1000 / periodArrivals[i]) : 1000;
137
+ }
138
+ }
111
139
 
112
- let t = currentStep * tick / 1000;
113
- return ((difference / duration) * t + arrivalRate) / Math.max(rampTo, arrivalRate) || 0;
114
- };
140
+ debug(`periodArrivals ${periodArrivals}`);
141
+ debug(`periodTick ${periodTick}`);
115
142
 
116
- let probabilities = Array.from({length: periods}, () => Math.random());
143
+ return async function rampTask(callback) {
144
+ ee.emit('phaseStarted', spec);
145
+ for (let period = 0; period < periods; period++) {
146
+ ticker(period);
147
+ await sleep(1000);
148
+ }
117
149
 
118
- debug(
119
- `rampTo: tick = ${tick}; difference = ${difference}; rampTo = ${rampTo}; arrivalRate = ${arrivalRate}; periods = ${periods}`
120
- );
150
+ ee.emit('phaseCompleted', spec);
151
+ }
121
152
 
122
- return function rampTask(callback) {
123
- ee.emit('phaseStarted', spec);
124
- let currentStep = 1;
125
- const timer = driftless.setDriftlessInterval(function maybeArrival() {
126
- if (currentStep <= periods) {
127
- let arrivalBreakpoint = arrivalProbability(currentStep);
128
- let roll = probabilities[currentStep-1];
129
- debug(`roll:${roll} <= breakpoint:${arrivalBreakpoint}`);
130
- if (roll <= arrivalBreakpoint) {
131
- ee.emit('arrival', spec);
132
- }
133
-
134
- currentStep++;
153
+ function ticker(currentPeriod) {
154
+ let currentArrivals = 0;
155
+ let arrivalTimer = driftless.setDriftlessInterval(function arrivals() {
156
+ if (currentArrivals < periodArrivals[currentPeriod]) {
157
+ ee.emit('arrival', spec);
158
+ currentArrivals++;
135
159
  } else {
136
- driftless.clearDriftless(timer);
137
- ee.emit('phaseCompleted', spec);
138
-
139
- return callback(null);
160
+ currentPeriod++;
161
+ driftless.clearDriftless(arrivalTimer);
140
162
  }
141
- }, tick);
142
- };
163
+ }, periodTick[currentPeriod]);
164
+ return;
165
+ }
143
166
  }
144
167
 
145
168
  function createArrivalCount(spec, ee) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@artilleryio/int-core",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "main": "./index.js",
5
5
  "dependencies": {
6
6
  "@artilleryio/sketches-js": "^1.0.4",
@@ -39,6 +39,8 @@
39
39
  "ws": "^7.5.7"
40
40
  },
41
41
  "scripts": {
42
+ "lint": "eslint --ext \".js,.ts,.tsx\" .",
43
+ "lint-fix": "npm run lint -- --fix",
42
44
  "test": "tap --no-coverage --timeout=300 test/core/unit/*.test.js && bash test/core/run.sh"
43
45
  },
44
46
  "devDependencies": {