/
main.go
230 lines (193 loc) · 5.72 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
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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package main
import (
"context"
"flag"
"fmt"
"log"
"log/slog"
"os"
"runtime"
"runtime/pprof"
"time"
"github.com/cilium/hive"
"github.com/cilium/hive/cell"
"github.com/cilium/hive/job"
"github.com/cilium/statedb"
"github.com/cilium/statedb/index"
"github.com/cilium/statedb/reconciler"
)
var logger = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelError,
}))
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
var numObjects = flag.Int("objects", 100000, "number of objects to create")
var batchSize = flag.Int("batchsize", 1000, "batch size for writes")
var incrBatchSize = flag.Int("incrbatchsize", 1000, "maximum batch size for incremental reconciliation")
var quiet = flag.Bool("quiet", false, "quiet output for CI")
type testObject struct {
id uint64
status reconciler.Status
}
func (t *testObject) GetStatus() reconciler.Status {
return t.status
}
func (t *testObject) SetStatus(status reconciler.Status) *testObject {
t.status = status
return t
}
func (t *testObject) Clone() *testObject {
t2 := *t
return &t2
}
type mockOps struct {
}
// Delete implements reconciler.Operations.
func (mt *mockOps) Delete(ctx context.Context, txn statedb.ReadTxn, obj *testObject) error {
return nil
}
// Prune implements reconciler.Operations.
func (mt *mockOps) Prune(ctx context.Context, txn statedb.ReadTxn, iter statedb.Iterator[*testObject]) error {
return nil
}
// Update implements reconciler.Operations.
func (mt *mockOps) Update(ctx context.Context, txn statedb.ReadTxn, obj *testObject, changed *bool) error {
if changed != nil {
*changed = true
}
return nil
}
var _ reconciler.Operations[*testObject] = &mockOps{}
var idIndex = statedb.Index[*testObject, uint64]{
Name: "id",
FromObject: func(t *testObject) index.KeySet {
return index.NewKeySet(index.Uint64(t.id))
},
FromKey: index.Uint64,
Unique: true,
}
func main() {
var memBefore runtime.MemStats
runtime.ReadMemStats(&memBefore)
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
defer f.Close() // error handling omitted for example
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
var (
mt = &mockOps{}
db *statedb.DB
)
testObjects, err := statedb.NewTable[*testObject]("test-objects", idIndex)
if err != nil {
panic(err)
}
hive := hive.NewWithOptions(
hive.Options{Logger: logger},
cell.SimpleHealthCell,
statedb.Cell,
job.Cell,
cell.Module(
"test",
"Test",
cell.Invoke(func(db_ *statedb.DB) error {
db = db_
return db.RegisterTable(testObjects)
}),
cell.Provide(
func() (*mockOps, reconciler.Operations[*testObject]) {
return mt, mt
},
),
cell.Provide(func() reconciler.Config[*testObject] {
return reconciler.Config[*testObject]{
Table: testObjects,
// Don't run the full reconciliation via timer, but rather explicitly so that the full
// reconciliation operations don't mix with incremental when not expected.
FullReconcilationInterval: time.Hour,
RetryBackoffMinDuration: time.Millisecond,
RetryBackoffMaxDuration: 10 * time.Millisecond,
IncrementalRoundSize: *incrBatchSize,
GetObjectStatus: (*testObject).GetStatus,
SetObjectStatus: (*testObject).SetStatus,
CloneObject: (*testObject).Clone,
Operations: mt,
}
}),
cell.Invoke(reconciler.Register[*testObject]),
),
)
err = hive.Start(context.TODO())
if err != nil {
panic(err)
}
start := time.Now()
// Create objects in batches to allow the reconciler to start working
// on them while they're added.
id := uint64(0)
batches := int(*numObjects / *batchSize)
for b := 0; b < batches; b++ {
if !*quiet {
fmt.Printf("\rInserting batch %d/%d ...", b+1, batches)
}
wtxn := db.WriteTxn(testObjects)
for j := 0; j < *batchSize; j++ {
testObjects.Insert(wtxn, &testObject{
id: id,
status: reconciler.StatusPending(),
})
id++
}
wtxn.Commit()
}
if !*quiet {
fmt.Printf("\nWaiting for reconciliation to finish ...\n\n")
}
// Wait for all to be reconciled by waiting for the last added objects to be marked
// reconciled. This only works here since none of the operations fail.
for {
obj, _, watch, ok := testObjects.FirstWatch(db.ReadTxn(), idIndex.Query(id-1))
if ok && obj.status.Kind == reconciler.StatusKindDone {
break
}
<-watch
}
end := time.Now()
duration := end.Sub(start)
timePerObject := float64(duration) / float64(*numObjects)
objsPerSecond := float64(time.Second) / timePerObject
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
defer f.Close() // error handling omitted for example
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
}
runtime.GC()
var memAfter runtime.MemStats
runtime.ReadMemStats(&memAfter)
err = hive.Stop(context.TODO())
if err != nil {
panic(err)
}
fmt.Printf("%d objects reconciled in %.2f seconds (batch size %d)\n",
*numObjects, float64(duration)/float64(time.Second), *batchSize)
fmt.Printf("Throughput %.2f objects per second\n", objsPerSecond)
fmt.Printf("Allocated %d objects, %dkB bytes, %dkB bytes still in use\n",
memAfter.HeapObjects-memBefore.HeapObjects,
(memAfter.HeapAlloc-memBefore.HeapAlloc)/1024,
(memAfter.HeapInuse-memBefore.HeapInuse)/1024)
}