-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
156 lines (131 loc) · 2.46 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
package main
import (
"flag"
"fmt"
"log"
"sync"
"sync/atomic"
"time"
)
/*
#include "db.h"
*/
// #cgo CXXFLAGS: -std=c++11
import "C"
var numWorkers = flag.Int("w", 100, "")
var groupType = flag.String("t", "go", "")
var numOps uint64
type db struct {
groupInGo bool
cdb *C.DB
mu sync.Mutex
cond *sync.Cond
committing bool
commitSeq uint64
pendingSeq uint64
pending []*batch
}
func newDB(groupInGo bool) *db {
db := &db{
groupInGo: groupInGo,
cdb: C.NewDB(),
}
db.cond = sync.NewCond(&db.mu)
return db
}
func (db *db) newBatch() *batch {
return &batch{
cbatch: C.NewBatch(db.cdb),
}
}
func (db *db) commit(b *batch) int {
if !db.groupInGo {
return int(C.CommitBatch(db.cdb, b.cbatch))
}
db.mu.Lock()
leader := len(db.pending) == 0
seq := db.pendingSeq
db.pending = append(db.pending, b)
var size int
if leader {
// We're the leader. Wait for any running commit to finish.
for db.committing {
db.cond.Wait()
}
pending := db.pending
db.pending = nil
db.pendingSeq++
db.committing = true
db.mu.Unlock()
for _, p := range pending[1:] {
b.combine(p)
}
b.apply()
size = len(pending)
db.mu.Lock()
db.committing = false
db.commitSeq = seq
db.cond.Broadcast()
} else {
// We're a follower. Wait for the commit to finish.
for db.commitSeq < seq {
db.cond.Wait()
}
}
db.mu.Unlock()
return size
}
type batch struct {
cbatch *C.Batch
}
func (b *batch) combine(other *batch) {
// In the real implementation, this combines the batches together.
}
func (b *batch) apply() {
C.ApplyBatch(b.cbatch)
}
func (b *batch) free() {
C.FreeBatch(b.cbatch)
}
func worker(db *db) {
for {
b := db.newBatch()
db.commit(b)
b.free()
atomic.AddUint64(&numOps, 1)
time.Sleep(time.Microsecond)
}
}
func main() {
flag.Parse()
var groupInGo bool
switch *groupType {
case "go":
groupInGo = true
case "cgo":
groupInGo = false
default:
log.Fatalf("unknown batch type: %s", *groupType)
}
db := newDB(groupInGo)
for i := 0; i < *numWorkers; i++ {
go worker(db)
}
start := time.Now()
lastNow := start
var lastOps uint64
for i := 0; ; i++ {
time.Sleep(time.Second)
if i%20 == 0 {
fmt.Println("_elapsed____ops/sec")
}
now := time.Now()
elapsed := now.Sub(lastNow)
ops := atomic.LoadUint64(&numOps)
fmt.Printf("%8s %10.1f\n",
time.Duration(time.Since(start).Seconds()+0.5)*time.Second,
float64(ops-lastOps)/elapsed.Seconds())
lastNow = now
lastOps = ops
}
}