@artilleryio/int-core 1.0.0

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.
@@ -0,0 +1,88 @@
1
+ /* This Source Code Form is subject to the terms of the Mozilla Public
2
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
+
5
+ 'use strict';
6
+
7
+ const l = require('lodash');
8
+
9
+ module.exports = create;
10
+
11
+ // naive implementation of selection with replacement
12
+ function create(list) {
13
+ let dist = l.reduce(
14
+ list,
15
+ function (acc, el, i) {
16
+ for (let j = 0; j < el.weight * 100; j++) {
17
+ acc.push(i);
18
+ }
19
+ return acc;
20
+ },
21
+ []
22
+ );
23
+
24
+ return function () {
25
+ let i = dist[l.random(0, dist.length - 1)];
26
+ return [i, list[i]];
27
+ };
28
+ }
29
+
30
+ function bench() {
31
+ const items = [
32
+ { weight: 0, value: 'zero' },
33
+ { weight: 1, value: 'a' },
34
+ { weight: 2, value: 'b' },
35
+ { weight: 3, value: 'c' },
36
+ { weight: 4, value: 'd' },
37
+ { weight: 5, value: 'e' },
38
+ { weight: 1, value: 'f' },
39
+ { weight: 2, value: 'g' },
40
+ { weight: 3, value: 'h' },
41
+ { weight: 4, value: 'i' },
42
+ { weight: 5, value: 'j' },
43
+ { weight: 10, value: 'k' }
44
+ ];
45
+
46
+ const picker = create(items);
47
+
48
+ const ITERS = Math.pow(10, 6);
49
+ const startedAt = Date.now();
50
+
51
+ const picks = l.map(l.range(ITERS), function () {
52
+ let x = picker()[1];
53
+ return x;
54
+ });
55
+
56
+ const delta = Date.now() - startedAt;
57
+ console.log(
58
+ 'ITERS = %s\ndelta = %s\nITERS/delta = %s',
59
+ ITERS,
60
+ delta,
61
+ Math.round(ITERS / delta)
62
+ );
63
+
64
+ const sumWeights = l.reduce(
65
+ items,
66
+ function (acc, item) {
67
+ return acc + item.weight;
68
+ },
69
+ 0
70
+ );
71
+
72
+ console.log('sumWeights = %s', sumWeights);
73
+
74
+ l.each(items, function (p) {
75
+ let count = l.filter(picks, { value: p.value }).length;
76
+ console.log(
77
+ 'Count of %s = %s (should be: %s%, is: %s%)',
78
+ p.value,
79
+ count,
80
+ Math.round((p.weight / sumWeights) * 1000) / 1000,
81
+ Math.round((count / ITERS) * 1000) / 1000
82
+ );
83
+ });
84
+ }
85
+
86
+ if (require.main === module) {
87
+ bench();
88
+ }
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@artilleryio/int-core",
3
+ "version": "1.0.0",
4
+ "main": "./index.js",
5
+ "dependencies": {
6
+ "@artilleryio/sketches-js": "^1.0.4",
7
+ "@hapi/basic": "^6.0.0",
8
+ "agentkeepalive": "^4.1.0",
9
+ "arrivals": "^2.1.2",
10
+ "async": "^2.6.4",
11
+ "chalk": "^2.4.2",
12
+ "cheerio": "^1.0.0-rc.10",
13
+ "commons": "*",
14
+ "cookie-parser": "^1.4.3",
15
+ "csv-parse": "^4.16.3",
16
+ "debug": "^4.3.1",
17
+ "decompress-response": "^6.0.0",
18
+ "deep-for-each": "^3.0.0",
19
+ "driftless": "^2.0.3",
20
+ "esprima": "^4.0.0",
21
+ "eventemitter3": "^4.0.4",
22
+ "express": "^4.16.3",
23
+ "fast-deep-equal": "^3.1.3",
24
+ "filtrex": "^0.5.4",
25
+ "form-data": "^3.0.0",
26
+ "got": "^11.8.5",
27
+ "hpagent": "^0.1.1",
28
+ "https-proxy-agent": "^5.0.0",
29
+ "lodash": "^4.17.19",
30
+ "nock": "^11.8.2",
31
+ "proxy": "^1.0.2",
32
+ "socket.io": "^3.1.2",
33
+ "socket.io-client": "^4.5.1",
34
+ "socketio-wildcard": "^2.0.0",
35
+ "tap": "^15.2.3",
36
+ "tough-cookie": "^4.0.0",
37
+ "try-require": "^1.2.1",
38
+ "uuid": "^8.0.0",
39
+ "ws": "^7.5.7"
40
+ },
41
+ "scripts": {
42
+ "test": "tap --no-coverage --timeout=300 test/core/unit/*.test.js && bash test/core/run.sh"
43
+ },
44
+ "devDependencies": {
45
+ "@hapi/hapi": "^20.1.3",
46
+ "rewiremock": "^3.14.3",
47
+ "sinon": "^4.5.0"
48
+ }
49
+ }