11import { isSuccess } from "@/json-rpc/util" ;
22
3- type Method = "follow" | "header" | "body" ;
4-
5- type LatencyMap = Record < Method , { n : number ; total : number ; data : number [ ] } > ;
3+ type LatencyMap = Record < string , { n : number ; total : number ; data : number [ ] } > ;
64
75type Stats = {
86 host : string ;
@@ -15,7 +13,7 @@ type Stats = {
1513 latency : LatencyMap ;
1614} ;
1715
18- type Pending = { method : Method ; sent : number } ;
16+ type Pending = { method : string ; sent : number } ;
1917
2018export type Script = {
2119 onOpen ?: ( send : Send ) => void ;
@@ -24,18 +22,7 @@ export type Script = {
2422 onEvent ?: ( subId : string | null , ev : any , send : Send ) => void ;
2523} ;
2624
27- type Send = (
28- method : Method ,
29- rpcMethod : string ,
30- params : any [ ] ,
31- inFollow ?: boolean ,
32- ) => void ;
33-
34- const emptyLatency = ( ) : LatencyMap => ( {
35- follow : { n : 0 , total : 0 , data : [ ] } ,
36- header : { n : 0 , total : 0 , data : [ ] } ,
37- body : { n : 0 , total : 0 , data : [ ] } ,
38- } ) ;
25+ type Send = ( rpcMethod : string , params ?: any [ ] ) => number ;
3926
4027const createStats = ( host : string ) : Stats => ( {
4128 host,
@@ -45,10 +32,10 @@ const createStats = (host: string): Stats => ({
4532 exfiltrated : 0 ,
4633 msgs : 0 ,
4734 events : 0 ,
48- latency : emptyLatency ( ) ,
35+ latency : { } ,
4936} ) ;
5037
51- export function runChainHeadBench (
38+ export function runBench (
5239 endpoint : string ,
5340 {
5441 iterations,
@@ -70,12 +57,13 @@ export function runChainHeadBench(
7057 let nextId = 1 ;
7158 let subId : string | null = null ;
7259
73- const send : Send = ( method , rpcMethod , params ) => {
60+ const send : Send = ( rpcMethod , params ) => {
7461 const id = nextId ++ ;
75- pendings . set ( id , { method, sent : performance . now ( ) } ) ;
62+ pendings . set ( id , { method : rpcMethod , sent : performance . now ( ) } ) ;
7663 ws . send (
7764 JSON . stringify ( { jsonrpc : "2.0" , id, method : rpcMethod , params } ) ,
7865 ) ;
66+ return id ;
7967 } ;
8068
8169 ws . onopen = ( ) => {
@@ -96,7 +84,12 @@ export function runChainHeadBench(
9684 const ok = isSuccess ( msg ) ;
9785 if ( ! isWarm ) {
9886 const dt = now - p . sent ;
99- const lat = stats . latency [ p . method ] ;
87+
88+ if ( stats . latency [ p . method ] === undefined ) {
89+ stats . latency [ p . method ] = { n : 0 , total : 0 , data : [ ] } ;
90+ }
91+ const lat = stats . latency [ p . method ] ! ;
92+
10093 lat . n ++ ;
10194 lat . total += dt ;
10295 lat . data . push ( dt ) ;
@@ -109,7 +102,7 @@ export function runChainHeadBench(
109102 } else {
110103 script . onError ?.( msg . error , send ) ;
111104 }
112- if ( p . method === "follow " ) subId = msg . result ;
105+ if ( p . method === "chainHead_v1_follow " ) subId = msg . result ;
113106 } else {
114107 stats . exfiltrated ++ ;
115108 }
@@ -203,7 +196,9 @@ export const printSummary = (s: Stats) => {
203196 ) ;
204197 console . log ( "Latency (ms):" ) ;
205198
206- for ( const method of Object . keys ( s . latency ) as Method [ ] ) {
199+ for ( const method of Object . keys ( s . latency ) as string [ ] ) {
200+ if ( s . latency [ method ] === undefined ) continue ;
201+
207202 const xs = s . latency [ method ] . data ;
208203 const r = summarize ( xs ) ;
209204
0 commit comments