Skip to content

Commit 8d09bc5

Browse files
committed
add benchmarks
1 parent 7610518 commit 8d09bc5

22 files changed

+2048
-20
lines changed

README.md

+84-20
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,98 @@ fmt.Println(string(data))
3535
data, err := bencode.Unmarshal(value)
3636
```
3737

38-
## Performance [benchmarks](https://github.com/IncSW/go-bencode/tree/benchmarks/benchmarks)
38+
## Performance [benchmarks](https://github.com/IncSW/go-bencode/tree/master/benchmarks)
3939

4040
### Go 1.16, Debian 9.1, i7-7700
4141

4242
### Marshal
4343

44-
| Library | Time | Bytes Allocated | Objects Allocated |
45-
| :--------------------------- | :---------: | :-------------: | :---------------: |
46-
| IncSW/go-bencode [MarshalTo] | 581.0 ns/op | 112 B/op | 2 allocs/op |
47-
| IncSW/go-bencode [Marshal] | 662.6 ns/op | 624 B/op | 3 allocs/op |
48-
| marksamman/bencode | 820.3 ns/op | 384 B/op | 8 allocs/op |
49-
| cristalhq/bencode | 994.2 ns/op | 928 B/op | 4 allocs/op |
50-
| aleksatr/go-bencode | 1061 ns/op | 736 B/op | 9 allocs/op |
51-
| nabilanam/bencode | 2103 ns/op | 1192 B/op | 44 allocs/op |
52-
| jackpal/bencode-go | 4676 ns/op | 2016 B/op | 45 allocs/op |
53-
| zeebo/bencode | 4889 ns/op | 1376 B/op | 33 allocs/op |
44+
| Library | Time | Bytes Allocated | Objects Allocated | Notes |
45+
| :------------------ | :---------: | :-------------: | :---------------: | :---: |
46+
| IncSW/go-bencode | 662.6 ns/op | 624 B/op | 3 allocs/op | |
47+
| cristalhq/bencode | 939.4 ns/op | 928 B/op | 4 allocs/op | |
48+
| marksamman/bencode | 1002 ns/op | 736 B/op | 9 allocs/op | 4 |
49+
| aleksatr/go-bencode | 1060 ns/op | 736 B/op | 9 allocs/op | |
50+
| chihaya/chihaya | 1722 ns/op | 1009 B/op | 53 allocs/op | 1 |
51+
| lajide/bencode | 1725 ns/op | 1011 B/op | 53 allocs/op | 1 |
52+
| nabilanam/bencode | 2865 ns/op | 3192 B/op | 54 allocs/op | 5 |
53+
| anacrolix/torrent | 3179 ns/op | 1328 B/op | 25 allocs/op | |
54+
| lwch/bencode | 3340 ns/op | 1792 B/op | 75 allocs/op | 1, 2 |
55+
| tumdum/bencoding | 3419 ns/op | 1752 B/op | 60 allocs/op | |
56+
| stints/bencode | 4018 ns/op | 3120 B/op | 100 allocs/op | 1, 2 |
57+
| ehmry/go-bencode | 4569 ns/op | 1496 B/op | 33 allocs/op | |
58+
| jackpal/bencode-go | 4702 ns/op | 2016 B/op | 45 allocs/op | |
59+
| zeebo/bencode | 5003 ns/op | 1376 B/op | 33 allocs/op | |
60+
| owenliang/dht | 5180 ns/op | 3279 B/op | 80 allocs/op | 5 |
61+
| cuberat/go-bencode | 5589 ns/op | 1929 B/op | 71 allocs/op | 2 |
62+
63+
### MarshalTo
64+
65+
| Library | Time | Bytes Allocated | Objects Allocated | Notes |
66+
| :----------------- | :---------: | :-------------: | :---------------: | :---: |
67+
| IncSW/go-bencode | 581.0 ns/op | 112 B/op | 2 allocs/op | |
68+
| cristalhq/bencode | 668.4 ns/op | 0 B/op | 0 allocs/op | |
69+
| chihaya/chihaya | 1432 ns/op | 307 B/op | 49 allocs/op | 1 |
70+
| lajide/bencode | 1462 ns/op | 307 B/op | 49 allocs/op | 1 |
71+
| anacrolix/torrent | 2954 ns/op | 720 B/op | 21 allocs/op | |
72+
| lwch/bencode | 3093 ns/op | 1089 B/op | 71 allocs/op | 1, 2 |
73+
| tumdum/bencoding | 3474 ns/op | 1752 B/op | 60 allocs/op | |
74+
| jackpal/bencode-go | 4479 ns/op | 1408 B/op | 41 allocs/op | |
75+
| ehmry/go-bencode | 4650 ns/op | 1528 B/op | 33 allocs/op | |
76+
| cuberat/go-bencode | 5360 ns/op | 1321 B/op | 67 allocs/op | 2 |
5477

5578
### Unmarshal
5679

57-
| Library | Time | Bytes Allocated | Objects Allocated |
58-
| :------------------ | :--------: | :-------------: | :---------------: |
59-
| IncSW/go-bencode | 1001 ns/op | 960 B/op | 18 allocs/op |
60-
| cristalhq/bencode | 1160 ns/op | 960 B/op | 18 allocs/op |
61-
| nabilanam/bencode | 1379 ns/op | 1240 B/op | 39 allocs/op |
62-
| aleksatr/go-bencode | 2270 ns/op | 1816 B/op | 51 allocs/op |
63-
| jackpal/bencode-go | 2577 ns/op | 1688 B/op | 59 allocs/op |
64-
| marksamman/bencode | 2725 ns/op | 5768 B/op | 54 allocs/op |
65-
| zeebo/bencode | 5988 ns/op | 6392 B/op | 92 allocs/op |
80+
| Library | Time | Bytes Allocated | Objects Allocated | Notes |
81+
| :------------------ | :---------: | :-------------: | :---------------: | :---: |
82+
| IncSW/go-bencode | 991.5 ns/op | 960 B/op | 18 allocs/op | |
83+
| cristalhq/bencode | 1160 ns/op | 960 B/op | 18 allocs/op | |
84+
| nabilanam/bencode | 1379 ns/op | 1240 B/op | 39 allocs/op | |
85+
| owenliang/dht | 1702 ns/op | 1352 B/op | 46 allocs/op | |
86+
| aleksatr/go-bencode | 2279 ns/op | 1816 B/op | 51 allocs/op | |
87+
| jackpal/bencode-go | 2597 ns/op | 1688 B/op | 59 allocs/op | |
88+
| marksamman/bencode | 2758 ns/op | 5768 B/op | 54 allocs/op | |
89+
| ehmry/go-bencode | 2865 ns/op | 2064 B/op | 41 allocs/op | |
90+
| chihaya/chihaya | 2961 ns/op | 5880 B/op | 61 allocs/op | |
91+
| lajide/bencode | 2973 ns/op | 5880 B/op | 61 allocs/op | |
92+
| anacrolix/torrent | 3723 ns/op | 2456 B/op | 62 allocs/op | |
93+
| cuberat/go-bencode | 4687 ns/op | 6544 B/op | 119 allocs/op | |
94+
| zeebo/bencode | 5954 ns/op | 6376 B/op | 91 allocs/op | |
95+
| tumdum/bencoding | 7891 ns/op | 6568 B/op | 157 allocs/op | |
96+
| lwch/bencode | - | - | - | 3 |
97+
| stints/bencode | - | - | - | 6 |
98+
99+
### RealWorld [ubuntu-21.04-desktop-amd64.iso.torrent](https://releases.ubuntu.com/21.04/ubuntu-21.04-desktop-amd64.iso.torrent)
100+
101+
| Library | Time | Bytes Allocated | Objects Allocated |
102+
| :---------------------------- | :---------: | :-------------: | :---------------: |
103+
| IncSW/go-bencode Unmarshal | 1279 ns/op | 1016 B/op | 21 allocs/op |
104+
| IncSW/go-bencode Marshal | 28572 ns/op | 262816 B/op | 4 allocs/op |
105+
| IncSW/go-bencode MarshalTo | 7800 ns/op | 160 B/op | 2 allocs/op |
106+
| |
107+
| |
108+
| cristalhq/bencode Unmarshal | 1560 ns/op | 1016 B/op | 21 allocs/op |
109+
| cristalhq/bencode Marshal | 49125 ns/op | 443168 B/op | 5 allocs/op |
110+
| cristalhq/bencode MarshalTo | 7709 ns/op | 0 B/op | 0 allocs/op |
111+
| |
112+
| |
113+
| aleksatr/go-bencode Unmarshal | 21615 ns/op | 223352 B/op | 69 allocs/op |
114+
| aleksatr/go-bencode Marshal | 26401 ns/op | 222689 B/op | 13 allocs/op |
115+
| |
116+
| |
117+
| jackpal/bencode-go Unmarshal | 48384 ns/op | 444484 B/op | 81 allocs/op |
118+
| jackpal/bencode-go Marshal | 55097 ns/op | 445694 B/op | 60 allocs/op |
119+
| jackpal/bencode-go MarshalTo | 35800 ns/op | 223128 B/op | 54 allocs/op |
120+
121+
122+
#### Notes
123+
124+
1. BUG: Keys must be strings and appear in sorted order (sorted as raw strings, not alphanumerics). http://bittorrent.org/beps/bep_0003.html#bencoding
125+
2. BUG: []byte encoding as a list
126+
3. BUG: not supported list in list
127+
4. WARN: ignoring unsupported types without errors
128+
5. INFO: just 4 types supported
129+
6. INFO: files only
66130

67131
## License
68132

benchmarks/aleksatr_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package benchmarks
2+
3+
import (
4+
"testing"
5+
6+
bencode "github.com/aleksatr/go-bencode"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func Benchmark_Aleksatr_Marshal(b *testing.B) {
11+
b.ReportAllocs()
12+
for n := 0; n < b.N; n++ {
13+
buffer, err = bencode.Encode(bytesInt64TestData)
14+
if err != nil {
15+
b.Fatal(err)
16+
}
17+
}
18+
b.StopTimer()
19+
assert.Equal(b, string(unmarshalTestData), string(buffer))
20+
}
21+
22+
func Benchmark_Aleksatr_Unmarshal(b *testing.B) {
23+
b.ReportAllocs()
24+
for n := 0; n < b.N; n++ {
25+
torrent, err = bencode.Decode(unmarshalTestData)
26+
if err != nil {
27+
b.Fatal(err)
28+
}
29+
}
30+
b.StopTimer()
31+
assert.Equal(b, stringInt64TestData, torrent)
32+
}
33+
34+
func Benchmark_Aleksatr_RealWorld(b *testing.B) {
35+
b.ReportAllocs()
36+
b.Run("Unmarshal", func(b *testing.B) {
37+
for n := 0; n < b.N; n++ {
38+
torrent, err = bencode.Decode(realWorldData)
39+
if err != nil {
40+
b.Fatal(err)
41+
}
42+
}
43+
b.StopTimer()
44+
})
45+
b.Run("Marshal", func(b *testing.B) {
46+
for n := 0; n < b.N; n++ {
47+
buffer, err = bencode.Encode(torrent)
48+
if err != nil {
49+
b.Fatal(err)
50+
}
51+
}
52+
})
53+
assert.Equal(b, string(realWorldData), string(buffer))
54+
}

benchmarks/anacrolix_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package benchmarks
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/anacrolix/torrent/bencode"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func Benchmark_Anacrolix_Marshal(b *testing.B) {
12+
b.ReportAllocs()
13+
for n := 0; n < b.N; n++ {
14+
buffer, err = bencode.Marshal(bytesInt64TestData)
15+
if err != nil {
16+
b.Fatal(err)
17+
}
18+
}
19+
b.StopTimer()
20+
assert.Equal(b, string(unmarshalTestData), string(buffer))
21+
}
22+
23+
func Benchmark_Anacrolix_MarshalTo(b *testing.B) {
24+
b.ReportAllocs()
25+
bytesBuffer = bytes.NewBuffer(make([]byte, 0, 512))
26+
b.ResetTimer()
27+
for n := 0; n < b.N; n++ {
28+
bytesBuffer.Reset()
29+
err = bencode.NewEncoder(bytesBuffer).Encode(bytesInt64TestData)
30+
if err != nil {
31+
b.Fatal(err)
32+
}
33+
}
34+
b.StopTimer()
35+
assert.Equal(b, string(unmarshalTestData), bytesBuffer.String())
36+
}
37+
38+
func Benchmark_Anacrolix_Unmarshal(b *testing.B) {
39+
b.ReportAllocs()
40+
for n := 0; n < b.N; n++ {
41+
torrent = nil
42+
err = bencode.Unmarshal(unmarshalTestData, &torrent)
43+
if err != nil {
44+
b.Fatal(err)
45+
}
46+
}
47+
b.StopTimer()
48+
assert.Equal(b, stringInt64TestData, torrent)
49+
}

benchmarks/chihaya_test.go

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package benchmarks
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/chihaya/chihaya/frontend/http/bencode"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
var chihayaUnmarshalTestData = bencode.Dict{
12+
"announce": "udp://tracker.publicbt.com:80/announce",
13+
"announce-list": bencode.List{
14+
bencode.List{"udp://tracker.publicbt.com:80/announce"},
15+
bencode.List{"udp://tracker.openbittorrent.com:80/announce"},
16+
},
17+
"comment": "Debian CD from cdimage.debian.org",
18+
"info": bencode.Dict{
19+
"name": "debian-8.8.0-arm64-netinst.iso",
20+
"length": int64(170917888),
21+
"piece length": int64(262144),
22+
},
23+
}
24+
25+
func Benchmark_Chihaya_Marshal(b *testing.B) {
26+
b.ReportAllocs()
27+
for n := 0; n < b.N; n++ {
28+
buffer, err = bencode.Marshal(bytesInt64TestData)
29+
if err != nil {
30+
b.Fatal(err)
31+
}
32+
}
33+
b.StopTimer()
34+
// assert.Equal(b, string(unmarshalTestData), string(buffer))
35+
// BUG: Keys must be strings and appear in sorted order (sorted as raw strings, not alphanumerics). http://bittorrent.org/beps/bep_0003.html#bencoding
36+
}
37+
38+
func Benchmark_Chihaya_MarshalTo(b *testing.B) {
39+
bytesBuffer = bytes.NewBuffer(make([]byte, 0, 512))
40+
b.ResetTimer()
41+
b.ReportAllocs()
42+
for n := 0; n < b.N; n++ {
43+
bytesBuffer.Reset()
44+
err = bencode.NewEncoder(bytesBuffer).Encode(bytesInt64TestData)
45+
if err != nil {
46+
b.Fatal(err)
47+
}
48+
}
49+
b.StopTimer()
50+
// assert.Equal(b, string(unmarshalTestData), bytesBuffer.String())
51+
// BUG: Keys must be strings and appear in sorted order (sorted as raw strings, not alphanumerics). http://bittorrent.org/beps/bep_0003.html#bencoding
52+
}
53+
54+
func Benchmark_Chihaya_Unmarshal(b *testing.B) {
55+
b.ReportAllocs()
56+
for n := 0; n < b.N; n++ {
57+
torrent, err = bencode.Unmarshal(unmarshalTestData)
58+
if err != nil {
59+
b.Fatal(err)
60+
}
61+
}
62+
b.StopTimer()
63+
assert.Equal(b, chihayaUnmarshalTestData, torrent)
64+
}
65+
66+
func Benchmark_Chihaya_RealWorld(b *testing.B) {
67+
b.Skip()
68+
b.ReportAllocs()
69+
b.Run("Unmarshal", func(b *testing.B) {
70+
for n := 0; n < b.N; n++ {
71+
torrent, err = bencode.Unmarshal(realWorldData)
72+
if err != nil {
73+
b.Fatal(err) // ERR: bencode: short read
74+
}
75+
}
76+
})
77+
b.Run("Marshal", func(b *testing.B) {
78+
for n := 0; n < b.N; n++ {
79+
buffer, err = bencode.Marshal(torrent)
80+
if err != nil {
81+
b.Fatal(err)
82+
}
83+
}
84+
})
85+
bytesBuffer = bytes.NewBuffer(buffer)
86+
b.Run("MarshalTo", func(b *testing.B) {
87+
for n := 0; n < b.N; n++ {
88+
bytesBuffer.Reset()
89+
err = bencode.NewEncoder(bytesBuffer).Encode(torrent)
90+
if err != nil {
91+
b.Fatal(err)
92+
}
93+
}
94+
})
95+
}

benchmarks/cristalhq_test.go

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package benchmarks
2+
3+
import (
4+
"testing"
5+
6+
"github.com/cristalhq/bencode"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func Benchmark_Cristalhq_Marshal(b *testing.B) {
11+
b.ReportAllocs()
12+
for n := 0; n < b.N; n++ {
13+
buffer, err = bencode.Marshal(bytesInt64TestData)
14+
if err != nil {
15+
b.Fatal(err)
16+
}
17+
}
18+
b.StopTimer()
19+
assert.Equal(b, string(unmarshalTestData), string(buffer))
20+
}
21+
22+
func Benchmark_Cristalhq_MarshalTo(b *testing.B) {
23+
b.ReportAllocs()
24+
buffer = make([]byte, 0, 512)
25+
b.ResetTimer()
26+
for n := 0; n < b.N; n++ {
27+
buffer, err = bencode.MarshalTo(buffer[:0], bytesInt64TestData)
28+
if err != nil {
29+
b.Fatal(err)
30+
}
31+
}
32+
b.StopTimer()
33+
assert.Equal(b, string(unmarshalTestData), string(buffer))
34+
}
35+
36+
func Benchmark_Cristalhq_Unmarshal(b *testing.B) {
37+
b.ReportAllocs()
38+
for n := 0; n < b.N; n++ {
39+
torrent = nil
40+
err = bencode.Unmarshal(unmarshalTestData, &torrent)
41+
if err != nil {
42+
b.Fatal(err)
43+
}
44+
}
45+
b.StopTimer()
46+
assert.Equal(b, bytesInt64TestData, torrent)
47+
}
48+
49+
func Benchmark_Cristalhq_RealWorld(b *testing.B) {
50+
b.ReportAllocs()
51+
b.Run("Unmarshal", func(b *testing.B) {
52+
for n := 0; n < b.N; n++ {
53+
torrent = nil
54+
err = bencode.Unmarshal(realWorldData, &torrent)
55+
if err != nil {
56+
b.Fatal(err)
57+
}
58+
}
59+
b.StopTimer()
60+
})
61+
b.Run("Marshal", func(b *testing.B) {
62+
for n := 0; n < b.N; n++ {
63+
buffer, err = bencode.Marshal(torrent)
64+
if err != nil {
65+
b.Fatal(err)
66+
}
67+
}
68+
})
69+
b.Run("MarshalTo", func(b *testing.B) {
70+
for n := 0; n < b.N; n++ {
71+
buffer, err = bencode.MarshalTo(buffer[:0], torrent)
72+
if err != nil {
73+
b.Fatal(err)
74+
}
75+
}
76+
})
77+
assert.Equal(b, string(realWorldData), string(buffer))
78+
}

0 commit comments

Comments
 (0)