-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_calls.go
220 lines (180 loc) · 4.27 KB
/
gen_calls.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"fmt"
"github.com/artheranet/arthera-node/logger"
"math"
"math/big"
"sync"
"sync/atomic"
"time"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
type CallsGenerator struct {
tps uint32
chainId *big.Int
ks *keystore.KeyStore
accs []accounts.Account
position uint
state genState
work sync.WaitGroup
done chan struct{}
sync.Mutex
logger.Instance
}
func NewCallsGenerator(cfg *PerfConfig, ks *keystore.KeyStore, name string) *CallsGenerator {
g := &CallsGenerator{
chainId: big.NewInt(cfg.ChainId),
ks: ks,
Instance: logger.New(name),
}
g.state.Log = g.Log
for _, acc := range ks.Accounts() {
if acc.Address == cfg.Payer {
continue
}
if err := ks.Unlock(acc, ""); err != nil {
panic(err)
}
g.accs = append(g.accs, acc)
}
return g
}
func (g *CallsGenerator) Start() <-chan *Transaction {
g.Lock()
defer g.Unlock()
if g.done != nil {
return nil
}
g.done = make(chan struct{})
output := make(chan *Transaction, 100)
g.work.Add(1)
go g.background(output)
return output
}
func (g *CallsGenerator) Stop() {
g.Lock()
defer g.Unlock()
if g.done == nil {
return
}
close(g.done)
g.work.Wait()
g.done = nil
}
func (g *CallsGenerator) getTPS() float64 {
tps := atomic.LoadUint32(&g.tps)
return float64(tps)
}
func (g *CallsGenerator) SetTPS(tps float64) {
x := uint32(math.Ceil(tps))
atomic.StoreUint32(&g.tps, x)
}
func (g *CallsGenerator) background(output chan<- *Transaction) {
defer g.work.Done()
defer close(output)
g.Log.Info("started")
defer g.Log.Info("stopped")
for {
begin := time.Now()
var (
generating time.Duration
sending time.Duration
)
tps := g.getTPS()
for count := tps; count > 0; count-- {
begin := time.Now()
tx := g.Yield()
generating += time.Since(begin)
begin = time.Now()
select {
case output <- tx:
sending += time.Since(begin)
continue
case <-g.done:
return
}
}
spent := time.Since(begin)
if spent >= time.Second {
g.Log.Debug("exceeded performance", "tps", tps, "generating", generating, "sending", sending)
continue
}
select {
case <-time.After(time.Second - spent):
continue
case <-g.done:
return
}
}
}
func (g *CallsGenerator) Yield() *Transaction {
if !g.state.IsReady(g.done) {
return nil
}
tx := g.generate(g.position, &g.state)
g.Log.Debug("generated tx", "position", g.position, "dsc", tx.Dsc)
g.position++
return tx
}
func (g *CallsGenerator) generate(position uint, state *genState) *Transaction {
count := uint(len(g.accs))
ballotAddr, _ := state.Session.(*common.Address)
var (
maker TxMaker
callback TxCallback
dsc string
)
switch step := (position % 100001); {
case step == 0:
dsc = "ballot contract creation"
acc := g.accs[position%count]
maker = g.ballotCreateContract(acc)
state.NotReady(dsc)
callback = func(r *types.Receipt, e error) {
// r may be nil, then panic
state.Session = &r.ContractAddress
state.Ready()
}
case 0 < step && step < 100000 && step%2 == 0:
acc := g.accs[(position/2)%count]
chose := ballotRandChose()
dsc = fmt.Sprintf("%s voites for %d", acc.Address.String(), chose)
maker = g.ballotVoite(acc, *ballotAddr, chose)
break
case 0 < step && step < 100000 && step%2 == 1:
//acc := g.accs[(position/2)%count]
//dsc = fmt.Sprintf("count voite logs of %s", acc.Address.String())
//maker = g.ballotCountOfVoites(acc, *ballotAddr)
dsc = "ballot logs reading"
maker = g.ballotLogs(*ballotAddr)
break
case step == 100000:
dsc = "ballot winner reading"
maker = g.ballotWinner(*ballotAddr)
default:
panic(fmt.Sprintf("unknown step %d", step))
}
return &Transaction{
Make: maker,
Callback: callback,
Dsc: dsc,
}
}
func (g *CallsGenerator) Payer(from accounts.Account, amounts ...*big.Int) *bind.TransactOpts {
t, err := bind.NewKeyStoreTransactorWithChainID(g.ks, from, g.chainId)
if err != nil {
panic(err)
}
t.Value = big.NewInt(0)
for _, amount := range amounts {
t.Value.Add(t.Value, amount)
}
return t
}
func (g *CallsGenerator) ReadOnly() *bind.CallOpts {
return &bind.CallOpts{}
}