Skip to content

Commit a54777a

Browse files
committed
test: add benchmarks
Closes TNTP-3733
1 parent 93a7028 commit a54777a

File tree

13 files changed

+1082
-7
lines changed

13 files changed

+1082
-7
lines changed

.github/workflows/testing.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,18 @@ jobs:
5151
run: make coverage coveralls-deps coveralls
5252

5353
run-benchmarks:
54-
if: false
54+
if: (github.event_name == 'push') ||
55+
(github.event_name == 'pull_request' &&
56+
github.event.pull_request.head.repo.full_name != github.repository) ||
57+
(github.event_name == 'workflow_dispatch')
5558

5659
runs-on: ubuntu-latest
5760

61+
strategy:
62+
fail-fast: false
63+
matrix:
64+
golang: ['1.24', 'stable']
65+
5866
steps:
5967
- uses: actions/checkout@v4
6068
- uses: actions/setup-go@v5

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ testrace:
1717
@echo "Running tests with race flag"
1818
@go test ./... -count=100 -race
1919

20+
.PHONY: bench
21+
bench:
22+
@echo "Running benchmarks"
23+
@go test ./... -count=1 -bench=. -benchmem
24+
2025
.PHONY: coverage
2126
coverage:
2227
@echo "Running tests with coveralls"

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,72 @@ value := opt.UnwrapOr("default")
9393
err := opt.EncodeMsgpack(encoder)
9494
```
9595

96+
## Benchmarking
97+
98+
Along with the approach supplied with go-option library pointer-based and slice-based approaches were benchmarked as well.
99+
100+
### Empty base type (init + Get)
101+
102+
```
103+
BenchmarkNoneInt/Typed-8 560566400 2.200 ns/op 0 B/op 0 allocs/op
104+
BenchmarkNoneInt/Generic-8 543332625 2.193 ns/op 0 B/op 0 allocs/op
105+
BenchmarkNoneInt/GenericPtr-8 487631254 2.474 ns/op 0 B/op 0 allocs/op
106+
BenchmarkNoneInt/GenericSlice-8 441513422 2.608 ns/op 0 B/op 0 allocs/op
107+
```
108+
109+
### Empty struct type (init + Get)
110+
111+
```
112+
BenchmarkNoneStruct/Typed-8 384845384 3.107 ns/op 0 B/op 0 allocs/op
113+
BenchmarkNoneStruct/Generic-8 415633797 2.884 ns/op 0 B/op 0 allocs/op
114+
BenchmarkNoneStruct/GenericPtr-8 331620082 3.580 ns/op 0 B/op 0 allocs/op
115+
BenchmarkNoneStruct/GenericSlice-8 387593746 3.115 ns/op 0 B/op 0 allocs/op
116+
```
117+
118+
### Non-empty base type (init + Get)
119+
120+
```
121+
BenchmarkSomeInt/Typed-8 499550200 2.231 ns/op 0 B/op 0 allocs/op
122+
BenchmarkSomeInt/Generic-8 321369986 3.491 ns/op 0 B/op 0 allocs/op
123+
BenchmarkSomeInt/GenericPtr-8 64221356 16.03 ns/op 8 B/op 1 allocs/op
124+
BenchmarkSomeInt/GenericSlice-8 71858188 16.53 ns/op 8 B/op 1 allocs/op
125+
```
126+
127+
### Non-empty struct type (init + Get)
128+
129+
```
130+
BenchmarkSomeStruct/Typed-8 358631294 3.407 ns/op 0 B/op 0 allocs/op
131+
BenchmarkSomeStruct/Generic-8 241312274 4.978 ns/op 0 B/op 0 allocs/op
132+
BenchmarkSomeStruct/GenericPtr-8 32534370 33.28 ns/op 24 B/op 1 allocs/op
133+
BenchmarkSomeStruct/GenericSlice-8 34119435 33.08 ns/op 24 B/op 1 allocs/op
134+
```
135+
136+
At this point we can see already that alternatives (based on pointer and slice) require allocations while go-option doesn't.
137+
138+
Now let's see encoding and decoding.
139+
140+
### Non-empty base type (encoding + decoding)
141+
142+
```
143+
BenchmarkEncodeDecodeInt/Typed-8 46089481 22.66 ns/op 0 B/op 0 allocs/op
144+
BenchmarkEncodeDecodeInt/Generic-8 10070619 119.6 ns/op 32 B/op 2 allocs/op
145+
BenchmarkEncodeDecodeInt/GenericPtr-8 20202076 58.14 ns/op 16 B/op 2 allocs/op
146+
BenchmarkEncodeDecodeInt/GenericSlice-8 17400481 66.24 ns/op 24 B/op 3 allocs/op
147+
```
148+
149+
As we can see generic implementation ~3-4 times slower than typed one, and even 2 times slower than the generic alternatives. That's why we need predefined optionals at least for base types.
150+
151+
### Non-empty struct type (encoding + decoding)
152+
153+
```
154+
BenchmarkEncodeDecodeStruct/Typed-8 12816339 90.85 ns/op 3 B/op 1 allocs/op
155+
BenchmarkEncodeDecodeStruct/Generic-8 2304001 532.5 ns/op 67 B/op 3 allocs/op
156+
BenchmarkEncodeDecodeStruct/GenericPtr-8 2071520 570.2 ns/op 75 B/op 4 allocs/op
157+
BenchmarkEncodeDecodeStruct/GenericSlice-8 2007445 587.4 ns/op 99 B/op 5 allocs/op
158+
```
159+
160+
161+
96162
[godoc-badge]: https://pkg.go.dev/badge/github.com/tarantool/go-option.svg
97163
[godoc-url]: https://pkg.go.dev/github.com/tarantool/go-option
98164
[actions-badge]: https://github.com/tarantool/go-option/actions/workflows/testing.yaml/badge.svg

0 commit comments

Comments
 (0)