-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathbench.js
152 lines (137 loc) · 3.57 KB
/
bench.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright 2020 The NATS Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Bench, connect, Metric } from "../esm/nats.js";
function getString(id) {
return document.getElementById(id).value;
}
function isChecked(id) {
return document.getElementById(id).checked;
}
function getTestChoice() {
if (isChecked("pubsub")) {
return "pubsub";
} else if (isChecked("pub")) {
return "pub";
} else if (isChecked("sub")) {
return "sub";
} else if (isChecked("reqrep")) {
return "reqrep";
}
}
function getNumber(id) {
const v = getString(id);
if (!isNaN(v)) {
return parseInt(v, 10);
}
return -1;
}
function updateResults(s) {
const p = document.createElement("pre");
p.appendChild(document.createTextNode(s));
document.getElementById("results").appendChild(p);
}
async function run() {
const server = getString("server");
const ws = isChecked("ws");
const nc = await connect(
{
servers: `${ws ? "ws://" : "wss://"}${server}`,
pendingLimit: 8192,
},
);
nc.closed()
.then((err) => {
if (err) {
console.error(err);
}
});
const kind = getTestChoice();
const t = {};
t.callbacks = isChecked("callbacks");
t.msgs = getNumber("count");
t.size = getNumber("payload");
t.subject = getString("subject");
t.pub = kind === "pub" || kind === "pubsub";
t.sub = kind === "sub" || kind === "pubsub";
t.req = kind === "reqrep";
t.rep = kind === "reqrep";
const bench = new Bench(nc, t);
const m = await bench.run();
const metrics = [];
metrics.push(...m);
await nc.close();
const pubsub = metrics.filter((m) => m.name === "pubsub").reduce(
reducer,
new Metric("pubsub", 0),
);
const reqrep = metrics.filter((m) => m.name === "reqrep").reduce(
reducer,
new Metric("reqrep", 0),
);
const pub = metrics.filter((m) => m.name === "pub").reduce(
reducer,
new Metric("pub", 0),
);
const sub = metrics.filter((m) => m.name === "sub").reduce(
reducer,
new Metric("sub", 0),
);
const req = metrics.filter((m) => m.name === "req").reduce(
reducer,
new Metric("req", 0),
);
const rep = metrics.filter((m) => m.name === "rep").reduce(
reducer,
new Metric("rep", 0),
);
const report = [];
if (pubsub && pubsub.msgs) {
report.push(pubsub.toString());
}
if (reqrep && reqrep.msgs) {
report.push(reqrep.toString());
}
if (pub && pub.msgs) {
report.push(pub.toString());
}
if (sub && sub.msgs) {
report.push(sub.toString());
}
if (req && req.msgs) {
report.push(req.toString());
}
if (rep && rep.msgs) {
report.push(rep.toString());
}
updateResults(report.join("\n"));
}
const reducer = (a, m) => {
if (a) {
a.name = m.name;
a.payload = m.payload;
a.bytes += m.bytes;
a.duration += m.duration;
a.msgs += m.msgs;
a.lang = m.lang;
a.version = m.version;
a.async = m.async;
a.max = Math.max(a.max === undefined ? 0 : a.max, m.duration);
a.min = Math.min(a.min === undefined ? m.duration : a.max, m.duration);
}
return a;
};
window.benchapp = {
run: run,
};