Skip to content

Commit

Permalink
chore: add min and max tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
St0rmBr3w committed Dec 4, 2024
1 parent 923520e commit 28aeac1
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion examples/oft/scripts/GasProfiler.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,11 @@ contract GasProfilerScript is Script {
uint256 gasBefore = gasleft();

(bool success, ) = caller.call{ value: params.msgValue }(callParams);
vm.stopPrank();

uint256 gasAfter = gasleft();

vm.stopPrank();

if (success) {
uint256 gasUsed = gasBefore - gasAfter;
gasUsedArray[successfulRuns] = gasUsed;
Expand All @@ -193,10 +195,14 @@ contract GasProfilerScript is Script {
function _logGasMetrics(uint256[] memory gasUsedArray, uint256 totalGasUsed, uint256 successfulRuns) internal view {
uint256 averageGas = totalGasUsed / successfulRuns;
uint256 medianGas = _calculateMedian(gasUsedArray, successfulRuns);
uint256 maximumGas = _calculateMaximum(gasUsedArray, successfulRuns);
uint256 minimumGas = _calculateMinimum(gasUsedArray, successfulRuns);

console.log("Gas Usage Metrics:");
console.log("Average Gas Used:", averageGas);
console.log("Median Gas Used:", medianGas);
console.log("Maximum Gas Used:", maximumGas);
console.log("Minimum Gas Used:", minimumGas);
console.log("Successful Runs:", successfulRuns);
}

Expand All @@ -210,6 +216,18 @@ contract GasProfilerScript is Script {
}
}

/// @notice Calculates the maximum of an array.
function _calculateMaximum(uint256[] memory array, uint256 length) internal pure returns (uint256) {
_sortArray(array, length);
return array[length - 1];
}

/// @notice Calculates the minimum of an array.
function _calculateMinimum(uint256[] memory array, uint256 length) internal pure returns (uint256) {
_sortArray(array, length);
return array[0];
}

/// @notice Sorts an array in ascending order.
function _sortArray(uint256[] memory array, uint256 length) internal pure {
for (uint256 i = 1; i < length; i++) {
Expand Down

0 comments on commit 28aeac1

Please sign in to comment.