Skip to content

Commit

Permalink
perf(levm): Add BubbleSort benchmark (#1839)
Browse files Browse the repository at this point in the history
**Motivation**
Adding more benchmarks to have an idea of how we fare vs other
implementations and find possible improvements.

**Description**
We add a new benchmark:

- `BubbleSort`: Creates an array of n random elements, sorts it via
bubble-sort and then checks the array is sorted.

<!-- Link to issues: Resolves #111, Resolves #222 -->
  • Loading branch information
dsocolobsky authored Jan 31, 2025
1 parent c2c3181 commit 0ad6473
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci_bench_levm_in_pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ jobs:
cat Fibonacci.md || echo "No results";
echo "#### Benchmark Results: ManyHashes";
cat ManyHashes.md || echo "No results";
echo "#### Benchmark Results: BubbleSort";
cat BubbleSort.md || echo "No results";
echo "#### Benchmark Results: ERC20 - Transfer";
cat ERC20Transfer.md || echo "No results";
echo "#### Benchmark Results: ERC20 - Mint";
Expand Down Expand Up @@ -97,6 +99,8 @@ jobs:
cat Fibonacci.md || echo "No results";
echo "#### Benchmark Results: ManyHashes";
cat ManyHashes.md || echo "No results";
echo "#### Benchmark Results: BubbleSort";
cat BubbleSort.md || echo "No results";
echo "#### Benchmark Results: ERC20 - Transfer";
cat ERC20Transfer.md || echo "No results";
echo "#### Benchmark Results: ERC20 - Mint";
Expand Down
5 changes: 5 additions & 0 deletions crates/vm/levm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ BENCH_FACT_ITERATIONS := 57
BENCH_FACT_REC_ITERATIONS := 57
BENCH_FIB_ITERATIONS := 57
BENCH_HASHES_ITERATIONS := 57
BENCH_BUBBLESORT_ITERATIONS := 100 # Size of the array to sort
BENCH_MINT_ITERATIONS := 500
BENCH_TRANSFER_ITERATIONS := 500
BENCH_APPROVAL_ITERATIONS := 500
Expand Down Expand Up @@ -110,6 +111,7 @@ revm-comparison: compile-contracts ## 📊 Run benchmarks of fibonacci and facto
$(call run_benchmark,Factorial,REPETITIONS,BENCH_FACT_ITERATIONS)
$(call run_benchmark,FactorialRecursive,REPETITIONS,BENCH_FACT_REC_ITERATIONS)
$(call run_benchmark,ManyHashes,REPETITIONS_SLOW,BENCH_HASHES_ITERATIONS)
$(call run_benchmark,BubbleSort,REPETITIONS_SLOW,BENCH_BUBBLESORT_ITERATIONS)
$(call run_benchmark,ERC20Approval,REPETITIONS_SLOW,BENCH_APPROVAL_ITERATIONS)
$(call run_benchmark,ERC20Transfer,REPETITIONS_SLOW,BENCH_TRANSFER_ITERATIONS)
$(call run_benchmark,ERC20Mint,REPETITIONS_SLOW,BENCH_MINT_ITERATIONS)
Expand All @@ -120,6 +122,7 @@ revm-comparison-ci: compile-contracts
$(call run_benchmark_ci,Factorial,REPETITIONS,BENCH_FACT_ITERATIONS)
$(call run_benchmark_ci,FactorialRecursive,REPETITIONS,BENCH_FACT_ITERATIONS)
$(call run_benchmark_ci,ManyHashes,REPETITIONS_SLOW,BENCH_HASHES_ITERATIONS)
$(call run_benchmark_ci,BubbleSort,REPETITIONS_SLOW,BENCH_BUBBLESORT_ITERATIONS)
$(call run_benchmark_ci,ERC20Approval,REPETITIONS_SLOW,BENCH_APPROVAL_ITERATIONS)
$(call run_benchmark_ci,ERC20Transfer,REPETITIONS_SLOW,BENCH_TRANSFER_ITERATIONS)
$(call run_benchmark_ci,ERC20Mint,REPETITIONS_SLOW,BENCH_MINT_ITERATIONS)
Expand Down Expand Up @@ -221,6 +224,7 @@ flamegraph-benchmarks: ## 🔥 Run benchmarks and create flamegraph
$(call run_flamegraph,Factorial,REPETITIONS,BENCH_FACT_ITERATIONS)
$(call run_flamegraph,FactorialRecursive,REPETITIONS,BENCH_FACT_ITERATIONS)
$(call run_flamegraph,ManyHashes,REPETITIONS_SLOW,BENCH_HASHES_ITERATIONS)
$(call run_flamegraph,BubbleSort,REPETITIONS_SLOW,BENCH_BUBBLESORT_ITERATIONS)
$(call run_flamegraph,ERC20Approval,REPETITIONS_SLOW,BENCH_APPROVAL_ITERATIONS)
$(call run_flamegraph,ERC20Transfer,REPETITIONS_SLOW,BENCH_TRANSFER_ITERATIONS)
$(call run_flamegraph,ERC20Mint,REPETITIONS_SLOW,BENCH_MINT_ITERATIONS)
Expand All @@ -233,6 +237,7 @@ samply-benchmarks: ## ⚡️ Run benchmarks and create samply profiling file
$(call run_samply,Factorial,REPETITIONS,BENCH_FACT_ITERATIONS)
$(call run_samply,FactorialRecursive,REPETITIONS,BENCH_FACT_ITERATIONS)
$(call run_samply,ManyHashes,REPETITIONS_SLOW,BENCH_HASHES_ITERATIONS)
$(call run_samply,BubbleSort,REPETITIONS_SLOW,BENCH_BUBBLESORT_ITERATIONS)
$(call run_samply,ERC20Approval,REPETITIONS_SLOW,BENCH_APPROVAL_ITERATIONS)
$(call run_samply,ERC20Transfer,REPETITIONS_SLOW,BENCH_TRANSFER_ITERATIONS)
$(call run_samply,ERC20Mint,REPETITIONS_SLOW,BENCH_MINT_ITERATIONS)
26 changes: 26 additions & 0 deletions crates/vm/levm/bench/revm_comparison/contracts/BubbleSort.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract BubbleSort {
uint256[] public numbers;

function Benchmark(uint256 amount) public returns (uint8 result) {
// Fill array with random numbers
for (uint256 i = 0; i < amount; i++) {
numbers.push(uint256(keccak256(abi.encodePacked(block.timestamp, block.prevrandao, i))) % 100);
}
uint256 n = numbers.length;
for (uint256 i = 0; i < n - 1; i++) {
for (uint256 j = 0; j < n - i - 1; j++) {
if (numbers[j] > numbers[j + 1]) {
(numbers[j], numbers[j + 1]) = (numbers[j + 1], numbers[j]);
}
}
}
// Ensure the array is sorted
for (uint256 i = 0; i < n - 1; i++) {
require(numbers[i] <= numbers[i + 1]);
}
return 0;
}
}
8 changes: 7 additions & 1 deletion crates/vm/levm/bench/revm_comparison/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ use std::fs;
use std::process::Command;

fn main() {
let contracts = ["Factorial", "FactorialRecursive", "Fibonacci", "ManyHashes"];
let contracts = [
"Factorial",
"FactorialRecursive",
"Fibonacci",
"ManyHashes",
"BubbleSort",
];
println!("Current directory: {:?}", std::env::current_dir().unwrap());
contracts.iter().for_each(|name| {
compile_contract(name);
Expand Down
8 changes: 4 additions & 4 deletions crates/vm/levm/bench/revm_comparison/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ pub fn run_with_levm(program: &str, runs: usize, calldata: &str) {
for _ in 0..runs - 1 {
let mut vm = new_vm_with_bytecode(bytecode.clone()).unwrap();
vm.call_frames.last_mut().unwrap().calldata = calldata.clone();
vm.env.gas_limit = 100_000_000;
vm.env.block_gas_limit = 100_000_001;
vm.env.gas_limit = u64::MAX - 1;
vm.env.block_gas_limit = u64::MAX;
let tx_report = black_box(vm.transact().unwrap());
assert!(tx_report.result == TxResult::Success);
}
let mut vm = new_vm_with_bytecode(bytecode.clone()).unwrap();
vm.call_frames.last_mut().unwrap().calldata = calldata.clone();
vm.env.gas_limit = 100_000_000;
vm.env.block_gas_limit = 100_000_001;
vm.env.gas_limit = u64::MAX - 1;
vm.env.block_gas_limit = u64::MAX;
let tx_report = black_box(vm.transact().unwrap());
assert!(tx_report.result == TxResult::Success);

Expand Down

0 comments on commit 0ad6473

Please sign in to comment.