-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBatchLockup.Gas.t.sol
272 lines (224 loc) · 10.5 KB
/
BatchLockup.Gas.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.22;
import { ud2x18 } from "@prb/math/src/UD2x18.sol";
import { LockupDynamic, LockupLinear, LockupTranched } from "@sablier/v2-core/src/types/DataTypes.sol";
import { BatchLockup } from "../src/types/DataTypes.sol";
import { BatchLockupBuilder } from "../test/utils/BatchLockupBuilder.sol";
import { Benchmark_Test } from "./Benchmark.t.sol";
/// @notice Tests used to benchmark {BatchLockup}.
/// @dev This contract creates a Markdown file with the gas usage of each function.
contract BatchLockup_Gas_Test is Benchmark_Test {
/*//////////////////////////////////////////////////////////////////////////
STATE VARIABLES
//////////////////////////////////////////////////////////////////////////*/
uint128 internal constant AMOUNT_PER_ITEM = 10e18;
uint8[5] internal batches = [5, 10, 20, 30, 50];
uint8[5] internal counts = [24, 24, 24, 24, 12];
/*//////////////////////////////////////////////////////////////////////////
TEST FUNCTION
//////////////////////////////////////////////////////////////////////////*/
function testGas_Implementations() external {
// Set the file path.
benchmarkResultsFile = string.concat(benchmarkResults, "SablierV2BatchLockup.md");
// Create the file if it doesn't exist, otherwise overwrite it.
vm.writeFile({
path: benchmarkResultsFile,
data: string.concat(
"# Benchmarks for BatchLockup\n\n",
"| Function | Lockup Type | Segments/Tranches | Batch Size | Gas Usage |\n",
"| --- | --- | --- | --- | --- |\n"
)
});
for (uint256 i; i < batches.length; ++i) {
// Benchmark the batch create functions for Lockup Linear.
gasCreateWithDurationsLL(batches[i]);
gasCreateWithTimestampsLL(batches[i]);
// Benchmark the batch create functions for Lockup Dynamic.
gasCreateWithDurationsLD({ batchSize: batches[i], segmentsCount: counts[i] });
gasCreateWithTimestampsLD({ batchSize: batches[i], segmentsCount: counts[i] });
// Benchmark the batch create functions for Lockup Tranched.
gasCreateWithDurationsLT({ batchSize: batches[i], tranchesCount: counts[i] });
gasCreateWithTimestampsLT({ batchSize: batches[i], tranchesCount: counts[i] });
}
}
/*//////////////////////////////////////////////////////////////////////////
GAS BENCHMARKS FOR BATCH FUNCTIONS
//////////////////////////////////////////////////////////////////////////*/
function gasCreateWithDurationsLD(uint256 batchSize, uint256 segmentsCount) internal {
BatchLockup.CreateWithDurationsLD[] memory params = BatchLockupBuilder.fillBatch({
params: defaults.createWithDurationsLD({
asset_: dai,
totalAmount_: uint128(AMOUNT_PER_ITEM * segmentsCount),
segments_: _generateSegmentsWithDuration(segmentsCount)
}),
batchSize: batchSize
});
uint256 initialGas = gasleft();
batchLockup.createWithDurationsLD(lockupDynamic, dai, params);
string memory gasUsed = vm.toString(initialGas - gasleft());
contentToAppend = string.concat(
"| `createWithDurationsLD` | Lockup Dynamic |",
vm.toString(segmentsCount),
" |",
vm.toString(batchSize),
" | ",
gasUsed,
" |"
);
// Append the content to the file.
appendToFile(benchmarkResultsFile, contentToAppend);
}
function gasCreateWithTimestampsLD(uint256 batchSize, uint256 segmentsCount) internal {
BatchLockup.CreateWithTimestampsLD[] memory params = BatchLockupBuilder.fillBatch({
params: defaults.createWithTimestampsLD({
asset_: dai,
totalAmount_: uint128(AMOUNT_PER_ITEM * segmentsCount),
segments_: _generateSegments(segmentsCount)
}),
batchSize: batchSize
});
uint256 initialGas = gasleft();
batchLockup.createWithTimestampsLD(lockupDynamic, dai, params);
string memory gasUsed = vm.toString(initialGas - gasleft());
contentToAppend = string.concat(
"| `createWithTimestampsLD` | Lockup Dynamic |",
vm.toString(segmentsCount),
" |",
vm.toString(batchSize),
" | ",
gasUsed,
" |"
);
// Append the data to the file
appendToFile(benchmarkResultsFile, contentToAppend);
}
function gasCreateWithDurationsLL(uint256 batchSize) internal {
BatchLockup.CreateWithDurationsLL[] memory params =
BatchLockupBuilder.fillBatch({ params: defaults.createWithDurationsLL(dai), batchSize: batchSize });
uint256 initialGas = gasleft();
batchLockup.createWithDurationsLL(lockupLinear, dai, params);
string memory gasUsed = vm.toString(initialGas - gasleft());
contentToAppend = string.concat(
"| `createWithDurationsLL` | Lockup Linear | N/A |", vm.toString(batchSize), " | ", gasUsed, " |"
);
// Append the content to the file.
appendToFile(benchmarkResultsFile, contentToAppend);
}
function gasCreateWithTimestampsLL(uint256 batchSize) internal {
BatchLockup.CreateWithTimestampsLL[] memory params =
BatchLockupBuilder.fillBatch({ params: defaults.createWithTimestampsLL(dai), batchSize: batchSize });
uint256 initialGas = gasleft();
batchLockup.createWithTimestampsLL(lockupLinear, dai, params);
string memory gasUsed = vm.toString(initialGas - gasleft());
contentToAppend = string.concat(
"| `createWithTimestampsLL` | Lockup Linear | N/A |", vm.toString(batchSize), " | ", gasUsed, " |"
);
// Append the data to the file
appendToFile(benchmarkResultsFile, contentToAppend);
}
function gasCreateWithDurationsLT(uint256 batchSize, uint256 tranchesCount) internal {
BatchLockup.CreateWithDurationsLT[] memory params = BatchLockupBuilder.fillBatch({
params: defaults.createWithDurationsLT({
asset_: dai,
totalAmount_: uint128(AMOUNT_PER_ITEM * tranchesCount),
tranches_: _generateTranchesWithDuration(tranchesCount)
}),
batchSize: batchSize
});
uint256 initialGas = gasleft();
batchLockup.createWithDurationsLT(lockupTranched, dai, params);
string memory gasUsed = vm.toString(initialGas - gasleft());
contentToAppend = string.concat(
"| `createWithDurationsLT` | Lockup Tranched |",
vm.toString(tranchesCount),
" |",
vm.toString(batchSize),
" | ",
gasUsed,
" |"
);
// Append the content to the file.
appendToFile(benchmarkResultsFile, contentToAppend);
}
function gasCreateWithTimestampsLT(uint256 batchSize, uint256 tranchesCount) internal {
BatchLockup.CreateWithTimestampsLT[] memory params = BatchLockupBuilder.fillBatch({
params: defaults.createWithTimestampsLT({
asset_: dai,
totalAmount_: uint128(AMOUNT_PER_ITEM * tranchesCount),
tranches_: _generateTranches(tranchesCount)
}),
batchSize: batchSize
});
uint256 initialGas = gasleft();
batchLockup.createWithTimestampsLT(lockupTranched, dai, params);
string memory gasUsed = vm.toString(initialGas - gasleft());
contentToAppend = string.concat(
"| `createWithTimestampsLT` | Lockup Tranched |",
vm.toString(tranchesCount),
" |",
vm.toString(batchSize),
" | ",
gasUsed,
" |"
);
// Append the data to the file
appendToFile(benchmarkResultsFile, contentToAppend);
}
/*//////////////////////////////////////////////////////////////////////////
HELPERS
//////////////////////////////////////////////////////////////////////////*/
function _generateSegments(uint256 segmentsCount) private view returns (LockupDynamic.Segment[] memory) {
LockupDynamic.Segment[] memory segments = new LockupDynamic.Segment[](segmentsCount);
// Populate segments.
for (uint256 i = 0; i < segmentsCount; ++i) {
segments[i] = LockupDynamic.Segment({
amount: AMOUNT_PER_ITEM,
exponent: ud2x18(0.5e18),
timestamp: getBlockTimestamp() + uint40(defaults.CLIFF_DURATION() * (1 + i))
});
}
return segments;
}
function _generateSegmentsWithDuration(uint256 segmentsCount)
private
view
returns (LockupDynamic.SegmentWithDuration[] memory)
{
LockupDynamic.SegmentWithDuration[] memory segments = new LockupDynamic.SegmentWithDuration[](segmentsCount);
// Populate segments.
for (uint256 i; i < segmentsCount; ++i) {
segments[i] = LockupDynamic.SegmentWithDuration({
amount: AMOUNT_PER_ITEM,
exponent: ud2x18(0.5e18),
duration: defaults.CLIFF_DURATION()
});
}
return segments;
}
function _generateTranches(uint256 tranchesCount) private view returns (LockupTranched.Tranche[] memory) {
LockupTranched.Tranche[] memory tranches = new LockupTranched.Tranche[](tranchesCount);
// Populate tranches.
for (uint256 i = 0; i < tranchesCount; ++i) {
tranches[i] = (
LockupTranched.Tranche({
amount: AMOUNT_PER_ITEM,
timestamp: getBlockTimestamp() + uint40(defaults.CLIFF_DURATION() * (1 + i))
})
);
}
return tranches;
}
function _generateTranchesWithDuration(uint256 tranchesCount)
private
view
returns (LockupTranched.TrancheWithDuration[] memory)
{
LockupTranched.TrancheWithDuration[] memory tranches = new LockupTranched.TrancheWithDuration[](tranchesCount);
// Populate tranches.
for (uint256 i; i < tranchesCount; ++i) {
tranches[i] =
LockupTranched.TrancheWithDuration({ amount: AMOUNT_PER_ITEM, duration: defaults.CLIFF_DURATION() });
}
return tranches;
}
}