BSOL: Benchmarking Solidity Smart Contract

Giulio Rebuffo
Mar 7 · 3 min read

When you code, there are many repetitive actions that you do, which could be automated and many people create scripts to remove them and sometimes they even publish these scripts to the public. Some weeks ago, I made a tool that automates the estimation of Gas Usage in smart contracts so that I don’t have to write boring Javascript for it and I decided to publish it.

I called this tool BSOL(aka. Benchmarking Solidity). BSOL’s aim is to make it easier when estimating gas usage and execution time and remove the need for writing Javascript to estimate gas usage and execution time in smart contracts. So now, why could this be useful and how can it be used?

Why would you want to benchmark your Smart Contract?

The first thing that comes to mind is to keep track of how much gas your Smart Contract requires in order to work but if you are testing the EVM you may also be interested in how much time does your Smart Contract takes to execute.

Anyone can estimate the gas that a smart contract requires in order to work by just writing some javascript and with BSOL you can just do it without writing any javascript and just use your Solidity code.

Additionally, you can also check how much time your Smart Contract takes to execute on average. This is useful when studying the relation between gas usage and execution time. For example, it can be used to find flaws and possible DDOS attacks in the Ethereum EVM.

How do you Benchmark your smart contracts

First of all, you need to actually install BSOL:

git clone https://github.com/Giulio2002/bsol
cd bsol
sudo sh install.sh

once BSOL is installed we can start working: now let’s assume the following token

token.sol

Now let’s write the following benchmark contract:

token_benchmark.sol

What BSOL will do is taking every contract whose name starts with “Benchmark” and analysing each of their functions that starts with “Benchmark”. The first benchmark contract analyses the token transfer function when the recipient isn’t initialized yet. The second benchmark contract analyses the token transfer function when the recipient is initialized by initializing it in the constructor. So now let’s run our benchmark:

bsol --sol token_benchmark.solContract: BenchmarkToken
Method: BenchmarkToken.BenchmarkTransfer() # Initialized
Gas Usage: 33367 Gas
Gas Usage per execution: 12367 Gas
Contract: BenchmarkTokenCreate
Method: BenchmarkTokenCreate.BenchmarkTransfer() # Not Initialized
Gas Usage: 48367 Gas
Gas Usage per execution: 27367 Gas

if we add the flag then we also get the average computation time of our code (out of 500 runs):

bsol --sol token_benchmark.sol --execution-timeContract: BenchmarkToken
Method: BenchmarkToken.BenchmarkTransfer()
Average Computation time: 353.599576µs
Gas Usage: 33367 Gas
Gas Usage per execution: 12367 Gas
Contract: BenchmarkTokenCreate
Method: BenchmarkTokenCreate.BenchmarkTransfer()
Average Computation time: 327.343436µs
Gas Usage: 48367 Gas
Gas Usage per execution: 27367 Gas

Extra Stuff

Coinmonks

Coinmonks is a non-profit Crypto educational publication.

Giulio Rebuffo

Written by

Coinmonks

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

More From Medium

More from Coinmonks

More from Coinmonks

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade