]> Sergey Matveev's repositories - btrtrc.git/blob - bencode/bench_test.go
Fix go:build directives
[btrtrc.git] / bencode / bench_test.go
1 package bencode_test
2
3 import (
4         "net"
5         "reflect"
6         "testing"
7
8         "github.com/anacrolix/dht/v2/krpc"
9         "github.com/bradfitz/iter"
10
11         "github.com/anacrolix/torrent/bencode"
12 )
13
14 func marshalAndUnmarshal(tb testing.TB, orig krpc.Msg) (ret krpc.Msg) {
15         b, err := bencode.Marshal(orig)
16         if err != nil {
17                 tb.Fatal(err)
18         }
19         err = bencode.Unmarshal(b, &ret)
20         if err != nil {
21                 tb.Fatal(err)
22         }
23         // ret.Q = "what"
24         return
25 }
26
27 func BenchmarkMarshalThenUnmarshalKrpcMsg(tb *testing.B) {
28         orig := krpc.Msg{
29                 T: "420",
30                 Y: "r",
31                 R: &krpc.Return{
32                         Token: func() *string { t := "re-up"; return &t }(),
33                 },
34                 IP:       krpc.NodeAddr{IP: net.ParseIP("1.2.3.4"), Port: 1337},
35                 ReadOnly: true,
36         }
37         first := marshalAndUnmarshal(tb, orig)
38         if !reflect.DeepEqual(orig, first) {
39                 tb.Fail()
40         }
41         tb.ReportAllocs()
42         tb.ResetTimer()
43         for range iter.N(tb.N) {
44                 marshalAndUnmarshal(tb, orig)
45         }
46 }