]> Sergey Matveev's repositories - btrtrc.git/commitdiff
bencode: Add benchmark for krpc.Msg
authorMatt Joiner <anacrolix@gmail.com>
Mon, 23 Jul 2018 00:32:19 +0000 (10:32 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 23 Jul 2018 00:32:19 +0000 (10:32 +1000)
bencode/bench_test.go [new file with mode: 0644]

diff --git a/bencode/bench_test.go b/bencode/bench_test.go
new file mode 100644 (file)
index 0000000..c0b5224
--- /dev/null
@@ -0,0 +1,45 @@
+package bencode_test
+
+import (
+       "net"
+       "reflect"
+       "testing"
+
+       "github.com/anacrolix/dht/krpc"
+       "github.com/anacrolix/torrent/bencode"
+       "github.com/bradfitz/iter"
+)
+
+func marshalAndUnmarshal(tb testing.TB, orig krpc.Msg) (ret krpc.Msg) {
+       b, err := bencode.Marshal(orig)
+       if err != nil {
+               tb.Fatal(err)
+       }
+       err = bencode.Unmarshal(b, &ret)
+       if err != nil {
+               tb.Fatal(err)
+       }
+       // ret.Q = "what"
+       return
+}
+
+func BenchmarkMarshalThenUnmarshalKrpcMsg(tb *testing.B) {
+       orig := krpc.Msg{
+               T: "420",
+               Y: "r",
+               R: &krpc.Return{
+                       Token: "re-up",
+               },
+               IP:       krpc.NodeAddr{IP: net.ParseIP("1.2.3.4"), Port: 1337},
+               ReadOnly: true,
+       }
+       first := marshalAndUnmarshal(tb, orig)
+       if !reflect.DeepEqual(orig, first) {
+               tb.Fail()
+       }
+       tb.ReportAllocs()
+       tb.ResetTimer()
+       for range iter.N(tb.N) {
+               marshalAndUnmarshal(tb, orig)
+       }
+}