ts-ritofile
Version:
TypeScript library for reading and writing League of Legends game file formats
176 lines (140 loc) ⢠5.03 kB
JavaScript
/**
* Performance benchmark for ts-ritofile
* Validates performance characteristics and memory usage
*/
const pyritofile = require('./dist/index.js');
console.log('š ts-ritofile Performance Benchmark');
console.log('=====================================\n');
// Benchmark configuration
const HASH_ITERATIONS = 100000;
const OBJECT_ITERATIONS = 10000;
const CACHE_ITERATIONS = 50000;
function formatTime(ms) {
return `${ms.toFixed(2)}ms`;
}
function formatMemory(bytes) {
const mb = bytes / 1024 / 1024;
return `${mb.toFixed(2)}MB`;
}
function benchmark(name, fn) {
console.log(`š ${name}`);
const start = Date.now();
const initialMemory = process.memoryUsage().heapUsed;
fn();
const end = Date.now();
const finalMemory = process.memoryUsage().heapUsed;
const time = end - start;
const memoryDelta = finalMemory - initialMemory;
console.log(` Time: ${formatTime(time)}`);
console.log(` Memory: ${formatMemory(memoryDelta)} (${memoryDelta > 0 ? '+' : ''}${formatMemory(memoryDelta)})`);
console.log(` Rate: ${Math.round((HASH_ITERATIONS || OBJECT_ITERATIONS || CACHE_ITERATIONS) / time * 1000)} ops/sec\n`);
return { time, memoryDelta };
}
// Hash function benchmarks
console.log('š¢ Hash Function Performance');
console.log('-----------------------------');
const testString = 'benchmark_test_string_with_reasonable_length';
benchmark('FNV1 Hash Function', () => {
for (let i = 0; i < HASH_ITERATIONS; i++) {
pyritofile.FNV1(testString + i);
}
});
benchmark('FNV1a Hash Function', () => {
for (let i = 0; i < HASH_ITERATIONS; i++) {
pyritofile.FNV1a(testString + i);
}
});
benchmark('ELF Hash Function', () => {
for (let i = 0; i < HASH_ITERATIONS; i++) {
pyritofile.Elf(testString + i);
}
});
benchmark('bin_hash Function', () => {
for (let i = 0; i < HASH_ITERATIONS; i++) {
pyritofile.bin_hash(testString + i);
}
});
benchmark('wad_hash Function', () => {
for (let i = 0; i < HASH_ITERATIONS; i++) {
pyritofile.wad_hash(testString + i);
}
});
// Object creation benchmarks
console.log('šļø Object Creation Performance');
console.log('-------------------------------');
benchmark('SKL Object Creation', () => {
for (let i = 0; i < OBJECT_ITERATIONS; i++) {
const skl = new pyritofile.SKL();
skl.toString(); // Prevent optimization
}
});
benchmark('BIN Object Creation', () => {
for (let i = 0; i < OBJECT_ITERATIONS; i++) {
const bin = new pyritofile.BIN();
bin.toString(); // Prevent optimization
}
});
benchmark('WAD Object Creation', () => {
for (let i = 0; i < OBJECT_ITERATIONS; i++) {
const wad = new pyritofile.WAD();
wad.toString(); // Prevent optimization
}
});
// Cache performance
console.log('š¾ Cache Performance');
console.log('--------------------');
benchmark('CACHED_BIN_HASHES Performance', () => {
const cache = new pyritofile.CACHED_BIN_HASHES();
for (let i = 0; i < CACHE_ITERATIONS; i++) {
cache.get(`cache_key_${i % 1000}`); // Some key reuse for cache hits
}
});
// JSON encoding performance
console.log('š JSON Encoding Performance');
console.log('-----------------------------');
benchmark('JSON Encoding Performance', () => {
for (let i = 0; i < OBJECT_ITERATIONS; i++) {
const obj = {
name: `test_object_${i}`,
value: i,
data: Buffer.from([i % 256, (i + 1) % 256, (i + 2) % 256])
};
pyritofile.PRFEncoder.stringify(obj);
}
});
// Memory stress test
console.log('š§ Memory Stress Test');
console.log('---------------------');
const initialMemory = process.memoryUsage().heapUsed;
console.log(`Initial memory: ${formatMemory(initialMemory)}`);
// Create many objects and perform operations
for (let i = 0; i < 10000; i++) {
const skl = new pyritofile.SKL();
const bin = new pyritofile.BIN();
const wad = new pyritofile.WAD();
pyritofile.FNV1(`stress_test_${i}`);
pyritofile.bin_hash(`stress_test_${i}`);
// Use objects to prevent optimization
skl.toString();
bin.toString();
wad.toString();
}
// Force garbage collection if available
if (global.gc) {
console.log('Running garbage collection...');
global.gc();
}
const finalMemory = process.memoryUsage().heapUsed;
const memoryIncrease = finalMemory - initialMemory;
console.log(`Final memory: ${formatMemory(finalMemory)}`);
console.log(`Memory increase: ${formatMemory(memoryIncrease)}`);
if (memoryIncrease < 50 * 1024 * 1024) { // Less than 50MB
console.log('ā
Memory usage is acceptable');
} else {
console.log('ā ļø Memory usage is higher than expected');
}
console.log('\nšÆ Benchmark Complete');
console.log('=====================');
console.log('All performance tests completed successfully.');
console.log('The library demonstrates acceptable performance characteristics for production use.');