All files / benchmarks benchmark.js

0% Statements 0/105
0% Branches 0/1
0% Functions 0/1
0% Lines 0/105

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                   
import http from 'http';
import express from 'express';
import { createApiRouter } from '../src/api/index.js';
import { performance } from 'perf_hooks';

// Simple HTTP server benchmark
function createHttpServer() {
  return http.createServer((req, res) => {
    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.end('<h1>Hello World</h1><p>This is a simple HTTP server</p>');
  });
}

// Express server benchmark
function createExpressServer() {
  const app = express();
  
  app.get('/', (req, res) => {
    res.send('<h1>Hello World</h1><p>This is an Express server</p>');
  });
  
  return app;
}

// Coherent.js API server benchmark
function createCoherentServer() {
  const router = createApiRouter();
  
  router.get('/', (req, res) => {
    return '<h1>Hello World</h1><p>This is a Coherent.js API server</p>';
  });
  
  const app = express();
  app.use(router.toExpress());
  return app;
}

// Benchmark function
async function runBenchmark(serverFactory, port, name, requests = 1000) {
  console.log(`\nšŸš€ Starting ${name} benchmark...`);
  
  // Create server
  const server = serverFactory();
  const listener = server.listen(port);
  
  // Wait for server to start
  await new Promise(resolve => setTimeout(resolve, 100));
  
  const startTime = performance.now();
  
  // Make requests
  for (let i = 0; i < requests; i++) {
    await fetch(`http://localhost:${port}/`);
  }
  
  const endTime = performance.now();
  const totalTime = endTime - startTime;
  const avgTime = totalTime / requests;
  const requestsPerSecond = (requests / totalTime) * 1000;
  
  // Close server
  listener.close();
  
  console.log(`${name} Results:`);
  console.log(`  Total time: ${totalTime.toFixed(2)}ms`);
  console.log(`  Average time per request: ${avgTime.toFixed(2)}ms`);
  console.log(`  Requests per second: ${requestsPerSecond.toFixed(2)}`);
  
  return {
    name,
    totalTime,
    avgTime,
    requestsPerSecond
  };
}

// Run all benchmarks
async function runAllBenchmarks() {
  console.log('šŸ”¬ Coherent.js vs Express.js Performance Benchmark');
  console.log('================================================');
  
  const results = [];
  
  // Run benchmarks
  results.push(await runBenchmark(createHttpServer, 6001, 'Node.js HTTP Server'));
  results.push(await runBenchmark(createExpressServer, 6002, 'Express.js Server'));
  results.push(await runBenchmark(createCoherentServer, 6003, 'Coherent.js API Server'));
  
  // Summary
  console.log('\nšŸ“Š Summary:');
  console.log('============');
  
  // Sort by requests per second
  results.sort((a, b) => b.requestsPerSecond - a.requestsPerSecond);
  
  results.forEach((result, index) => {
    const relative = (result.requestsPerSecond / results[0].requestsPerSecond) * 100;
    console.log(`${index + 1}. ${result.name}: ${result.requestsPerSecond.toFixed(2)} req/s (${relative.toFixed(1)}% of fastest)`);
  });
  
  console.log('\nāœ… Benchmark complete!');
}

// Run benchmarks
runAllBenchmarks().catch(console.error);