Skip to content

Commit

Permalink
doc(benchmark): use various batch size
Browse files Browse the repository at this point in the history
  • Loading branch information
smol-ninja committed Jun 27, 2024
1 parent df117b5 commit 34f3dc4
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 75 deletions.
99 changes: 54 additions & 45 deletions benchmark/BatchLockup.Gas.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@ contract BatchLockup_Gas_Test is Benchmark_Test {
STATE VARIABLES
//////////////////////////////////////////////////////////////////////////*/

uint128 internal constant AMOUNT_PER_LD_STREAM = AMOUNT_PER_SEGMENT * SEGMENTS_PER_STREAM;
uint128 internal constant AMOUNT_PER_LT_STREAM = AMOUNT_PER_TRANCHE * TRANCHES_PER_STREAM;
uint128 internal constant AMOUNT_PER_SEGMENT = 10e18;
uint128 internal constant AMOUNT_PER_TRANCHE = 10e18;
uint128 internal constant SEGMENTS_PER_STREAM = 5;
uint128 internal constant TRANCHES_PER_STREAM = 5;

uint8[5] internal batchSize = [2, 5, 10, 20, 50];
uint128 internal constant AMOUNT_PER_COUNT = 10e18;
uint8[5] internal batches = [5, 10, 20, 30, 50];
uint8[5] internal counts = [24, 24, 24, 24, 12];

/*//////////////////////////////////////////////////////////////////////////
TEST FUNCTION
Expand All @@ -42,28 +37,31 @@ contract BatchLockup_Gas_Test is Benchmark_Test {
)
});

for (uint256 i; i < batchSize.length; ++i) {
for (uint256 i; i < batches.length; ++i) {
// Gas benchmark the batch create functions for Lockup Linear.
gasCreateWithDurationsLL(batchSize[i]);
gasCreateWithTimestampsLL(batchSize[i]);
gasCreateWithDurationsLL(batches[i]);
gasCreateWithTimestampsLL(batches[i]);

// Gas benchmark the batch create functions for Lockup Dynamic.
gasCreateWithDurationsLD(batchSize[i]);
gasCreateWithTimestampsLD(batchSize[i]);
gasCreateWithDurationsLD({ batchsize: batches[i], segmentsCount: counts[i] });
gasCreateWithTimestampsLD({ batchsize: batches[i], segmentsCount: counts[i] });

// Gas benchmark the batch create functions for Lockup Tranched.
gasCreateWithDurationsLT(batchSize[i]);
gasCreateWithTimestampsLT(batchSize[i]);
gasCreateWithDurationsLT({ batchsize: batches[i], tranchesCount: counts[i] });
gasCreateWithTimestampsLT({ batchsize: batches[i], tranchesCount: counts[i] });
}
}

/*//////////////////////////////////////////////////////////////////////////
GAS BENCHMARKS FOR BATCH FUNCTIONS
//////////////////////////////////////////////////////////////////////////*/

function gasCreateWithDurationsLD(uint256 batchsize) internal {
function gasCreateWithDurationsLD(uint256 batchsize, uint256 segmentsCount) internal {
BatchLockup.CreateWithDurationsLD[] memory params = BatchLockupBuilder.fillBatch(
defaults.createWithDurationsLD(dai, AMOUNT_PER_LD_STREAM, _segmentsWithDuration()), batchsize
defaults.createWithDurationsLD(
dai, uint128(AMOUNT_PER_COUNT * segmentsCount), _segmentsWithDuration(segmentsCount)
),
batchsize
);

uint256 beforeGas = gasleft();
Expand All @@ -72,7 +70,7 @@ contract BatchLockup_Gas_Test is Benchmark_Test {

contentToAppend = string.concat(
"| `createWithDurationsLD` | Lockup Dynamic |",
vm.toString(SEGMENTS_PER_STREAM),
vm.toString(segmentsCount),
" |",
vm.toString(batchsize),
" | ",
Expand All @@ -84,9 +82,10 @@ contract BatchLockup_Gas_Test is Benchmark_Test {
appendToFile(benchmarkResultsFile, contentToAppend);
}

function gasCreateWithTimestampsLD(uint256 batchsize) internal {
function gasCreateWithTimestampsLD(uint256 batchsize, uint256 segmentsCount) internal {
BatchLockup.CreateWithTimestampsLD[] memory params = BatchLockupBuilder.fillBatch(
defaults.createWithTimestampsLD(dai, AMOUNT_PER_LD_STREAM, _segments()), batchsize
defaults.createWithTimestampsLD(dai, uint128(AMOUNT_PER_COUNT * segmentsCount), _segments(segmentsCount)),
batchsize
);

uint256 beforeGas = gasleft();
Expand All @@ -95,7 +94,7 @@ contract BatchLockup_Gas_Test is Benchmark_Test {

contentToAppend = string.concat(
"| `createWithTimestampsLD` | Lockup Dynamic |",
vm.toString(SEGMENTS_PER_STREAM),
vm.toString(segmentsCount),
" |",
vm.toString(batchsize),
" | ",
Expand Down Expand Up @@ -139,9 +138,12 @@ contract BatchLockup_Gas_Test is Benchmark_Test {
appendToFile(benchmarkResultsFile, contentToAppend);
}

function gasCreateWithDurationsLT(uint256 batchsize) internal {
function gasCreateWithDurationsLT(uint256 batchsize, uint256 tranchesCount) internal {
BatchLockup.CreateWithDurationsLT[] memory params = BatchLockupBuilder.fillBatch(
defaults.createWithDurationsLT(dai, AMOUNT_PER_LT_STREAM, _tranchesWithDuration()), batchsize
defaults.createWithDurationsLT(
dai, uint128(AMOUNT_PER_COUNT * tranchesCount), _tranchesWithDuration(tranchesCount)
),
batchsize
);

uint256 beforeGas = gasleft();
Expand All @@ -150,7 +152,7 @@ contract BatchLockup_Gas_Test is Benchmark_Test {

contentToAppend = string.concat(
"| `createWithDurationsLT` | Lockup Tranched |",
vm.toString(TRANCHES_PER_STREAM),
vm.toString(tranchesCount),
" |",
vm.toString(batchsize),
" | ",
Expand All @@ -162,9 +164,10 @@ contract BatchLockup_Gas_Test is Benchmark_Test {
appendToFile(benchmarkResultsFile, contentToAppend);
}

function gasCreateWithTimestampsLT(uint256 batchsize) internal {
function gasCreateWithTimestampsLT(uint256 batchsize, uint256 tranchesCount) internal {
BatchLockup.CreateWithTimestampsLT[] memory params = BatchLockupBuilder.fillBatch(
defaults.createWithTimestampsLT(dai, AMOUNT_PER_LT_STREAM, _tranches()), batchsize
defaults.createWithTimestampsLT(dai, uint128(AMOUNT_PER_COUNT * tranchesCount), _tranches(tranchesCount)),
batchsize
);

uint256 beforeGas = gasleft();
Expand All @@ -173,7 +176,7 @@ contract BatchLockup_Gas_Test is Benchmark_Test {

contentToAppend = string.concat(
"| `createWithTimestampsLT` | Lockup Tranched |",
vm.toString(TRANCHES_PER_STREAM),
vm.toString(tranchesCount),
" |",
vm.toString(batchsize),
" | ",
Expand All @@ -189,13 +192,13 @@ contract BatchLockup_Gas_Test is Benchmark_Test {
HELPERS
//////////////////////////////////////////////////////////////////////////*/

function _segments() private view returns (LockupDynamic.Segment[] memory) {
LockupDynamic.Segment[] memory segments = new LockupDynamic.Segment[](SEGMENTS_PER_STREAM);
function _segments(uint256 segmentsCount) private view returns (LockupDynamic.Segment[] memory) {
LockupDynamic.Segment[] memory segments = new LockupDynamic.Segment[](segmentsCount);

// Populate segments.
for (uint256 i = 0; i < SEGMENTS_PER_STREAM; ++i) {
for (uint256 i = 0; i < segmentsCount; ++i) {
segments[i] = LockupDynamic.Segment({
amount: AMOUNT_PER_SEGMENT,
amount: AMOUNT_PER_COUNT,
exponent: ud2x18(0.5e18),
timestamp: getBlockTimestamp() + uint40(defaults.CLIFF_DURATION() * (1 + i))
});
Expand All @@ -204,14 +207,17 @@ contract BatchLockup_Gas_Test is Benchmark_Test {
return segments;
}

function _segmentsWithDuration() private view returns (LockupDynamic.SegmentWithDuration[] memory) {
LockupDynamic.SegmentWithDuration[] memory segments =
new LockupDynamic.SegmentWithDuration[](SEGMENTS_PER_STREAM);
function _segmentsWithDuration(uint256 segmentsCount)
private
view
returns (LockupDynamic.SegmentWithDuration[] memory)
{
LockupDynamic.SegmentWithDuration[] memory segments = new LockupDynamic.SegmentWithDuration[](segmentsCount);

// Populate segments.
for (uint256 i; i < SEGMENTS_PER_STREAM; ++i) {
for (uint256 i; i < segmentsCount; ++i) {
segments[i] = LockupDynamic.SegmentWithDuration({
amount: AMOUNT_PER_SEGMENT,
amount: AMOUNT_PER_COUNT,
exponent: ud2x18(0.5e18),
duration: defaults.CLIFF_DURATION()
});
Expand All @@ -220,13 +226,13 @@ contract BatchLockup_Gas_Test is Benchmark_Test {
return segments;
}

function _tranches() private view returns (LockupTranched.Tranche[] memory) {
LockupTranched.Tranche[] memory tranches = new LockupTranched.Tranche[](TRANCHES_PER_STREAM);
function _tranches(uint256 tranchesCount) private view returns (LockupTranched.Tranche[] memory) {
LockupTranched.Tranche[] memory tranches = new LockupTranched.Tranche[](tranchesCount);
// Populate tranches.
for (uint256 i = 0; i < TRANCHES_PER_STREAM; ++i) {
for (uint256 i = 0; i < tranchesCount; ++i) {
tranches[i] = (
LockupTranched.Tranche({
amount: AMOUNT_PER_TRANCHE,
amount: AMOUNT_PER_COUNT,
timestamp: getBlockTimestamp() + uint40(defaults.CLIFF_DURATION() * (1 + i))
})
);
Expand All @@ -235,14 +241,17 @@ contract BatchLockup_Gas_Test is Benchmark_Test {
return tranches;
}

function _tranchesWithDuration() private view returns (LockupTranched.TrancheWithDuration[] memory) {
LockupTranched.TrancheWithDuration[] memory tranches =
new LockupTranched.TrancheWithDuration[](TRANCHES_PER_STREAM);
function _tranchesWithDuration(uint256 tranchesCount)
private
view
returns (LockupTranched.TrancheWithDuration[] memory)
{
LockupTranched.TrancheWithDuration[] memory tranches = new LockupTranched.TrancheWithDuration[](tranchesCount);

// Populate tranches.
for (uint256 i; i < TRANCHES_PER_STREAM; ++i) {
for (uint256 i; i < tranchesCount; ++i) {
tranches[i] =
LockupTranched.TrancheWithDuration({ amount: AMOUNT_PER_TRANCHE, duration: defaults.CLIFF_DURATION() });
LockupTranched.TrancheWithDuration({ amount: AMOUNT_PER_COUNT, duration: defaults.CLIFF_DURATION() });
}

return tranches;
Expand Down
60 changes: 30 additions & 30 deletions benchmark/results/SablierV2BatchLockup.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@

| Function | Lockup Type | Segments/Tranches | Batch Size | Gas Usage |
| ------------------------ | --------------- | ----------------- | ---------- | --------- |
| `createWithDurationsLL` | Lockup Linear | | 2 | 361844 |
| `createWithTimestampsLL` | Lockup Linear | | 2 | 324523 |
| `createWithDurationsLD` | Lockup Dynamic | 5 | 2 | 608910 |
| `createWithTimestampsLD` | Lockup Dynamic | 5 | 2 | 578586 |
| `createWithDurationsLT` | Lockup Tranched | 5 | 2 | 601881 |
| `createWithTimestampsLT` | Lockup Tranched | 5 | 2 | 572680 |
| `createWithDurationsLL` | Lockup Linear | | 5 | 734485 |
| `createWithTimestampsLL` | Lockup Linear | | 5 | 732963 |
| `createWithDurationsLD` | Lockup Dynamic | 5 | 5 | 1391832 |
| `createWithTimestampsLD` | Lockup Dynamic | 5 | 5 | 1368315 |
| `createWithDurationsLT` | Lockup Tranched | 5 | 5 | 1374272 |
| `createWithTimestampsLT` | Lockup Tranched | 5 | 5 | 1353582 |
| `createWithDurationsLL` | Lockup Linear | | 10 | 1417161 |
| `createWithTimestampsLL` | Lockup Linear | | 10 | 1414212 |
| `createWithDurationsLD` | Lockup Dynamic | 5 | 10 | 2732969 |
| `createWithTimestampsLD` | Lockup Dynamic | 5 | 10 | 2686132 |
| `createWithDurationsLT` | Lockup Tranched | 5 | 10 | 2697702 |
| `createWithTimestampsLT` | Lockup Tranched | 5 | 10 | 2656477 |
| `createWithDurationsLL` | Lockup Linear | | 20 | 2783678 |
| `createWithTimestampsLL` | Lockup Linear | | 20 | 2778224 |
| `createWithDurationsLD` | Lockup Dynamic | 5 | 20 | 5419557 |
| `createWithTimestampsLD` | Lockup Dynamic | 5 | 20 | 5326631 |
| `createWithDurationsLT` | Lockup Tranched | 5 | 20 | 5348503 |
| `createWithTimestampsLT` | Lockup Tranched | 5 | 20 | 5266764 |
| `createWithDurationsLL` | Lockup Linear | | 50 | 6889015 |
| `createWithTimestampsLL` | Lockup Linear | | 50 | 6877961 |
| `createWithDurationsLD` | Lockup Dynamic | 5 | 50 | 13503395 |
| `createWithTimestampsLD` | Lockup Dynamic | 5 | 50 | 13276192 |
| `createWithDurationsLT` | Lockup Tranched | 5 | 50 | 13318736 |
| `createWithTimestampsLT` | Lockup Tranched | 5 | 50 | 13124266 |
| `createWithDurationsLL` | Lockup Linear | | 5 | 771013 |
| `createWithTimestampsLL` | Lockup Linear | | 5 | 732772 |
| `createWithDurationsLD` | Lockup Dynamic | 24 | 5 | 3951599 |
| `createWithTimestampsLD` | Lockup Dynamic | 24 | 5 | 3815274 |
| `createWithDurationsLT` | Lockup Tranched | 24 | 5 | 3862661 |
| `createWithTimestampsLT` | Lockup Tranched | 24 | 5 | 3744535 |
| `createWithDurationsLL` | Lockup Linear | | 10 | 1417180 |
| `createWithTimestampsLL` | Lockup Linear | | 10 | 1414248 |
| `createWithDurationsLD` | Lockup Dynamic | 24 | 10 | 7819162 |
| `createWithTimestampsLD` | Lockup Dynamic | 24 | 10 | 7585613 |
| `createWithDurationsLT` | Lockup Tranched | 24 | 10 | 7632113 |
| `createWithTimestampsLT` | Lockup Tranched | 24 | 10 | 7444113 |
| `createWithDurationsLL` | Lockup Linear | | 20 | 2783509 |
| `createWithTimestampsLL` | Lockup Linear | | 20 | 2779081 |
| `createWithDurationsLD` | Lockup Dynamic | 24 | 20 | 15617207 |
| `createWithTimestampsLD` | Lockup Dynamic | 24 | 20 | 15131248 |
| `createWithDurationsLT` | Lockup Tranched | 24 | 20 | 15211892 |
| `createWithTimestampsLT` | Lockup Tranched | 24 | 20 | 14846363 |
| `createWithDurationsLL` | Lockup Linear | | 30 | 4143337 |
| `createWithTimestampsLL` | Lockup Linear | | 30 | 4148585 |
| `createWithDurationsLD` | Lockup Dynamic | 24 | 30 | 23460912 |
| `createWithTimestampsLD` | Lockup Dynamic | 24 | 30 | 22697560 |
| `createWithDurationsLT` | Lockup Tranched | 24 | 30 | 22794686 |
| `createWithTimestampsLT` | Lockup Tranched | 24 | 30 | 22267335 |
| `createWithDurationsLL` | Lockup Linear | | 50 | 6871104 |
| `createWithTimestampsLL` | Lockup Linear | | 50 | 6893873 |
| `createWithDurationsLD` | Lockup Dynamic | 12 | 50 | 22990717 |
| `createWithTimestampsLD` | Lockup Dynamic | 12 | 50 | 22355937 |
| `createWithDurationsLT` | Lockup Tranched | 12 | 50 | 22413551 |
| `createWithTimestampsLT` | Lockup Tranched | 12 | 50 | 22006164 |

0 comments on commit 34f3dc4

Please sign in to comment.