-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmark.mjs
More file actions
206 lines (167 loc) Β· 7.61 KB
/
benchmark.mjs
File metadata and controls
206 lines (167 loc) Β· 7.61 KB
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
/**
* Meow Decoder - Performance Benchmark (Node.js)
*
* Run with: node examples/benchmark.mjs
*
* This measures timing of each crypto operation to identify bottlenecks.
*/
import { readFile } from 'fs/promises';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Load the WASM module (ESM with async init)
const wasmModule = await import(join(__dirname, '..', 'crypto_core', 'pkg', 'crypto_core.js'));
const wasmPath = join(__dirname, '..', 'crypto_core', 'pkg', 'crypto_core_bg.wasm');
const wasmBytes = await readFile(wasmPath);
await wasmModule.default(wasmBytes);
const wasm = wasmModule;
// ANSI colors for terminal output
const colors = {
reset: '\x1b[0m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
cyan: '\x1b[36m',
bold: '\x1b[1m',
};
function formatMs(ms) {
if (ms > 1000) return `${(ms / 1000).toFixed(2)}s`;
return `${ms.toFixed(2)}ms`;
}
function getStatus(ms) {
if (ms > 1000) return { text: 'SLOW', color: colors.red };
if (ms > 100) return { text: 'MEDIUM', color: colors.yellow };
return { text: 'FAST', color: colors.green };
}
async function timeAsync(label, fn) {
const start = performance.now();
const result = await fn();
const elapsed = performance.now() - start;
return { result, elapsed, label };
}
function printHeader(text) {
console.log('\n' + colors.bold + colors.cyan + 'β'.repeat(60) + colors.reset);
console.log(colors.bold + ' ' + text + colors.reset);
console.log(colors.cyan + 'β'.repeat(60) + colors.reset);
}
function printTiming(timing) {
const status = getStatus(timing.elapsed);
console.log(
` ${timing.label.padEnd(45)} ${formatMs(timing.elapsed).padStart(10)} ${status.color}[${status.text}]${colors.reset}`
);
}
async function runFullBenchmark() {
printHeader('π¬ FULL CRYPTO BENCHMARK');
const encoder = new TextEncoder();
const testMessage = 'Hello, this is a test message for benchmarking! '.repeat(10);
const password = 'TestPassword123!';
const timings = [];
// 1. Generate salt
const saltTiming = await timeAsync('1. Generate salt (16 bytes)', async () => {
return wasm.generate_salt();
});
timings.push(saltTiming);
const salt = saltTiming.result.data;
// 2. Generate nonce
const nonceTiming = await timeAsync('2. Generate nonce (12 bytes)', async () => {
return wasm.generate_nonce();
});
timings.push(nonceTiming);
const nonce = nonceTiming.result.data;
// 3. Key derivation (Argon2id)
const keyTiming = await timeAsync('3. Key derivation (Argon2id, 64MiB, 3 iter)', async () => {
return wasm.derive_key(encoder.encode(password), salt, null, null);
});
timings.push(keyTiming);
const key = keyTiming.result.data;
// 4. Encryption
const encryptTiming = await timeAsync(`4. Encryption (AES-256-GCM, ${testMessage.length} bytes)`, async () => {
return wasm.encrypt(encoder.encode(testMessage), key, nonce, null);
});
timings.push(encryptTiming);
const ciphertext = encryptTiming.result.data;
// 5. Decryption
const decryptTiming = await timeAsync(`5. Decryption (AES-256-GCM, ${ciphertext.length} bytes)`, async () => {
return wasm.decrypt(ciphertext, key, nonce, null);
});
timings.push(decryptTiming);
// Print results
console.log('\n Operation Time Status');
console.log(' ' + '-'.repeat(56));
for (const t of timings) {
printTiming(t);
}
const total = timings.reduce((sum, t) => sum + t.elapsed, 0);
const keyPct = ((keyTiming.elapsed / total) * 100).toFixed(1);
console.log(' ' + '-'.repeat(56));
console.log(` ${colors.bold}TOTAL${colors.reset}`.padEnd(55) + formatMs(total).padStart(10));
console.log('\n' + colors.bold + 'π Analysis:' + colors.reset);
console.log(` β’ Key derivation is ${keyPct}% of total time`);
console.log(` β’ This is expected - Argon2id is intentionally slow for security`);
console.log(` β’ The Web Worker keeps UI responsive during this operation`);
return timings;
}
async function runKeyDerivationComparison() {
printHeader('π KEY DERIVATION PARAMETER COMPARISON');
const encoder = new TextEncoder();
const password = 'TestPassword123!';
const salt = wasm.generate_salt().data;
const configs = [
{ memory: 8192, iterations: 1, label: '8 MiB, 1 iter (Demo mode)' },
{ memory: 16384, iterations: 1, label: '16 MiB, 1 iter' },
{ memory: 32768, iterations: 2, label: '32 MiB, 2 iter (Fast)' },
{ memory: 65536, iterations: 3, label: '64 MiB, 3 iter (Default)' },
{ memory: 131072, iterations: 4, label: '128 MiB, 4 iter (High sec)' },
];
console.log('\n Configuration Time Status');
console.log(' ' + '-'.repeat(56));
for (const cfg of configs) {
const t = await timeAsync(`${cfg.label}`, async () => {
return wasm.derive_key(encoder.encode(password), salt, cfg.memory, cfg.iterations);
});
printTiming(t);
}
console.log('\n' + colors.bold + 'π‘ Recommendations:' + colors.reset);
console.log(' β’ Demo/testing: 8-16 MiB, 1 iter (~50-100ms)');
console.log(' β’ Interactive use: 32 MiB, 2 iter (~300-500ms)');
console.log(' β’ Default (secure): 64 MiB, 3 iter (~1-2s)');
console.log(' β’ High security: 128+ MiB, 4+ iter (slower but more secure)');
}
async function runEncryptionScaling() {
printHeader('π¦ ENCRYPTION PERFORMANCE BY DATA SIZE');
const encoder = new TextEncoder();
const password = 'TestPassword123!';
const salt = wasm.generate_salt().data;
const nonce = wasm.generate_nonce().data;
// Use faster key derivation for this test
console.log(' Using fast key derivation (8 MiB, 1 iter) for base measurement...');
const key = wasm.derive_key(encoder.encode(password), salt, 8192, 1).data;
const sizes = [100, 1000, 10000, 50000, 100000, 500000, 1000000];
console.log('\n Data Size Time Throughput');
console.log(' ' + '-'.repeat(60));
for (const size of sizes) {
const data = new Uint8Array(size).fill(65);
const start = performance.now();
wasm.encrypt(data, key, nonce, null);
const elapsed = performance.now() - start;
const throughput = (size / 1024 / 1024) / (elapsed / 1000); // MB/s
const sizeStr = size >= 1000000 ? `${(size/1000000).toFixed(1)} MB` :
size >= 1000 ? `${(size/1000).toFixed(0)} KB` : `${size} B`;
console.log(
` Encrypt ${sizeStr.padEnd(40)} ${formatMs(elapsed).padStart(10)} ${throughput.toFixed(1)} MB/s`
);
}
}
async function main() {
console.log(colors.bold + colors.green);
console.log('βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ');
console.log('β π± MEOW DECODER - PERFORMANCE PROFILER β');
console.log('βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ');
console.log(colors.reset);
await runFullBenchmark();
await runKeyDerivationComparison();
await runEncryptionScaling();
console.log('\n' + colors.green + 'β
Benchmark complete!' + colors.reset + '\n');
}
main().catch(console.error);