Skip to content

Commit

Permalink
VRF-881: adding load test metric calculation in seconds for VRF V2Plu…
Browse files Browse the repository at this point in the history
…s Consumer contract; adding more parametrization options in setup-env script
  • Loading branch information
iljapavlovs committed Feb 15, 2024
1 parent b275212 commit 72c46f5
Show file tree
Hide file tree
Showing 14 changed files with 312 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import {VRFV2PlusClient} from "../libraries/VRFV2PlusClient.sol";
contract VRFV2PlusLoadTestWithMetrics is VRFConsumerBaseV2Plus {
uint256 public s_responseCount;
uint256 public s_requestCount;
uint256 public s_averageFulfillmentInMillions = 0; // in millions for better precision
uint256 public s_slowestFulfillment = 0;
uint256 public s_fastestFulfillment = 999;
uint256 public s_averageResponseTimeInBlocksMillions = 0; // in millions for better precision
uint256 public s_slowestResponseTimeInBlocks = 0;
uint256 public s_fastestResponseTimeInBlocks = 999;
uint256 public s_slowestResponseTimeInSeconds = 0;
uint256 public s_fastestResponseTimeInSeconds = 999;
uint256 public s_averageResponseTimeInSecondsMillions = 0;

uint256 public s_lastRequestId;
// solhint-disable-next-line chainlink-solidity/prefix-storage-variables-with-s-underscore
mapping(uint256 => uint256) internal requestHeights; // requestIds to block number when rand request was made

struct RequestStatus {
bool fulfilled;
Expand All @@ -34,22 +36,38 @@ contract VRFV2PlusLoadTestWithMetrics is VRFConsumerBaseV2Plus {

// solhint-disable-next-line chainlink-solidity/prefix-internal-functions-with-underscore
function fulfillRandomWords(uint256 _requestId, uint256[] memory _randomWords) internal override {
uint256 fulfilmentBlockNumber = ChainSpecificUtil._getBlockNumber();
uint256 requestDelay = fulfilmentBlockNumber - requestHeights[_requestId];
uint256 requestDelayInMillions = requestDelay * 1_000_000;

if (requestDelay > s_slowestFulfillment) {
s_slowestFulfillment = requestDelay;
}
s_fastestFulfillment = requestDelay < s_fastestFulfillment ? requestDelay : s_fastestFulfillment;
s_averageFulfillmentInMillions = s_responseCount > 0
? (s_averageFulfillmentInMillions * s_responseCount + requestDelayInMillions) / (s_responseCount + 1)
: requestDelayInMillions;

s_requests[_requestId].fulfilled = true;
s_requests[_requestId].randomWords = _randomWords;
s_requests[_requestId].fulfilmentTimestamp = block.timestamp;
s_requests[_requestId].fulfilmentBlockNumber = fulfilmentBlockNumber;
s_requests[_requestId].fulfilmentBlockNumber = ChainSpecificUtil._getBlockNumber();

uint256 responseTimeInBlocks = s_requests[_requestId].fulfilmentBlockNumber -
s_requests[_requestId].requestBlockNumber;
uint256 responseTimeInSeconds = s_requests[_requestId].fulfilmentTimestamp -
s_requests[_requestId].requestTimestamp;

(
s_slowestResponseTimeInBlocks,
s_fastestResponseTimeInBlocks,
s_averageResponseTimeInBlocksMillions
) = _calculateMetrics(
responseTimeInBlocks,
s_fastestResponseTimeInBlocks,
s_slowestResponseTimeInBlocks,
s_averageResponseTimeInBlocksMillions,
s_responseCount
);
(
s_slowestResponseTimeInSeconds,
s_fastestResponseTimeInSeconds,
s_averageResponseTimeInSecondsMillions
) = _calculateMetrics(
responseTimeInSeconds,
s_fastestResponseTimeInSeconds,
s_slowestResponseTimeInSeconds,
s_averageResponseTimeInSecondsMillions,
s_responseCount
);

s_responseCount++;
}
Expand Down Expand Up @@ -86,14 +104,16 @@ contract VRFV2PlusLoadTestWithMetrics is VRFConsumerBaseV2Plus {
fulfilmentBlockNumber: 0
});
s_requestCount++;
requestHeights[requestId] = requestBlockNumber;
}
}

function reset() external {
s_averageFulfillmentInMillions = 0; // in millions for better precision
s_slowestFulfillment = 0;
s_fastestFulfillment = 999;
s_averageResponseTimeInBlocksMillions = 0; // in millions for better precision
s_slowestResponseTimeInBlocks = 0;
s_fastestResponseTimeInBlocks = 999;
s_averageResponseTimeInSecondsMillions = 0; // in millions for better precision
s_slowestResponseTimeInSeconds = 0;
s_fastestResponseTimeInSeconds = 999;
s_requestCount = 0;
s_responseCount = 0;
}
Expand Down Expand Up @@ -122,4 +142,23 @@ contract VRFV2PlusLoadTestWithMetrics is VRFConsumerBaseV2Plus {
request.fulfilmentBlockNumber
);
}

function _calculateMetrics(
uint256 _responseTime,
uint256 _fastestResponseTime,
uint256 _slowestResponseTime,
uint256 _averageInMillions,
uint256 _responseCount
) internal returns (uint256 slowest, uint256 fastest, uint256 average) {
uint256 _requestDelayInMillions = _responseTime * 1_000_000;
if (_responseTime > _slowestResponseTime) {
_slowestResponseTime = _responseTime;
}
_fastestResponseTime = _responseTime < _fastestResponseTime ? _responseTime : _fastestResponseTime;
uint256 _averageInMillions = _responseCount > 0
? (_averageInMillions * _responseCount + _requestDelayInMillions) / (_responseCount + 1)
: _requestDelayInMillions;

return (_slowestResponseTime, _fastestResponseTime, _averageInMillions);
}
}
Loading

0 comments on commit 72c46f5

Please sign in to comment.