@@ -20,7 +20,27 @@ const AMOUNT_PER_NOTE = 1_000_000;
2020
2121const MINIMUM_NOTES_FOR_RECURSION_LEVEL = [ 0 , 2 , 10 ] ;
2222
23+ /**
24+ * Expected number of node round trips per account contract and payment method.
25+ * These values are measured per single execution (stats are reset before each profile).
26+ * Update these values after verifying that any changes to round trip counts are intentional.
27+ *
28+ * Unfortunately these values depend on the test running in the expected sequence.
29+ */
30+ const EXPECTED_ROUND_TRIPS : Record < string , number > = {
31+ // 'ecdsar1+bridged_fee_juice': 32,
32+ 'ecdsar1+private_fpc' : 266 , // 215 when run on it's own? why?
33+ 'ecdsar1+sponsored_fpc' : 139 ,
34+ // 'ecdsar1+fee_juice': 32,
35+ // 'schnorr+bridged_fee_juice': 32,
36+ 'schnorr+private_fpc' : 506 ,
37+ 'schnorr+sponsored_fpc' : 289 ,
38+ // 'schnorr+fee_juice': 32,
39+ } ;
40+
2341describe ( 'AMM benchmark' , ( ) => {
42+ // Disable RPC batching by setting maxBatchSize to 0
43+ // This ensures accurate round trip measurements in the benchmark
2444 const t = new ClientFlowsBenchmark ( 'amm' ) ;
2545 // The wallet used by the admin to interact
2646 let adminWallet : Wallet ;
@@ -73,9 +93,11 @@ describe('AMM benchmark', () => {
7393 await t . teardown ( ) ;
7494 } ) ;
7595
76- for ( const accountType of config . accounts ) {
77- ammBenchmark ( accountType ) ;
78- }
96+ // for (const accountType of config.accounts) {
97+ // ammBenchmark(accountType);
98+ // }
99+ ammBenchmark ( 'ecdsar1' ) ;
100+ // ammBenchmark('schnorr');
79101
80102 function ammBenchmark ( accountType : AccountType ) {
81103 return describe ( `AMM benchmark for ${ accountType } ` , ( ) => {
@@ -158,7 +180,7 @@ describe('AMM benchmark', () => {
158180 . methods . add_liquidity ( amountToSend , amountToSend , amountToSend , amountToSend , nonceForAuthwits )
159181 . with ( { authWitnesses : [ token0Authwit , token1Authwit ] } ) ;
160182
161- await captureProfile (
183+ const profileResult = await captureProfile (
162184 `${ accountType } +amm_add_liquidity_1_recursions+${ benchmarkingPaymentMethod } ` ,
163185 addLiquidityInteraction ,
164186 options ,
@@ -176,6 +198,34 @@ describe('AMM benchmark', () => {
176198 1 , // Kernel hiding
177199 ) ;
178200
201+ // Log round trip details
202+ const roundTripStats = profileResult . stats . nodeRPCCalls ?. roundTrips ;
203+ if ( roundTripStats ) {
204+ console . log ( `\n=== Round Trip Details for ${ accountType } +${ benchmarkingPaymentMethod } ===` ) ;
205+ console . log ( `Total round trips: ${ roundTripStats . roundTrips } ` ) ;
206+ console . log ( `Total blocking time: ${ roundTripStats . totalBlockingTime . toFixed ( 2 ) } ms` ) ;
207+ console . log ( '\nPer round trip:' ) ;
208+ for ( let i = 0 ; i < roundTripStats . roundTripMethods . length ; i ++ ) {
209+ const methods = roundTripStats . roundTripMethods [ i ] ;
210+ const duration = roundTripStats . roundTripDurations [ i ] ;
211+ console . log ( ` RT ${ i + 1 } : ${ duration . toFixed ( 2 ) } ms - [${ methods . join ( ', ' ) } ]` ) ;
212+ }
213+ console . log ( '' ) ;
214+ }
215+
216+ // Check that the number of round trips matches the expected value.
217+ // This serves as a regression test - if round trips change, update EXPECTED_ROUND_TRIPS.
218+ const roundTripsKey = `${ accountType } +${ benchmarkingPaymentMethod } ` ;
219+ const actualRoundTrips = profileResult . stats . nodeRPCCalls ?. roundTrips . roundTrips ?? 0 ;
220+ const expectedRoundTrips = EXPECTED_ROUND_TRIPS [ roundTripsKey ] ;
221+ if ( expectedRoundTrips === undefined ) {
222+ throw new Error (
223+ `Missing expected round trips for ${ roundTripsKey } . ` +
224+ `Add '${ roundTripsKey } ': ${ actualRoundTrips } to EXPECTED_ROUND_TRIPS.` ,
225+ ) ;
226+ }
227+ expect ( actualRoundTrips ) . toBe ( expectedRoundTrips ) ;
228+
179229 if ( process . env . SANITY_CHECKS ) {
180230 const tx = await addLiquidityInteraction . send ( { from : benchysAddress } ) . wait ( ) ;
181231 expect ( tx . transactionFee ! ) . toBeGreaterThan ( 0n ) ;
@@ -184,9 +234,11 @@ describe('AMM benchmark', () => {
184234 } ) ;
185235 }
186236
187- for ( const paymentMethod of config . feePaymentMethods ) {
188- addLiquidityTest ( paymentMethod ) ;
189- }
237+ // for (const paymentMethod of config.feePaymentMethods) {
238+ // addLiquidityTest(paymentMethod);
239+ // }
240+ // addLiquidityTest('private_fpc');
241+ addLiquidityTest ( 'sponsored_fpc' ) ;
190242 } ) ;
191243 }
192244} ) ;
0 commit comments