-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.ts
More file actions
74 lines (61 loc) · 2.31 KB
/
benchmark.ts
File metadata and controls
74 lines (61 loc) · 2.31 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
import { FpArchiveDataManager } from './src/MultiZipReader';
import { crc32 } from 'zlib';
const testDir = "G:\\Data\\Flashpoint\\Data\\ArchiveData";
async function benchmarkZipRead(): Promise<void> {
const archive = new FpArchiveDataManager();
const start = performance.now();
await archive.loadArchive("G:\\Data\\Flashpoint\\Data\\ArchiveData\\Images.zip");
const end = performance.now();
const timeMs = end - start;
// Calculate average index time
let indexTotal = 0;
for (const source of archive.sources) {
indexTotal += Object.keys(source.data).length;
}
const filesPerSecond = indexTotal / (timeMs / 1000);
console.log(`Indexed ${indexTotal.toLocaleString()} Files in ${timeMs.toFixed(0)}ms (${Math.floor(filesPerSecond).toLocaleString()} Files/sec)`)
}
async function testZipRead(): Promise<void> {
const archive = new FpArchiveDataManager();
await archive.loadDirectory(testDir);
const names = Object.keys(archive.sources[0]!.data);
for (let i = 0; i < 100; i++) {
// Verify 100 random files
const randIdx = Math.floor(Math.random() * names.length);
const name = names[randIdx] as string;
const file = archive.sources[0]!.data[name];
const stream = await archive.readFileStream(name);
if (stream !== null && file !== undefined) {
const hash = await new Promise<number>((resolve, reject) => {
const chunks: Buffer[] = [];
stream.on('data', (chunk) => {
if (typeof chunk === 'string') {
chunks.push(Buffer.from(chunk));
} else {
chunks.push(chunk);
}
});
stream.on('end', () => {
const buffer = Buffer.concat(chunks);
if (buffer.length !== file.length) {
throw new Error('Did not read whole length');
}
const hash = crc32(buffer);
resolve(hash >>> 0); // Convert to unsigned 32-bit
});
stream.on('error', reject);
});
if (hash !== file.crc32) {
throw new Error(`Bad hash: Expected ${file.crc32.toString(16).toUpperCase()}, Got ${hash.toString(16).toUpperCase()}`);
}
} else {
throw new Error('File missing?');
}
}
console.log('Read data matched successfully');
}
async function run() {
await benchmarkZipRead();
await testZipRead();
}
run();