-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(changelog): optimize enforceSizeBoundary
These optimizations make the enforceSizeBoundary function more efficient across various scenarios, leading to overall better performance while maintaining the same behaviour (*). Running tool: /home/gg/.goenv/versions/1.22.4/bin/go test -benchmem -run=^$ -tags ebpf -bench ^(BenchmarkEnforceSizeBoundaryOld|BenchmarkEnforceSizeBoundary)$ github.com/aquasecurity/tracee/pkg/changelog -benchtime=100000000x goos: linux goarch: amd64 pkg: github.com/aquasecurity/tracee/pkg/changelog cpu: AMD Ryzen 9 7950X 16-Core Processor | Test Case | Old (ns/op) | New (ns/op) | (%) | |------------------------|-------------|-------------|--------| | No change needed | 1.494 | 1.395 | 6.63% | | Trim excess duplicates | 30.07 | 21.53 | 28.41% | | Remove oldest entries | 27.71 | 24.96 | 9.90% | Tests and benchmarks are included. (*) The behaviour of the function is the same, but the implementation fixes a bug that when coalescing duplicate values, the removal of the oldest entry was not being done correctly. Instead of removing the oldest entry, the function was removing the newest one.
- Loading branch information
Showing
3 changed files
with
251 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package changelog | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func BenchmarkEnforceSizeBoundary(b *testing.B) { | ||
// Define test cases | ||
testCases := []struct { | ||
name string | ||
changelog Changelog[int] | ||
}{ | ||
{ | ||
name: "No change needed", | ||
changelog: Changelog[int]{ | ||
changes: []item[int]{ | ||
{value: 1, timestamp: getTimeFromSec(1)}, | ||
{value: 2, timestamp: getTimeFromSec(2)}, | ||
}, | ||
timestamps: map[time.Time]int{ | ||
getTimeFromSec(1): 0, | ||
getTimeFromSec(2): 1, | ||
}, | ||
maxSize: 5, | ||
}, | ||
}, | ||
{ | ||
name: "Trim excess with duplicates", | ||
changelog: Changelog[int]{ | ||
changes: []item[int]{ | ||
{value: 1, timestamp: getTimeFromSec(1)}, | ||
{value: 1, timestamp: getTimeFromSec(2)}, | ||
{value: 2, timestamp: getTimeFromSec(3)}, | ||
{value: 3, timestamp: getTimeFromSec(4)}, | ||
{value: 3, timestamp: getTimeFromSec(5)}, | ||
}, | ||
timestamps: map[time.Time]int{ | ||
getTimeFromSec(1): 0, | ||
getTimeFromSec(2): 1, | ||
getTimeFromSec(3): 2, | ||
getTimeFromSec(4): 3, | ||
getTimeFromSec(5): 4, | ||
}, | ||
maxSize: 3, | ||
}, | ||
}, | ||
{ | ||
name: "Remove oldest entries", | ||
changelog: Changelog[int]{ | ||
changes: []item[int]{ | ||
{value: 1, timestamp: getTimeFromSec(1)}, | ||
{value: 2, timestamp: getTimeFromSec(2)}, | ||
{value: 3, timestamp: getTimeFromSec(3)}, | ||
{value: 4, timestamp: getTimeFromSec(4)}, | ||
}, | ||
timestamps: map[time.Time]int{ | ||
getTimeFromSec(1): 0, | ||
getTimeFromSec(2): 1, | ||
getTimeFromSec(3): 2, | ||
getTimeFromSec(4): 3, | ||
}, | ||
maxSize: 2, | ||
}, | ||
}, | ||
} | ||
|
||
// Run benchmarks | ||
for _, tc := range testCases { | ||
b.Run(tc.name, func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
clv := tc.changelog // Create a copy for each iteration | ||
clv.enforceSizeBoundary() | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.