-
Notifications
You must be signed in to change notification settings - Fork 199
/
testStorage.go
288 lines (245 loc) · 6.9 KB
/
testStorage.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package integrationTests
import (
"bytes"
"crypto/rand"
"fmt"
"math/big"
"runtime"
"sync"
"sync/atomic"
"time"
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-go/storage"
"github.com/multiversx/mx-chain-go/storage/cache"
"github.com/multiversx/mx-chain-go/storage/database"
"github.com/multiversx/mx-chain-go/storage/storageunit"
)
const batchDelaySeconds = 10
const maxBatchSize = 30000
const maxOpenFiles = 10
// TestStorage represents a container type of class used in integration tests for storage
type TestStorage struct {
rcvAddr []byte
sndAddr []byte
sig []byte
nbTxs int
mapRemovedKeys *sync.Map
maxWritten *uint64
}
// NewTestStorage return an object of type TestStorage
func NewTestStorage() *TestStorage {
testStorage := new(TestStorage)
testStorage.rcvAddr = make([]byte, 32)
testStorage.sndAddr = make([]byte, 32)
testStorage.sig = make([]byte, 64)
_, _ = rand.Reader.Read(testStorage.rcvAddr)
_, _ = rand.Reader.Read(testStorage.sndAddr)
_, _ = rand.Reader.Read(testStorage.sig)
return testStorage
}
// InitAdditionalFieldsForStorageOperations init additional structure fields to can do storage operations
func (ts *TestStorage) InitAdditionalFieldsForStorageOperations(nbTxsWrite int, mapRemovedKeys *sync.Map, maxWritten *uint64) {
if ts == nil {
return
}
ts.nbTxs = nbTxsWrite
ts.mapRemovedKeys = mapRemovedKeys
ts.maxWritten = maxWritten
}
// CreateStoredData creates stored data
func (ts *TestStorage) CreateStoredData(nonce uint64) ([]byte, []byte) {
tx := &transaction.Transaction{
Nonce: nonce,
GasLimit: 0,
GasPrice: 0,
RcvAddr: ts.rcvAddr,
SndAddr: ts.sndAddr,
Signature: ts.sig,
Value: big.NewInt(1),
}
txBuff, _ := TestMarshalizer.Marshal(tx)
txHash := TestHasher.Compute(string(txBuff))
return txHash, txBuff
}
// CreateStorageLevelDB creates a storage levelDB
func (ts *TestStorage) CreateStorageLevelDB() storage.Storer {
db, _ := database.NewLevelDB("Transactions", batchDelaySeconds, maxBatchSize, maxOpenFiles)
cacher, _ := cache.NewLRUCache(50000)
store, _ := storageunit.NewStorageUnit(
cacher,
db,
)
return store
}
// CreateStorageLevelDBSerial creates a storage levelDB serial
func (ts *TestStorage) CreateStorageLevelDBSerial() storage.Storer {
db, _ := database.NewSerialDB("Transactions", batchDelaySeconds, maxBatchSize, maxOpenFiles)
cacher, _ := cache.NewLRUCache(50000)
store, _ := storageunit.NewStorageUnit(
cacher,
db,
)
return store
}
// WriteMultipleWithNotif write multiple data in storage without notification
func (ts *TestStorage) WriteMultipleWithNotif(
store storage.Storer,
wg *sync.WaitGroup,
chWriteDone chan struct{},
nbNotifDone int,
errors *int32,
) {
defer wg.Done()
written := 10000
initTime := time.Now()
startTime := time.Now()
for counter := 1; counter <= ts.nbTxs; counter++ {
if counter%written == 0 {
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
endTime := time.Now()
diff := endTime.Sub(startTime)
cumul := endTime.Sub(initTime)
writesPerSecond := float64(counter) / cumul.Seconds()
fmt.Printf("Written %d, total %d in %f s\nCumulativeWriteTime %f writes/s:%f\n",
written,
counter,
diff.Seconds(),
cumul.Seconds(),
writesPerSecond)
fmt.Printf("timestamp: %d, num go: %d, go mem: %s, sys mem: %s, total mem: %s, num GC: %d\n",
time.Now().Unix(),
runtime.NumGoroutine(),
core.ConvertBytes(memStats.Alloc),
core.ConvertBytes(memStats.Sys),
core.ConvertBytes(memStats.TotalAlloc),
memStats.NumGC,
)
startTime = time.Now()
}
key, val := ts.CreateStoredData(uint64(counter))
errPut := store.Put(key, val)
if errPut != nil {
fmt.Println(errPut.Error())
atomic.AddInt32(errors, 1)
return
}
atomic.StoreUint64(ts.maxWritten, uint64(counter))
}
fmt.Println("Done Writing!")
for i := 0; i < nbNotifDone; i++ {
chWriteDone <- struct{}{}
}
}
// RemoveMultiple remove multiple data from storage
func (ts *TestStorage) RemoveMultiple(
store storage.Storer,
wg *sync.WaitGroup,
chEndRemove chan struct{},
errors *int32,
) {
defer wg.Done()
for {
select {
case <-chEndRemove:
fmt.Println("Done Removing!")
return
case <-time.After(time.Millisecond * 100):
//remove happen less often than writes
}
if atomic.LoadUint64(ts.maxWritten) == 0 {
//not written yet
continue
}
maxWrittenUint64 := atomic.LoadUint64(ts.maxWritten)
maxWrittenBigInt := big.NewInt(0).SetUint64(maxWrittenUint64)
existingNonce, _ := rand.Int(rand.Reader, maxWrittenBigInt)
key, _ := ts.CreateStoredData(existingNonce.Uint64())
ts.mapRemovedKeys.Store(string(key), struct{}{})
errRemove := store.Remove(key)
if errRemove != nil {
fmt.Println(errRemove.Error())
atomic.AddInt32(errors, 1)
return
}
if maxWrittenUint64 == uint64(ts.nbTxs) {
fmt.Println("Done Removing!")
return
}
}
}
// ReadMultiple read multiple data from storage
func (ts *TestStorage) ReadMultiple(
store storage.Storer,
wg *sync.WaitGroup,
chStartTrigger chan struct{},
errors *int32,
) {
defer wg.Done()
<-chStartTrigger
read := uint64(10000)
initTime := time.Now()
startTime := time.Now()
maxRoutines := make(chan struct{}, 5000)
actualRead := uint64(0)
wgRead := &sync.WaitGroup{}
wgRead.Add(ts.nbTxs)
for cnt := 1; cnt <= ts.nbTxs; cnt++ {
maxRoutines <- struct{}{}
go func(count uint64) {
defer func() {
<-maxRoutines
wgRead.Done()
}()
var maxWrittenUint64 uint64
for {
maxWrittenUint64 = atomic.LoadUint64(ts.maxWritten)
if count <= maxWrittenUint64 {
break
}
<-time.After(time.Microsecond)
}
key, val := ts.CreateStoredData(count)
v, errGet := store.Get(key)
_, ok := ts.mapRemovedKeys.Load(string(key))
if !ok && errGet != nil {
fmt.Printf("Not getting tx with nonce %d\n", count)
atomic.AddInt32(errors, 1)
return
}
if !ok && !bytes.Equal(val, v) {
fmt.Printf("Not equal values with nonce %d\n", count)
atomic.AddInt32(errors, 1)
return
}
aRead := atomic.AddUint64(&actualRead, 1)
if aRead%read == 0 {
var memStats runtime.MemStats
runtime.ReadMemStats(&memStats)
endTime := time.Now()
diff := endTime.Sub(startTime)
cumul := endTime.Sub(initTime)
readsPerSecond := float64(aRead) / cumul.Seconds()
fmt.Printf("Read %d, total %d in %f s\nCumulativeReadTime %f reads/s %f\n",
read,
aRead,
diff.Seconds(),
cumul.Seconds(),
readsPerSecond,
)
fmt.Printf("timestamp: %d, num go: %d, go mem: %s, sys mem: %s, total mem: %s, num GC: %d\n",
time.Now().Unix(),
runtime.NumGoroutine(),
core.ConvertBytes(memStats.Alloc),
core.ConvertBytes(memStats.Sys),
core.ConvertBytes(memStats.TotalAlloc),
memStats.NumGC,
)
startTime = time.Now()
}
}(uint64(cnt))
}
wgRead.Wait()
fmt.Println("Done Reading!")
}