Skip to content

Commit

Permalink
w3vm: added call benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
lmittmann committed Jul 10, 2024
1 parent 5dad2b3 commit 70caedf
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
40 changes: 40 additions & 0 deletions w3vm/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,46 @@ func BenchmarkVM(b *testing.B) {
b.ReportMetric(float64(gasSimulated)/dur.Seconds(), "gas/s")
}

func BenchmarkVMCall_UniswapV3Quote(b *testing.B) {
addrQuoter := w3.A("0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6")
addrWETH := w3.A("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2")
addrUSDC := w3.A("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48")
funcQuote := w3.MustNewFunc("quoteExactInputSingle(address tokenIn, address tokenOut, uint24 fee, uint256 amountIn, uint160 sqrtPriceLimitX96)", "uint256 amountOut")

vm, err := w3vm.New(
w3vm.WithFork(client, big.NewInt(20_000_000)),
w3vm.WithTB(b),
)
if err != nil {
b.Fatalf("Failed to build VM: %v", err)
}

input, err := funcQuote.EncodeArgs(addrWETH, addrUSDC, big.NewInt(500), w3.BigEther, w3.Big0)
if err != nil {
b.Fatalf("Failed to encode input: %v", err)
}
quoteMsg := &w3types.Message{
To: &addrQuoter,
Input: input,
}

b.ReportAllocs()
b.ResetTimer()

var gasSimulated uint64
for range b.N {
receipt, err := vm.Call(quoteMsg)
if err != nil {
b.Fatalf("Failed to quote: %v", err)
}
gasSimulated += receipt.GasUsed
}

// report simulated gas per second
dur := b.Elapsed()
b.ReportMetric(float64(gasSimulated)/dur.Seconds(), "gas/s")
}

func BenchmarkVMSnapshot(b *testing.B) {
depositMsg := &w3types.Message{
From: addr0,
Expand Down
Loading

0 comments on commit 70caedf

Please sign in to comment.