-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.go
116 lines (104 loc) · 2.37 KB
/
main.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"encoding/binary"
"encoding/gob"
"log"
"math/rand"
"time"
"github.com/anthdm/hbbft"
)
func main() {
benchmark(4, 128, 100)
benchmark(6, 128, 200)
benchmark(8, 128, 400)
benchmark(12, 128, 1000)
}
type message struct {
from uint64
payload hbbft.MessageTuple
}
func benchmark(n, txsize, batchSize int) {
log.Printf("Starting benchmark %d nodes %d tx size %d batch size over 5 seconds...", n, txsize, batchSize)
var (
nodes = makeNodes(n, 10000, txsize, batchSize)
messages = make(chan message, 1024*1024)
)
for _, node := range nodes {
if err := node.Start(); err != nil {
log.Fatal(err)
}
for _, msg := range node.Messages() {
messages <- message{node.ID, msg}
}
}
timer := time.After(5 * time.Second)
running:
for {
select {
case messag := <-messages:
node := nodes[messag.payload.To]
hbmsg := messag.payload.Payload.(hbbft.HBMessage)
if err := node.HandleMessage(messag.from, hbmsg.Epoch, hbmsg.Payload.(*hbbft.ACSMessage)); err != nil {
log.Fatal(err)
}
for _, msg := range node.Messages() {
messages <- message{node.ID, msg}
}
case <-timer:
for _, node := range nodes {
total := 0
for _, txx := range node.Outputs() {
total += len(txx)
}
log.Printf("node (%d) processed a total of (%d) transactions in 5 seconds [ %d tx/s ]",
node.ID, total, total/5)
}
break running
default:
}
}
}
func makeNodes(n, ntx, txsize, batchSize int) []*hbbft.HoneyBadger {
nodes := make([]*hbbft.HoneyBadger, n)
for i := 0; i < n; i++ {
cfg := hbbft.Config{
N: n,
ID: uint64(i),
Nodes: makeids(n),
BatchSize: batchSize,
}
nodes[i] = hbbft.NewHoneyBadger(cfg)
for ii := 0; ii < ntx; ii++ {
nodes[i].AddTransaction(newTx(txsize))
}
}
return nodes
}
func makeids(n int) []uint64 {
ids := make([]uint64, n)
for i := 0; i < n; i++ {
ids[i] = uint64(i)
}
return ids
}
type tx struct {
Nonce uint64
Data []byte
}
// Size can be used to simulate large transactions in the network.
func newTx(size int) *tx {
return &tx{
Nonce: rand.Uint64(),
Data: make([]byte, size),
}
}
// Hash implements the hbbft.Transaction interface.
func (t *tx) Hash() []byte {
buf := make([]byte, 8) // sizeOf(uint64) + len(data)
binary.LittleEndian.PutUint64(buf, t.Nonce)
return buf
}
func init() {
rand.Seed(time.Now().UnixNano())
gob.Register(&tx{})
}