]> Sergey Matveev's repositories - btrtrc.git/blob - bencode/bench_test.go
d6faafb76a124989f40abd52b26ecf1b453118d4
[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/anacrolix/torrent/bencode"
10 )
11
12 func marshalAndUnmarshal(tb testing.TB, orig krpc.Msg) (ret krpc.Msg) {
13         b, err := bencode.Marshal(orig)
14         if err != nil {
15                 tb.Fatal(err)
16         }
17         err = bencode.Unmarshal(b, &ret)
18         if err != nil {
19                 tb.Fatal(err)
20         }
21         // ret.Q = "what"
22         return
23 }
24
25 func BenchmarkMarshalThenUnmarshalKrpcMsg(tb *testing.B) {
26         orig := krpc.Msg{
27                 T: "420",
28                 Y: "r",
29                 R: &krpc.Return{
30                         Token: func() *string { t := "re-up"; return &t }(),
31                 },
32                 IP:       krpc.NodeAddr{IP: net.ParseIP("1.2.3.4"), Port: 1337},
33                 ReadOnly: true,
34         }
35         first := marshalAndUnmarshal(tb, orig)
36         if !reflect.DeepEqual(orig, first) {
37                 tb.Fail()
38         }
39         tb.ReportAllocs()
40         tb.ResetTimer()
41         for i := 0; i < tb.N; i += 1 {
42                 marshalAndUnmarshal(tb, orig)
43         }
44 }