I need to test, how much gas will be used to use binary search on array of 1,000,000 items in a smart contract. Is it possible to do that using any testing library or framework. I have used hardhat, which is too slow for populating the array. I am not sure about forge/foundry, but is there any way of testing that?
I have tried to populate the array with hardhat, directly making calls to smart contract , but it is too slow. It would be nice to find some way of testing that functionality.
The real case is: The items will be pushed step by step, each step is a different transaction, and when it reaches to 1,000,000 , it find one specific item.
If you add some sample code to your question, that might help get you a more specific answer. Right now, I can give you more general advice.
(1) Additional batch-add function:
Add a new function to your smart contract that allows you to add items in batches. E.g. 50 or more at a time, depending on the size of the parameters you need to send in and gas limits.
Use this only for testing purposes, and ensure that the function is removed before deployment if that is indeed your use case.
This approach will help you populate the required 1 million items in a fewer number of transactions. However, as long as the way your batch addition function works is compatible with your one-at-a-time addition function in terms of how it is persisted in smart contract storage, your subsequent search function will give you the same gas estimate.
(2) Existing deployment + hardhat forking
If you already have this smart contract deployed in the wild with real data of 1 million objects, you can may be able to skip the population step, and just connect to the already-deployed smart contract.
However, you will need to use Hardhat's network forking feature,
in combination with hardhat-ethers
's getImpersonatedSigner()
function
in order to be able to write any (meaningful) tests against it.
How this works is that HardhatVM (which is a simulated EVM implementation, that runs 100% on localhost
, and therefore very fast)
is told to replicate the state of a particular EVM network at a specific block number (similar to a temporary fork of the network).
Using this approach, you'll be able to write your tests against the target smart contract, of course, assuming that data layout and access is compatible with your approach.
(3) Set the block gas limit when testing
As mentioned by the OP in comments, for test VMs you can set any value for the block gas limit.
For example networks.hardhat.blockGasLimit
to configure HardhatVM
{
// ...
networks: {
hardhat: {
blockGasLimit: 1000000000
},
}
// ...
}
This can be used to set a higher allowance in the initial set up phase.