keuss 1.7.0 → 1.7.1
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/README.md +1 -0
- package/package.json +1 -1
- package/playground/stream-loops.js +114 -0
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/*
|
|
2
|
+
*
|
|
3
|
+
* very simple example of stream-mongo: one element pushed, consumed three times
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const async = require ('async');
|
|
8
|
+
const _ = require ('lodash');
|
|
9
|
+
const MQ = require ('../backends/stream-mongo');
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
const group_cardinality = 3;
|
|
13
|
+
const mesgs = 1000000;
|
|
14
|
+
|
|
15
|
+
const stats = {
|
|
16
|
+
consumer: {
|
|
17
|
+
last: 0,
|
|
18
|
+
total: 0
|
|
19
|
+
},
|
|
20
|
+
producer: {
|
|
21
|
+
last: 0,
|
|
22
|
+
total: 0
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function do_times (cnt, fn, done) {
|
|
27
|
+
if (!cnt) {
|
|
28
|
+
console.log ('loop done')
|
|
29
|
+
return done(null, 0);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
fn (cnt, err => {
|
|
33
|
+
if (err) return done (err, cnt);
|
|
34
|
+
do_times (cnt-1, fn, done);
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
function payload (n) {
|
|
40
|
+
return {elem: n, headline: 'something something', tags: {a: n, b: 2*n}}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
function headers (n) {
|
|
45
|
+
return {cnt: n, h1: 'something something', h2: false}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
function producer_looper (q, id) {
|
|
50
|
+
console.log (`creating producer looper ${id}`);
|
|
51
|
+
return (n, cb) => {
|
|
52
|
+
q.push (payload (n), {hdrs: headers(n)}, err => {
|
|
53
|
+
// console.log ('[%s] push #%d', id, n);
|
|
54
|
+
stats.producer.total++;
|
|
55
|
+
cb (err);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
function consumer_looper (q, id) {
|
|
62
|
+
console.log (`creating consumer looper ${id}`);
|
|
63
|
+
return (n, cb) => {
|
|
64
|
+
q.pop (id, err => {
|
|
65
|
+
// console.log ('[%s] pop #%d', id, n);
|
|
66
|
+
stats.consumer.total++
|
|
67
|
+
cb (err);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
// initialize factory
|
|
74
|
+
MQ ({
|
|
75
|
+
url: 'mongodb://localhost/keuss_test_stream'
|
|
76
|
+
}, (err, factory) => {
|
|
77
|
+
if (err) return console.error(err);
|
|
78
|
+
|
|
79
|
+
const groups = _.range (1, group_cardinality + 1).map (i => `G${i}`).join (',');
|
|
80
|
+
console.log('groups: ', groups);
|
|
81
|
+
|
|
82
|
+
// create queues and clients
|
|
83
|
+
const queues = {};
|
|
84
|
+
queues['qp'] = factory.queue ('test_stream', {groups});
|
|
85
|
+
|
|
86
|
+
for (let i = 1; i <= group_cardinality; i++) {
|
|
87
|
+
queues[`qc${i}`] = factory.queue ('test_stream', {group: `G${i}`});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
console.log (`created queues (one producer, ${group_cardinality} consumers)`);
|
|
91
|
+
|
|
92
|
+
// create tasks
|
|
93
|
+
const tasks = [];
|
|
94
|
+
tasks.push (cb => do_times (mesgs, producer_looper (queues['qp'], 'p0'), cb));
|
|
95
|
+
|
|
96
|
+
for (let t = 1; t <= group_cardinality; t++) {
|
|
97
|
+
tasks.push (cb => do_times (mesgs, consumer_looper (queues[`qc${t}`], `c${t}`), cb),)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
console.log (`created tasks (one producer, ${group_cardinality} consumers)`);
|
|
101
|
+
|
|
102
|
+
async.parallel (tasks, err => {
|
|
103
|
+
factory.close ();
|
|
104
|
+
if (err) console.error (err);
|
|
105
|
+
console.log ('done');
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
setInterval (() => {
|
|
109
|
+
console.log (`produced ${stats.producer.total - stats.producer.last} msg/s, consumed ${stats.consumer.total - stats.consumer.last} msg/s`);
|
|
110
|
+
stats.producer.last = stats.producer.total;
|
|
111
|
+
stats.consumer.last = stats.consumer.total
|
|
112
|
+
}, 1000);
|
|
113
|
+
});
|
|
114
|
+
|