-
Notifications
You must be signed in to change notification settings - Fork 0
/
Benchmarks.t.sol
129 lines (101 loc) · 5.61 KB
/
Benchmarks.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.23;
import { LockupDynamic } from "@sablier/v2-core/src/types/DataTypes.sol";
import { ISablierV2LockupTranched } from "src/interfaces/ISablierV2LockupTranched.sol";
import { console2 } from "forge-std/src/console2.sol";
import { Base_Test } from "./Base.t.sol";
contract Benchmarks is Base_Test {
/*//////////////////////////////////////////////////////////////////////////
CREATE FUNCTION
//////////////////////////////////////////////////////////////////////////*/
// Note: we start from the second stream (index = 1), because the first stream will consume more gas the second
// stream, even if they both have a single segment/tranche. The first stream ever created involves writing
// multiple zero slots to non-zero values. This is we start from an index of 1.
function test_CreateWithMilestones_LockupDynamic_GasTests() external {
uint8[13] memory segmentCounts = [1, 2, 4, 6, 8, 12, 24, 36, 48, 60, 72, 96, 120];
uint256[] memory beforeGas = new uint256[](segmentCounts.length);
uint256[] memory afterGas = new uint256[](segmentCounts.length);
LockupDynamic.CreateWithMilestones memory params;
for (uint256 i = 0; i < segmentCounts.length; ++i) {
params = getDynamicParams(segmentCounts[i]);
beforeGas[i] = gasleft();
lockupDynamic.createWithMilestones(params);
afterGas[i] = gasleft();
}
for (uint256 i = 1; i < segmentCounts.length; ++i) {
uint256 gasUsed = beforeGas[i] - afterGas[i];
console2.log("Gas used for createWithMilestones: ", gasUsed, " with segments length: ", segmentCounts[i]);
}
}
function test_CreateWithMilestones_LockupTranched_GasTests() external {
uint8[9] memory trancheCounts = [1, 2, 4, 6, 12, 24, 36, 48, 60];
uint256[] memory beforeGas = new uint256[](trancheCounts.length);
uint256[] memory afterGas = new uint256[](trancheCounts.length);
ISablierV2LockupTranched.CreateWithMilestones memory params;
for (uint256 i = 0; i < trancheCounts.length; ++i) {
params = getTranchedParams(trancheCounts[i]);
beforeGas[i] = gasleft();
lockupTranched.createWithMilestones(params);
afterGas[i] = gasleft();
}
for (uint256 i = 1; i < trancheCounts.length; ++i) {
uint256 gasUsed = beforeGas[i] - afterGas[i];
console2.log("Gas used for createWithMilestones: ", gasUsed, " with tranches length: ", trancheCounts[i]);
}
}
/*//////////////////////////////////////////////////////////////////////////
WITHDRAW FUNCTION
//////////////////////////////////////////////////////////////////////////*/
function test_Withdraw_LockupDynamic_GasTests() external {
uint8[13] memory segmentCounts = [1, 2, 4, 6, 8, 12, 24, 36, 48, 60, 72, 96, 120];
uint256[] memory streamIds = new uint256[](segmentCounts.length);
LockupDynamic.CreateWithMilestones memory params;
for (uint256 i = 0; i < segmentCounts.length; ++i) {
params = getDynamicParams(segmentCounts[i]);
streamIds[i] = lockupDynamic.createWithMilestones(params);
}
uint256[] memory beforeGas = new uint256[](segmentCounts.length);
uint256[] memory afterGas = new uint256[](segmentCounts.length);
uint40 warpTime;
uint128 withdrawAmount;
for (uint256 i = 0; i < segmentCounts.length; ++i) {
// 10 seconds before the end time to calculate the streamed amount for the last segment
warpTime = lockupDynamic.getEndTime(streamIds[i]) - 10;
vm.warp(warpTime);
withdrawAmount = lockupDynamic.withdrawableAmountOf(streamIds[i]);
beforeGas[i] = gasleft();
lockupDynamic.withdraw(streamIds[i], recipient, withdrawAmount);
afterGas[i] = gasleft();
}
for (uint256 i = 1; i < segmentCounts.length; ++i) {
uint256 gasUsed = beforeGas[i] - afterGas[i];
console2.log("Gas used for withdraw: ", gasUsed, " with segments length: ", segmentCounts[i]);
}
}
function test_Withdraw_LockupTranched_GasTests() external {
uint8[9] memory trancheCounts = [2, 2, 4, 6, 12, 24, 36, 48, 60];
uint256[] memory streamIds = new uint256[](trancheCounts.length);
ISablierV2LockupTranched.CreateWithMilestones memory params;
for (uint256 i = 0; i < trancheCounts.length; ++i) {
params = getTranchedParams(trancheCounts[i]);
streamIds[i] = lockupTranched.createWithMilestones(params);
}
uint256[] memory beforeGas = new uint256[](trancheCounts.length);
uint256[] memory afterGas = new uint256[](trancheCounts.length);
uint40 warpTime;
uint128 withdrawAmount;
for (uint256 i = 0; i < trancheCounts.length; ++i) {
// 10 seconds before the end time to calculate the streamed amount for the last tranche
warpTime = lockupTranched.getEndTime(streamIds[i]) - 10;
vm.warp(warpTime);
withdrawAmount = lockupTranched.withdrawableAmountOf(streamIds[i]);
beforeGas[i] = gasleft();
lockupTranched.withdraw(streamIds[i], recipient, withdrawAmount);
afterGas[i] = gasleft();
}
for (uint256 i = 1; i < trancheCounts.length; ++i) {
uint256 gasUsed = beforeGas[i] - afterGas[i];
console2.log("Gas used for withdraw: ", gasUsed, " with tranches length: ", trancheCounts[i]);
}
}
}