@@ -98,23 +98,23 @@ contract PulseGasBenchmark is Test, PulseTestUtils {
9898 );
9999 }
100100
101- function testFlow_01_Feed () public {
101+ function testBasicFlowWith01Feeds () public {
102102 _runBenchmarkWithFeeds (1 );
103103 }
104104
105- function testFlow_02_Feeds () public {
105+ function testBasicFlowWith02Feeds () public {
106106 _runBenchmarkWithFeeds (2 );
107107 }
108108
109- function testFlow_04_Feeds () public {
109+ function testBasicFlowWith04Feeds () public {
110110 _runBenchmarkWithFeeds (4 );
111111 }
112112
113- function testFlow_08_Feeds () public {
113+ function testBasicFlowWith08Feeds () public {
114114 _runBenchmarkWithFeeds (8 );
115115 }
116116
117- function testFlow_10_Feeds () public {
117+ function testBasicFlowWith10Feeds () public {
118118 _runBenchmarkWithFeeds (10 );
119119 }
120120
@@ -124,7 +124,9 @@ contract PulseGasBenchmark is Test, PulseTestUtils {
124124 // The last fulfillment will be the most expensive since it needs
125125 // to linearly scan through all the fulfilled requests in storage
126126 // in order to update _state.lastUnfulfilledReq
127- // NOTE: Run test with -vv to see extra gas logs.
127+ //
128+ // NOTE: Run test with `forge test --gas-report --match-test testMultipleRequestsOutOfOrderFulfillment`
129+ // and observe the `max` value for `executeCallback` to see the cost of the most expensive request.
128130 function testMultipleRequestsOutOfOrderFulfillment () public {
129131 uint64 timestamp = SafeCast.toUint64 (block .timestamp );
130132 bytes32 [] memory priceIds = createPriceIds (2 );
@@ -181,9 +183,76 @@ contract PulseGasBenchmark is Test, PulseTestUtils {
181183 uint endGas = gasleft ();
182184
183185 // Log gas usage for the last callback which would be the most expensive
184- // in the original implementation
185- console.log ("Gas used for last callback (seq 1): " , midGas - endGas);
186- console.log ("Gas used for all other callbacks: " , startGas - midGas);
186+ // in the original implementation (need to run test with -vv)
187+ console.log (
188+ "Gas used for last callback (seq 1): %s " ,
189+ vm.toString (midGas - endGas)
190+ );
191+ console.log (
192+ "Gas used for all other callbacks: %s " ,
193+ vm.toString (startGas - midGas)
194+ );
195+ }
196+
197+ // Helper function to run the overflow mapping benchmark with a specified number of feeds
198+ function _runOverflowBenchmarkWithFeeds (uint256 numFeeds ) internal {
199+ uint64 timestamp = SafeCast.toUint64 (block .timestamp );
200+ bytes32 [] memory priceIds = createPriceIds (numFeeds);
201+ uint32 callbackGasLimit = 100000 ;
202+ uint128 totalFee = pulse.getFee (
203+ defaultProvider,
204+ callbackGasLimit,
205+ priceIds
206+ );
207+
208+ // Create NUM_REQUESTS requests to fill up the main array
209+ // The constant is defined in PulseState.sol as 32
210+ uint64 [] memory sequenceNumbers = new uint64 [](32 );
211+ vm.deal (address (consumer), 50 ether);
212+
213+ // Use the same timestamp for all requests to avoid "Too far in future" error
214+ for (uint i = 0 ; i < 32 ; i++ ) {
215+ vm.prank (address (consumer));
216+ sequenceNumbers[i] = pulse.requestPriceUpdatesWithCallback {
217+ value: totalFee
218+ }(defaultProvider, timestamp, priceIds, callbackGasLimit);
219+ }
220+
221+ // Create one more request that will go to the overflow mapping
222+ // (This could potentially happen earlier if a shortKey collides,
223+ // but this guarantees it.)
224+ vm.prank (address (consumer));
225+ pulse.requestPriceUpdatesWithCallback {value: totalFee}(
226+ defaultProvider,
227+ timestamp,
228+ priceIds,
229+ callbackGasLimit
230+ );
231+ }
232+
233+ // These tests benchmark the gas usage when a new request overflows the fixed-size
234+ // request array and gets stored in the overflow mapping.
235+ //
236+ // NOTE: Run test with `forge test --gas-report --match-test testOverflowMappingGasUsageWithXXFeeds`
237+ // and observe the `max` value for `executeCallback` to see the cost of the overflowing request.
238+ function testOverflowMappingGasUsageWith01Feeds () public {
239+ _runOverflowBenchmarkWithFeeds (1 );
240+ }
241+
242+ function testOverflowMappingGasUsageWith02Feeds () public {
243+ _runOverflowBenchmarkWithFeeds (2 );
244+ }
245+
246+ function testOverflowMappingGasUsageWith04Feeds () public {
247+ _runOverflowBenchmarkWithFeeds (4 );
248+ }
249+
250+ function testOverflowMappingGasUsageWith08Feeds () public {
251+ _runOverflowBenchmarkWithFeeds (8 );
252+ }
253+
254+ function testOverflowMappingGasUsageWith10Feeds () public {
255+ _runOverflowBenchmarkWithFeeds (10 );
187256 }
188257}
189258
0 commit comments