forked from openpgpjs/openpgpjs
/
memory_usage.js
251 lines (210 loc) · 10.7 KB
/
memory_usage.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* eslint-disable no-console */
const assert = require('assert');
const stream = require('@openpgp/web-stream-tools');
const path = require('path');
const { writeFileSync, unlinkSync } = require('fs');
const { fork } = require('child_process');
const openpgp = require('../..');
/**
* Benchmark max memory usage recorded during execution of the given function.
* This spawns a new v8 instance and runs the code there in isolation, to avoid interference between tests.
* @param {Funtion} function to benchmark (can be async)
* @returns {NodeJS.MemoryUsage} memory usage snapshot with max RSS (sizes in bytes)
*/
const benchmark = async function(fn) {
const tmpFileName = path.join(__dirname, 'tmp.js');
// the code to execute must be written to a file
writeFileSync(tmpFileName, `
const assert = require('assert');
const stream = require('@openpgp/web-stream-tools');
const openpgp = require('../..');
let maxMemoryComsumption;
let activeSampling = false;
function sampleOnce() {
const memUsage = process.memoryUsage();
if (!maxMemoryComsumption || memUsage.rss > maxMemoryComsumption.rss) {
maxMemoryComsumption = memUsage;
}
}
function samplePeriodically() {
setImmediate(() => {
sampleOnce();
activeSampling && samplePeriodically();
});
}
// main body
(async () => {
maxMemoryComsumption = null;
activeSampling = true;
samplePeriodically();
await (${fn.toString()})();
// setImmediate is run at the end of the event loop, so we need to manually collect the latest sample
sampleOnce();
process.send(maxMemoryComsumption);
process.exit(); // child process doesn't exit otherwise
})();
`);
const maxMemoryComsumption = await new Promise((resolve, reject) => {
const child = fork(tmpFileName);
child.on('message', function (message) {
resolve(message);
});
child.on('error', function (err) {
reject(err);
});
});
unlinkSync(tmpFileName);
return maxMemoryComsumption;
};
const onError = err => {
console.error('The memory benchmark tests failed by throwing the following error:');
console.error(err);
// eslint-disable-next-line no-process-exit
process.exit(1);
};
class MemoryBenchamrkSuite {
constructor() {
this.tests = [];
}
add(name, fn) {
this.tests.push({ name, fn });
}
async run() {
const stats = [];
for (const { name, fn } of this.tests) {
const memoryUsage = await benchmark(fn).catch(onError);
// convert values to MB
Object.entries(memoryUsage).forEach(([name, value]) => {
memoryUsage[name] = (value / 1024 / 1024).toFixed(2);
});
const { rss, ...usageDetails } = memoryUsage;
// raw entry format accepted by github-action-pull-request-benchmark
stats.push({
name,
value: rss,
range: Object.entries(usageDetails).map(([name, value]) => `${name}: ${value}`).join(', '),
unit: 'MB',
biggerIsBetter: false
});
}
return stats;
}
}
/**
* Memory usage tests.
* All the necessary variables must be declared inside the test function.
*/
(async () => {
const suite = new MemoryBenchamrkSuite();
suite.add('empty test (baseline)', () => {});
suite.add('openpgp.encrypt/decrypt (CFB, binary)', async () => {
const passwords = 'password';
const config = { aeadProtect: false, preferredCompressionAlgorithm: openpgp.enums.compression.uncompressed };
const plaintextMessage = await openpgp.createMessage({ binary: new Uint8Array(1000000).fill(1) });
const armoredEncryptedMessage = await openpgp.encrypt({ message: plaintextMessage, passwords, config });
const encryptedMessage = await openpgp.readMessage({ armoredMessage: armoredEncryptedMessage });
assert.ok(encryptedMessage.packets[1] instanceof openpgp.SymEncryptedIntegrityProtectedDataPacket);
await openpgp.decrypt({ message: encryptedMessage, passwords, config });
});
suite.add('openpgp.encrypt/decrypt (CFB, text)', async () => {
const passwords = 'password';
const config = { aeadProtect: false, preferredCompressionAlgorithm: openpgp.enums.compression.uncompressed };
const plaintextMessage = await openpgp.createMessage({ text: 'a'.repeat(10000000 / 2) }); // two bytes per character
const armoredEncryptedMessage = await openpgp.encrypt({ message: plaintextMessage, passwords, config });
const encryptedMessage = await openpgp.readMessage({ armoredMessage: armoredEncryptedMessage });
assert.ok(encryptedMessage.packets[1] instanceof openpgp.SymEncryptedIntegrityProtectedDataPacket);
await openpgp.decrypt({ message: encryptedMessage, passwords, config });
});
suite.add('openpgp.encrypt/decrypt (AEAD, binary)', async () => {
const passwords = 'password';
const config = { aeadProtect: true, preferredCompressionAlgorithm: openpgp.enums.compression.uncompressed };
const plaintextMessage = await openpgp.createMessage({ binary: new Uint8Array(1000000).fill(1) });
const armoredEncryptedMessage = await openpgp.encrypt({ message: plaintextMessage, passwords, config });
const encryptedMessage = await openpgp.readMessage({ armoredMessage: armoredEncryptedMessage });
assert.ok(encryptedMessage.packets[1] instanceof openpgp.AEADEncryptedDataPacket);
await openpgp.decrypt({ message: encryptedMessage, passwords, config });
});
suite.add('openpgp.encrypt/decrypt (AEAD, text)', async () => {
const passwords = 'password';
const config = { aeadProtect: true, preferredCompressionAlgorithm: openpgp.enums.compression.uncompressed };
const plaintextMessage = await openpgp.createMessage({ text: 'a'.repeat(10000000 / 2) }); // two bytes per character
const armoredEncryptedMessage = await openpgp.encrypt({ message: plaintextMessage, passwords, config });
const encryptedMessage = await openpgp.readMessage({ armoredMessage: armoredEncryptedMessage });
assert.ok(encryptedMessage.packets[1] instanceof openpgp.AEADEncryptedDataPacket);
await openpgp.decrypt({ message: encryptedMessage, passwords, config });
});
// streaming tests
suite.add('openpgp.encrypt/decrypt (CFB, binary, with streaming)', async () => {
await stream.loadStreamsPonyfill();
const passwords = 'password';
const config = { aeadProtect: false, preferredCompressionAlgorithm: openpgp.enums.compression.uncompressed };
const plaintextMessage = await openpgp.createMessage({ binary: stream.toStream(new Uint8Array(1000000).fill(1)) });
assert(plaintextMessage.fromStream);
const armoredEncryptedMessage = await openpgp.encrypt({ message: plaintextMessage, passwords, config });
const encryptedMessage = await openpgp.readMessage({ armoredMessage: armoredEncryptedMessage });
assert.ok(encryptedMessage.packets[1] instanceof openpgp.SymEncryptedIntegrityProtectedDataPacket);
const { data: decryptedData } = await openpgp.decrypt({ message: encryptedMessage, passwords, config });
await stream.readToEnd(decryptedData);
});
suite.add('openpgp.encrypt/decrypt (CFB, text, with streaming)', async () => {
await stream.loadStreamsPonyfill();
const passwords = 'password';
const config = { aeadProtect: false, preferredCompressionAlgorithm: openpgp.enums.compression.uncompressed };
const plaintextMessage = await openpgp.createMessage({ text: stream.toStream('a'.repeat(10000000 / 2)) });
assert(plaintextMessage.fromStream);
const armoredEncryptedMessage = await openpgp.encrypt({ message: plaintextMessage, passwords, config });
const encryptedMessage = await openpgp.readMessage({ armoredMessage: armoredEncryptedMessage });
assert.ok(encryptedMessage.packets[1] instanceof openpgp.SymEncryptedIntegrityProtectedDataPacket);
const { data: decryptedData } = await openpgp.decrypt({ message: encryptedMessage, passwords, config });
await stream.readToEnd(decryptedData);
});
suite.add('openpgp.encrypt/decrypt (AEAD, binary, with streaming)', async () => {
await stream.loadStreamsPonyfill();
const passwords = 'password';
const config = { aeadProtect: true, preferredCompressionAlgorithm: openpgp.enums.compression.uncompressed };
const plaintextMessage = await openpgp.createMessage({ binary: stream.toStream(new Uint8Array(1000000).fill(1)) });
assert(plaintextMessage.fromStream);
const armoredEncryptedMessage = await openpgp.encrypt({ message: plaintextMessage, passwords, config });
const encryptedMessage = await openpgp.readMessage({ armoredMessage: armoredEncryptedMessage });
assert.ok(encryptedMessage.packets[1] instanceof openpgp.AEADEncryptedDataPacket);
await openpgp.decrypt({ message: encryptedMessage, passwords, config });
});
suite.add('openpgp.encrypt/decrypt (AEAD, text, with streaming)', async () => {
await stream.loadStreamsPonyfill();
const passwords = 'password';
const config = { aeadProtect: true, preferredCompressionAlgorithm: openpgp.enums.compression.uncompressed };
const plaintextMessage = await openpgp.createMessage({ text: stream.toStream('a'.repeat(10000000 / 2)) });
assert(plaintextMessage.fromStream);
const armoredEncryptedMessage = await openpgp.encrypt({ message: plaintextMessage, passwords, config });
const encryptedMessage = await openpgp.readMessage({ armoredMessage: armoredEncryptedMessage });
assert.ok(encryptedMessage.packets[1] instanceof openpgp.AEADEncryptedDataPacket);
await openpgp.decrypt({ message: encryptedMessage, passwords, config });
});
suite.add('openpgp.encrypt/decrypt (PGP, text @ 100MB, with streaming)', async () => {
await stream.loadStreamsPonyfill();
const userID = { name: 'test', email: 'a@b.com' };
const passphrase = 'super long and hard to guess secret';
const { privateKey: rawPrivateKey, publicKey: rawPublicKey } =
await openpgp.generateKey({ userIDs: [userID], format: 'armored', passphrase });
function* largeDataGenerator() {
const chunkSize = 1024 * 1024; /*1MB*/
const numberOfChunks = 100;
for (let chunkNumber = 0; chunkNumber < numberOfChunks; chunkNumber++) {
yield 'a'.repeat(chunkSize);
}
}
const plaintextMessage = await openpgp.createMessage({ text: require('stream').Readable.from(largeDataGenerator()) });
assert(plaintextMessage.fromStream);
const publicKey = await openpgp.readKey({ armoredKey: rawPublicKey });
const armoredEncryptedMessage = await openpgp.encrypt({ message: plaintextMessage, encryptionKeys: [publicKey] });
const encryptedMessage = await openpgp.readMessage({ armoredMessage: armoredEncryptedMessage });
const privateKey = await openpgp.decryptKey({
privateKey: await openpgp.readKey({ armoredKey: rawPrivateKey }),
passphrase
});
await openpgp.decrypt({ message: encryptedMessage, decryptionKeys: privateKey });
});
const stats = await suite.run();
// Print JSON stats to stdout
console.log(JSON.stringify(stats, null, 4));
})();