about summary refs log tree commit diff stats
path: root/js/baba-yaga/src/benchmarks/benchmark-test.js
blob: c34bffc26232e68e7f466fcae44508f4e2335d56 (plain) (blame)
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
// benchmark-test.js - Simple benchmark test for our optimizations

import { benchmarkLexers } from './lexer-optimized.js';
import { benchmarkScopes } from './scope-stack.js';
import { benchmarkBuiltins } from './builtins-optimized.js';
import { BabaYagaEngine } from './engine.js';
import { OptimizedBabaYagaEngine } from './engine-optimized.js';
import { BabaYagaConfig } from './config.js';

// Test program for benchmarking
const testProgram = `
numbers : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
doubled : map (x -> x * 2) numbers;
filtered : filter (x -> x > 10) doubled;
sum : reduce (acc x -> acc + x) 0 filtered;

factorial : n ->
  when n is
    0 then 1
    1 then 1
    _ then n * (factorial (n - 1));

factorials : map factorial [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
result : reduce (acc x -> acc + x) sum factorials;
io.out result;
`;

async function runQuickBenchmark() {
  console.log('šŸš€ Quick Performance Benchmark\n');

  // Component benchmarks
  console.log('šŸ“Š Component Benchmarks:');
  
  console.log('šŸ”¤ Lexer:');
  const lexerResults = await benchmarkLexers(testProgram, 500);
  
  console.log('\nšŸ“š Scope Stack:');
  const scopeResults = await benchmarkScopes(25000);
  
  console.log('\n⚔ Built-ins:');
  const builtinResults = await benchmarkBuiltins(2500);
  
  // End-to-end benchmark
  console.log('\nšŸ End-to-End Benchmark:');
  
  // Original engine
  const originalConfig = new BabaYagaConfig({
    enableOptimizations: false,
    enableDebugMode: false
  });
  const originalEngine = new BabaYagaEngine(originalConfig);
  
  // Warm up
  for (let i = 0; i < 5; i++) {
    await originalEngine.execute(testProgram);
  }
  
  const iterations = 500;
  const originalStart = performance.now();
  for (let i = 0; i < iterations; i++) {
    await originalEngine.execute(testProgram);
  }
  const originalTime = performance.now() - originalStart;
  
  // Optimized engine
  const optimizedConfig = new BabaYagaConfig({
    enableOptimizations: true,
    enableDebugMode: false
  });
  const optimizedEngine = new OptimizedBabaYagaEngine(optimizedConfig);
  
  // Warm up
  for (let i = 0; i < 5; i++) {
    await optimizedEngine.execute(testProgram);
  }
  
  const optimizedStart = performance.now();
  for (let i = 0; i < iterations; i++) {
    await optimizedEngine.execute(testProgram);
  }
  const optimizedTime = performance.now() - optimizedStart;
  
  console.log(`Original engine:  ${originalTime.toFixed(2)}ms (${(originalTime/iterations).toFixed(2)}ms avg)`);
  console.log(`Optimized engine: ${optimizedTime.toFixed(2)}ms (${(optimizedTime/iterations).toFixed(2)}ms avg)`);
  console.log(`Overall speedup:  ${(originalTime/optimizedTime).toFixed(2)}x`);
  
  // Summary
  console.log('\nšŸ“ˆ Performance Summary:');
  console.log(`  Lexer speedup:    ${lexerResults.speedup.toFixed(2)}x`);
  console.log(`  Scope speedup:    ${scopeResults.speedup.toFixed(2)}x`);
  console.log(`  Built-in speedup: ${builtinResults.speedup.toFixed(2)}x`);
  console.log(`  Overall speedup:  ${(originalTime/optimizedTime).toFixed(2)}x`);
  
  const optimizedStats = optimizedEngine.getStats();
  console.log('\nšŸŽÆ Optimization Statistics:');
  console.log(`  Built-in optimization rate: ${(optimizedStats.optimizations.builtinOptimizationRate * 100).toFixed(1)}%`);
  console.log(`  AST pool hit rate:          ${(optimizedStats.optimizations.astPoolHitRate * 100).toFixed(1)}%`);
  console.log(`  Total optimizations used:   ${optimizedStats.optimizations.totalOptimizations}`);
}

// Run the benchmark
runQuickBenchmark().catch(console.error);