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