|
| 1 | +#!/usr/bin/env node |
| 2 | +/* eslint-disable no-console */ |
| 3 | +/** |
| 4 | + * Performance comparison script for machine-id vs node-machine-id |
| 5 | + * |
| 6 | + * This script measures and compares the performance of @mongodb-js/machine-id |
| 7 | + * against the node-machine-id package. |
| 8 | + */ |
| 9 | + |
| 10 | +import { getMachineId } from '../dist/index.js'; |
| 11 | +import { machineIdSync } from 'node-machine-id'; |
| 12 | + |
| 13 | +// Configuration |
| 14 | +const ITERATIONS = 100; |
| 15 | +const WARMUP_ITERATIONS = 10; |
| 16 | + |
| 17 | +// Utility to format time |
| 18 | +function formatTime(ms: number): string { |
| 19 | + if (ms < 1) { |
| 20 | + return `${(ms * 1000).toFixed(2)}µs`; |
| 21 | + } |
| 22 | + return `${ms.toFixed(2)}ms`; |
| 23 | +} |
| 24 | + |
| 25 | +// Utility to format comparison |
| 26 | +function formatComparison(time1: number, time2: number): string { |
| 27 | + if (time1 < time2) { |
| 28 | + return `${(time2 / time1).toFixed(2)}x faster`; |
| 29 | + } else { |
| 30 | + return `${(time1 / time2).toFixed(2)}x slower`; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +function runBenchmark() { |
| 35 | + console.log('========================================'); |
| 36 | + console.log('Machine ID Performance Benchmark'); |
| 37 | + console.log('========================================'); |
| 38 | + console.log(`Platform: ${process.platform}`); |
| 39 | + console.log(`Node.js version: ${process.version}`); |
| 40 | + console.log(`Test iterations: ${ITERATIONS}`); |
| 41 | + console.log('----------------------------------------'); |
| 42 | + |
| 43 | + // Warm-up |
| 44 | + console.log('Warming up...'); |
| 45 | + for (let i = 0; i < WARMUP_ITERATIONS; i++) { |
| 46 | + getMachineId({ raw: true }); |
| 47 | + machineIdSync(true); |
| 48 | + } |
| 49 | + |
| 50 | + // Test raw mode (no hashing) |
| 51 | + console.log('\nRaw machine ID retrieval:'); |
| 52 | + |
| 53 | + const startOursRaw = process.hrtime.bigint(); |
| 54 | + for (let i = 0; i < ITERATIONS; i++) { |
| 55 | + getMachineId({ raw: true }); |
| 56 | + } |
| 57 | + const endOursRaw = process.hrtime.bigint(); |
| 58 | + const ourTimeRaw = Number(endOursRaw - startOursRaw) / 1_000_000; // ms |
| 59 | + |
| 60 | + // node-machine-id |
| 61 | + const startOtherRaw = process.hrtime.bigint(); |
| 62 | + for (let i = 0; i < ITERATIONS; i++) { |
| 63 | + machineIdSync(true); |
| 64 | + } |
| 65 | + const endOtherRaw = process.hrtime.bigint(); |
| 66 | + const otherTimeRaw = Number(endOtherRaw - startOtherRaw) / 1_000_000; // ms |
| 67 | + |
| 68 | + console.log( |
| 69 | + `@mongodb-js/machine-id: ${formatTime(ourTimeRaw)} total, ${formatTime(ourTimeRaw / ITERATIONS)} per call`, |
| 70 | + ); |
| 71 | + console.log( |
| 72 | + `node-machine-id: ${formatTime(otherTimeRaw)} total, ${formatTime(otherTimeRaw / ITERATIONS)} per call`, |
| 73 | + ); |
| 74 | + console.log( |
| 75 | + `Comparison: @mongodb-js/machine-id is ${formatComparison(ourTimeRaw, otherTimeRaw)}`, |
| 76 | + ); |
| 77 | + |
| 78 | + // Test hashed mode |
| 79 | + console.log('\nHashed machine ID:'); |
| 80 | + |
| 81 | + // @mongodb-js/machine-id |
| 82 | + const startOursHashed = process.hrtime.bigint(); |
| 83 | + for (let i = 0; i < ITERATIONS; i++) { |
| 84 | + getMachineId(); |
| 85 | + } |
| 86 | + const endOursHashed = process.hrtime.bigint(); |
| 87 | + const ourTimeHashed = Number(endOursHashed - startOursHashed) / 1_000_000; // ms |
| 88 | + |
| 89 | + // node-machine-id |
| 90 | + const startOtherHashed = process.hrtime.bigint(); |
| 91 | + for (let i = 0; i < ITERATIONS; i++) { |
| 92 | + machineIdSync(); |
| 93 | + } |
| 94 | + const endOtherHashed = process.hrtime.bigint(); |
| 95 | + const otherTimeHashed = Number(endOtherHashed - startOtherHashed) / 1_000_000; // ms |
| 96 | + |
| 97 | + console.log( |
| 98 | + `@mongodb-js/machine-id: ${formatTime(ourTimeHashed)} total, ${formatTime(ourTimeHashed / ITERATIONS)} per call`, |
| 99 | + ); |
| 100 | + console.log( |
| 101 | + `node-machine-id: ${formatTime(otherTimeHashed)} total, ${formatTime(otherTimeHashed / ITERATIONS)} per call`, |
| 102 | + ); |
| 103 | + console.log( |
| 104 | + `Comparison: @mongodb-js/machine-id is ${formatComparison(ourTimeHashed, otherTimeHashed)}`, |
| 105 | + ); |
| 106 | + |
| 107 | + console.log('\n========================================'); |
| 108 | +} |
| 109 | + |
| 110 | +// Run the benchmark |
| 111 | +runBenchmark(); |
0 commit comments