forked from dgraph-io/dgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
237 lines (213 loc) · 4.92 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
231
232
233
234
235
236
237
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"math/rand"
"net/url"
"os"
"strings"
"sync/atomic"
"time"
"github.com/dgraph-io/dgraph/client"
"github.com/dgraph-io/dgraph/protos/api"
"github.com/dgraph-io/dgraph/x"
"google.golang.org/grpc"
)
var addrs = flag.String("addrs", "", "comma separated dgraph addresses")
var mode = flag.String("mode", "", "mode to run in ('mutate' or 'query')")
var conc = flag.Int("j", 1, "number of operations to run in parallel")
func init() {
rand.Seed(time.Now().Unix())
}
var ctx = context.Background()
func main() {
flag.Parse()
c := makeClient()
// Install the schema automatically on the first run. This allows the same
// command to be used when running this program for the first and
// subsequent times. We guess if it's the first run based on the number of
// schema items.
resp, err := c.NewTxn().Query(ctx, "schema {}")
x.Check(err)
if len(resp.Schema) < 5 {
// Run each schema alter separately so that there is an even
// distribution among all groups.
for _, s := range schema() {
x.Check(c.Alter(ctx, &api.Operation{
Schema: s,
}))
}
x.Check2(c.NewTxn().Mutate(ctx, &api.Mutation{
CommitNow: true,
SetNquads: []byte(initialData()),
}))
}
switch *mode {
case "mutate":
var errCount int64
var mutateCount int64
for i := 0; i < *conc; i++ {
go func() {
for {
err := mutate(c)
if err == nil {
atomic.AddInt64(&mutateCount, 1)
} else {
atomic.AddInt64(&errCount, 1)
}
}
}()
}
for {
time.Sleep(time.Second)
fmt.Printf("Status: success_mutations=%d errors=%d\n",
atomic.LoadInt64(&mutateCount), atomic.LoadInt64(&errCount))
}
case "query":
var errCount int64
var queryCount int64
for i := 0; i < *conc; i++ {
go func() {
for {
err := showNode(c)
if err == nil {
atomic.AddInt64(&queryCount, 1)
} else {
atomic.AddInt64(&errCount, 1)
}
}
}()
}
for {
time.Sleep(time.Second)
fmt.Printf("Status: success_queries=%d errors=%d\n",
atomic.LoadInt64(&queryCount), atomic.LoadInt64(&errCount))
}
default:
fmt.Printf("unknown mode: %q\n", *mode)
os.Exit(1)
}
}
func schema() []string {
s := []string{"xid: string @index(exact) .\n"}
for char := 'a'; char <= 'z'; char++ {
s = append(s, fmt.Sprintf("count_%c: int .\n", char))
}
for char := 'a'; char <= 'z'; char++ {
s = append(s, fmt.Sprintf("attr_%c: string .\n", char))
}
return s
}
func initialData() string {
rdfs := "_:root <xid> \"root\" .\n"
for char := 'a'; char <= 'z'; char++ {
rdfs += fmt.Sprintf("_:root <count_%c> \"0\" .\n", char)
}
return rdfs
}
func makeClient() *client.Dgraph {
var dgcs []api.DgraphClient
for _, addr := range strings.Split(*addrs, ",") {
c, err := grpc.Dial(addr, grpc.WithInsecure())
x.Check(err)
dgcs = append(dgcs, api.NewDgraphClient(c))
}
return client.NewDgraphClient(dgcs...)
}
type runner struct {
txn *client.Txn
}
func mutate(c *client.Dgraph) error {
r := &runner{
txn: c.NewTxn(),
}
defer r.txn.Discard(ctx)
char := 'a' + rune(rand.Intn(26))
var result struct {
Q []struct {
Uid *string
Count *int
}
}
if err := r.query(&result, `
{
q(func: eq(xid, "root")) {
uid
count: count_%c
}
}
`, char); err != nil {
return err
}
x.AssertTrue(len(result.Q) > 0 && result.Q[0].Count != nil && result.Q[0].Uid != nil)
if _, err := r.txn.Mutate(ctx, &api.Mutation{
SetNquads: []byte(fmt.Sprintf("<%s> <count_%c> \"%d\" .\n",
*result.Q[0].Uid, char, *result.Q[0].Count+1)),
}); err != nil {
return err
}
rdfs := fmt.Sprintf("_:node <xid> \"%c_%d\" .\n", char, *result.Q[0].Count)
for char := 'a'; char <= 'z'; char++ {
if rand.Float64() < 0.9 {
continue
}
payload := make([]byte, 16+rand.Intn(16))
rand.Read(payload)
rdfs += fmt.Sprintf("_:node <attr_%c> \"%s\" .\n", char, url.QueryEscape(string(payload)))
}
if _, err := r.txn.Mutate(ctx, &api.Mutation{
SetNquads: []byte(rdfs),
}); err != nil {
return err
}
return r.txn.Commit(ctx)
}
func showNode(c *client.Dgraph) error {
r := &runner{
txn: c.NewTxn(),
}
defer r.txn.Discard(ctx)
char := 'a' + rune(rand.Intn(26))
var result struct {
Q []struct {
Count *int
}
}
q := fmt.Sprintf(`
{
q(func: eq(xid, "root")) {
uid
count: count_%c
}
}
`, char)
resp, err := r.txn.Query(ctx, q)
if err != nil {
return err
}
if err := json.Unmarshal(resp.Json, &result); err != nil {
return err
}
x.AssertTruef(len(result.Q) > 0 && result.Q[0].Count != nil, "%v %+v", string(resp.Json), result)
var m map[string]interface{}
if err := r.query(&m, `
{
q(func: eq(xid, "%c_%d")) {
expand(_all_)
}
}
`, char, rand.Intn(*result.Q[0].Count)); err != nil {
return err
}
return nil
}
func (r *runner) query(out interface{}, q string, args ...interface{}) error {
q = fmt.Sprintf(q, args...)
resp, err := r.txn.Query(ctx, q)
if err != nil {
return err
}
return json.Unmarshal(resp.Json, out)
}