11import yargs from 'yargs' ;
22import { hideBin } from 'yargs/helpers' ;
33import { basename } from 'path' ;
4+ import { promises as fs } from 'fs' ;
45import * as hdr from 'hdr-histogram-js' ;
56hdr . initWebAssemblySync ( ) ;
67
7- const { path, times, concurrency } = yargs ( hideBin ( process . argv ) )
8+ const { path, times, concurrency, 'redis-server-host' : host } = yargs ( hideBin ( process . argv ) )
89 . option ( 'path' , {
910 type : 'string' ,
1011 demandOption : true
@@ -19,49 +20,40 @@ const { path, times, concurrency } = yargs(hideBin(process.argv))
1920 default : 100 ,
2021 demandOption : true
2122 } )
23+ . option ( 'redis-server-host' , {
24+ type : 'string'
25+ } )
2226 . parseSync ( ) ;
2327
24- async function setup ( ) {
25- const module = await import ( path ) ;
26- await module . setup ( ) ;
27- return module ;
28- }
29-
30- function getMetadata ( ) {
31- return new Promise ( resolve => {
32- process . once ( 'message' , resolve ) ;
33- process . send ( 'ready' ) ;
34- } ) ;
35- }
36-
37- const [ { benchmark, teardown } , metadata ] = await Promise . all ( [
38- setup ( ) ,
39- getMetadata ( )
40- ] ) ;
28+ const [ { metadata, timestamp } , module ] = await Promise . all ( [
29+ new Promise ( resolve => {
30+ process . once ( 'message' , resolve ) ;
31+ process . send ( 'ready' ) ;
32+ } ) ,
33+ import ( path )
34+ ] ) ,
35+ { benchmark, teardown } = await module . default ( host , metadata ) ;
4136
4237async function run ( times ) {
4338 return new Promise ( resolve => {
4439 const histogram = hdr . build ( { useWebAssembly : true } ) ;
4540 let num = 0 ,
4641 inProgress = 0 ;
4742
48- function run ( ) {
43+ async function run ( ) {
4944 ++ inProgress ;
5045 ++ num ;
5146
5247 const start = process . hrtime . bigint ( ) ;
53- benchmark ( metadata )
54- . catch ( err => console . error ( err ) )
55- . finally ( ( ) => {
56- histogram . recordValue ( Number ( process . hrtime . bigint ( ) - start ) ) ;
57- -- inProgress ;
48+ await benchmark ( metadata ) ;
49+ histogram . recordValue ( Number ( process . hrtime . bigint ( ) - start ) ) ;
50+ -- inProgress ;
5851
59- if ( num < times ) {
60- run ( ) ;
61- } else if ( inProgress === 0 ) {
62- resolve ( histogram ) ;
63- }
64- } ) ;
52+ if ( num < times ) {
53+ run ( ) ;
54+ } else if ( inProgress === 0 ) {
55+ resolve ( histogram ) ;
56+ }
6557 }
6658
6759 const toInitiate = Math . min ( concurrency , times ) ;
@@ -75,8 +67,20 @@ async function run(times) {
7567await run ( Math . min ( times * 0.1 , 10_000 ) ) ;
7668
7769// benchmark
78- const histogram = await run ( times ) ;
70+ const benchmarkStart = process . hrtime . bigint ( ) ,
71+ histogram = await run ( times ) ,
72+ benchmarkNanoseconds = process . hrtime . bigint ( ) - benchmarkStart ,
73+ json = {
74+ timestamp,
75+ operationsPerSecond : times / Number ( benchmarkNanoseconds ) * 1_000_000_000 ,
76+ p0 : histogram . getValueAtPercentile ( 0 ) ,
77+ p50 : histogram . getValueAtPercentile ( 50 ) ,
78+ p95 : histogram . getValueAtPercentile ( 95 ) ,
79+ p99 : histogram . getValueAtPercentile ( 99 ) ,
80+ p100 : histogram . getValueAtPercentile ( 100 )
81+ } ;
7982console . log ( `[${ basename ( path ) } ]:` ) ;
80- console . table ( histogram . toJSON ( ) ) ;
83+ console . table ( json ) ;
84+ await fs . writeFile ( `${ path } .json` , JSON . stringify ( json ) ) ;
8185
8286await teardown ( ) ;
0 commit comments