-
Notifications
You must be signed in to change notification settings - Fork 0
/
performance_test1.js
101 lines (83 loc) · 2.7 KB
/
performance_test1.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
// TEST REST ENDPOINT, where it's service is hosted in GCP Cloud Run
const { GoogleAuth } = require('google-auth-library');
const SuperLoop = require('super-loop');
const { Readable } = require('node:stream');
const stats = require('measured-core').createCollection();
const histogram = stats.histogram('latency');
const BASE_URL = process.env.API_BASE_URL;
// decode private key if stored in base64 form
const SA_KEY = JSON.parse(
Buffer.from(process.env.GCP_IAM_KEY, 'base64').toString(
'ascii',
),
);
const auth = new GoogleAuth({
credentials: SA_KEY
});
let client = null;
async function invoke(url, body) {
// client can be reused
if (client == null) {
client = await auth.getIdTokenClient(process.env.TARGET_AUDIENCE); // set baseUrl as targetAudience
}
return client.request({
method: 'POST',
url,
data: {
...body,
},
});
}
let startIndex = 99900000000;
async function send_message(offset) {
const testTarget = `${BASE_URL}/send_message`;
const index = startIndex + offset;
const txId = `pt-tx-${index}`;
let userId = `${index}`;
if (index % 2000 == 0) { // interleaving some special data in the feed
userId = '8888888'
}
try {
const result = await invoke(testTarget, {
"transactionId": txId,
"userId": userId,
"topic": "MESSAGE_TYPE_1",
"title": "G'day mate!",
"body": "This is a good day!",
"eventTimestamp": "2022-02-10T05:35:31.165Z",
"correlationId": "12333abc",
"pt": true
});
console.log(JSON.stringify(result.data));
} catch (err) {
if (err?.response?.data?.code >= 400 || err?.response?.data?.code < 500) {
console.log(JSON.stringify(err?.response?.data));
} else {
console.error('failed to send message', err);
}
}
}
// pumping requests using super-loop
// measurement is done using measured-core
(async function () {
const intv = setInterval(function () {
console.log(JSON.stringify(stats, null, 2));
//console.log(histogram.toJSON());
}, 1000);
const sloop = new SuperLoop();
sloop.on('warn', (e) => {
console.warn(e);
});
await sloop.pipeFrom(Readable.from(Array.from(Array(1_000_000).keys())))
.rate(75)
.concurrency(200)
.invoke(async function (data) {
stats.meter('throughput').mark();
const ts_from = Date.now();
await send_message(data);
const latency = Date.now() - ts_from;
histogram.update(latency);
})
.exec();
clearInterval(intv);
}()).catch(console.error);