forked from celestiaorg/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedssser.go
173 lines (147 loc) · 3.64 KB
/
edssser.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
package edssser
import (
"context"
"errors"
"fmt"
"os"
"sync"
"testing"
"time"
"github.com/ipfs/go-datastore"
"github.com/celestiaorg/celestia-app/pkg/da"
"github.com/celestiaorg/celestia-node/share/eds"
"github.com/celestiaorg/celestia-node/share/eds/edstest"
)
type Config struct {
EDSSize int
EDSWrites int
EnableLog bool
LogFilePath string
StatLogFreq int
OpTimeout time.Duration
}
// EDSsser stand for EDS Store Stresser.
type EDSsser struct {
config Config
datastore datastore.Batching
edsstoreMu sync.Mutex
edsstore *eds.Store
statsFileMu sync.Mutex
statsFile *os.File
}
func NewEDSsser(path string, datastore datastore.Batching, cfg Config) (*EDSsser, error) {
storeCfg := eds.DefaultParameters()
edsstore, err := eds.NewStore(storeCfg, path, datastore)
if err != nil {
return nil, err
}
return &EDSsser{
config: cfg,
datastore: datastore,
edsstore: edsstore,
}, nil
}
func (ss *EDSsser) Run(ctx context.Context) (stats Stats, err error) {
ss.edsstoreMu.Lock()
defer ss.edsstoreMu.Unlock()
err = ss.edsstore.Start(ctx)
if err != nil {
return stats, err
}
defer func() {
err = errors.Join(err, ss.edsstore.Stop(ctx))
}()
edsHashes, err := ss.edsstore.List()
if err != nil {
return stats, err
}
fmt.Printf("recovered %d EDSes\n\n", len(edsHashes))
t := &testing.T{}
for toWrite := ss.config.EDSWrites - len(edsHashes); ctx.Err() == nil && toWrite > 0; toWrite-- {
took, err := ss.put(ctx, t)
stats.TotalWritten++
stats.TotalTime += took
if took < stats.MinTime || stats.MinTime == 0 {
stats.MinTime = took
} else if took > stats.MaxTime {
stats.MaxTime = took
}
if ss.config.EnableLog {
if stats.TotalWritten%ss.config.StatLogFreq == 0 {
stats := stats.Finalize()
fmt.Println(stats)
go func() {
err := ss.dumpStat(stats)
if err != nil {
fmt.Printf("error dumping stats: %s\n", err.Error())
}
}()
}
if err != nil {
fmt.Printf("ERROR put: %s, took: %v, at: %v\n", err.Error(), took, time.Now())
continue
}
if took > ss.config.OpTimeout/2 {
fmt.Println("long put", "size", ss.config.EDSSize, "took", took, "at", time.Now())
continue
}
fmt.Println("square written", "size", ss.config.EDSSize, "took", took, "at", time.Now())
}
}
return stats, nil
}
func (ss *EDSsser) dumpStat(stats Stats) (err error) {
ss.statsFileMu.Lock()
defer ss.statsFileMu.Unlock()
ss.statsFile, err = os.Create(ss.config.LogFilePath + "/edssser_stats.txt")
if err != nil {
return err
}
_, err = ss.statsFile.Write([]byte(stats.String()))
if err != nil {
return err
}
return ss.statsFile.Close()
}
type Stats struct {
TotalWritten int
TotalTime, MinTime, MaxTime, AvgTime time.Duration
// Deviation ?
}
func (stats Stats) Finalize() Stats {
if stats.TotalTime != 0 {
stats.AvgTime = stats.TotalTime / time.Duration(stats.TotalWritten)
}
return stats
}
func (stats Stats) String() string {
return fmt.Sprintf(`
TotalWritten %d
TotalWritingTime %v
MaxTime %s
MinTime %s
AvgTime %s
`,
stats.TotalWritten,
stats.TotalTime,
stats.MaxTime,
stats.MinTime,
stats.AvgTime,
)
}
func (ss *EDSsser) put(ctx context.Context, t *testing.T) (time.Duration, error) {
ctx, cancel := context.WithTimeout(ctx, ss.config.OpTimeout)
if ss.config.OpTimeout == 0 {
ctx, cancel = context.WithCancel(ctx)
}
defer cancel()
// divide by 2 to get ODS size as expected by RandEDS
square := edstest.RandEDS(t, ss.config.EDSSize/2)
dah, err := da.NewDataAvailabilityHeader(square)
if err != nil {
return 0, err
}
now := time.Now()
err = ss.edsstore.Put(ctx, dah.Hash(), square)
return time.Since(now), err
}