2018-07-23 08:32:19 +08:00
|
|
|
package bencode_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2019-03-19 17:43:51 +08:00
|
|
|
"github.com/anacrolix/dht/v2/krpc"
|
2019-08-21 18:58:40 +08:00
|
|
|
"github.com/anacrolix/torrent/bencode"
|
2018-07-23 08:32:19 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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{
|
2019-01-22 05:53:42 +08:00
|
|
|
Token: func() *string { t := "re-up"; return &t }(),
|
2018-07-23 08:32:19 +08:00
|
|
|
},
|
|
|
|
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()
|
2021-09-14 11:46:50 +08:00
|
|
|
for i := 0; i < tb.N; i += 1 {
|
2018-07-23 08:32:19 +08:00
|
|
|
marshalAndUnmarshal(tb, orig)
|
|
|
|
}
|
|
|
|
}
|