forked from lightningnetwork/lightning-onion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_test.go
93 lines (76 loc) · 1.69 KB
/
bench_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package sphinx
import (
"bytes"
"testing"
"github.com/btcsuite/btcd/btcec"
)
var (
s *OnionPacket
p *ProcessedPacket
)
func BenchmarkPathPacketConstruction(b *testing.B) {
b.StopTimer()
var (
err error
sphinxPacket *OnionPacket
route PaymentPath
)
for i := 0; i < NumMaxHops; i++ {
privKey, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
b.Fatalf("unable to generate key: %v", privKey)
}
hopData := HopData{
Realm: [1]byte{0x00},
ForwardAmount: uint64(i),
OutgoingCltv: uint32(i),
}
copy(hopData.NextAddress[:], bytes.Repeat([]byte{byte(i)}, 8))
route[i] = OnionHop{
NodePub: *privKey.PubKey(),
HopData: hopData,
}
}
d, _ := btcec.PrivKeyFromBytes(btcec.S256(), bytes.Repeat([]byte{'A'}, 32))
b.ReportAllocs()
b.StartTimer()
for i := 0; i < b.N; i++ {
sphinxPacket, err = NewOnionPacket(&route, d, nil)
if err != nil {
b.Fatalf("unable to create packet: %v", err)
}
}
s = sphinxPacket
}
func BenchmarkProcessPacket(b *testing.B) {
b.StopTimer()
path, _, _, sphinxPacket, err := newTestRoute(1)
if err != nil {
b.Fatalf("unable to create test route: %v", err)
}
b.ReportAllocs()
path[0].log.Start()
defer path[0].log.Stop()
b.StartTimer()
var (
pkt *ProcessedPacket
)
for i := 0; i < b.N; i++ {
pkt, err = path[0].ProcessOnionPacket(sphinxPacket, nil, uint32(i))
if err != nil {
b.Fatalf("unable to process packet %d: %v", i, err)
}
b.StopTimer()
router := path[0]
router.log.Stop()
path[0] = &Router{
nodeID: router.nodeID,
nodeAddr: router.nodeAddr,
onionKey: router.onionKey,
log: NewMemoryReplayLog(),
}
path[0].log.Start()
b.StartTimer()
}
p = pkt
}