This repository has been archived by the owner on Sep 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb_writes.go
89 lines (73 loc) · 1.8 KB
/
mongodb_writes.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
package main
import (
"errors"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"log"
"strconv"
"strings"
)
const (
MONGO_MIN_DOCUMENT_LENGTH = 64
MONGO_DEFAULT_DOCUMENT_LENGTH = MONGO_MIN_DOCUMENT_LENGTH
)
type mongodb_writes struct {
conf *MongoBehaviorInfo
deadbeef_id interface{}
collection func() *mgo.Collection
doc_length int
doc_data string
}
func (this *mongodb_writes) Init(info *MongoBehaviorInfo) (err error) {
this.conf = info
this.collection = info.collection
err = this.parseProperties(this.conf.properties)
if err != nil {
return
}
this.doc_data = this.document_data()
return
}
func (this *mongodb_writes) Close() {
// nop
}
func (this *mongodb_writes) Work() (res WorkResult) {
err := this.insert_document()
switch {
case err != nil:
log.Fatalf("writes: %+v", err)
default:
res = WRK_OK
}
return
}
func (this *mongodb_writes) insert_document() (err error) {
// Use the same document data every time to eliminate the
// overhead of random data generation from the results.
// Hopefully, that doesn't invalidate the test.
doc := M{"_id": bson.NewObjectId(), "data": this.doc_data}
err = this.collection().Insert(doc)
if err != nil {
return
}
return
}
func (this *mongodb_writes) document_data() string {
if this.doc_length == MONGO_MIN_DOCUMENT_LENGTH {
return "I wrote some Go!" // should be exactly 64 bytes total now.
} else {
return strings.Repeat("x", this.doc_length)
}
}
func (this *mongodb_writes) parseProperties(props map[string]string) (err error) {
if v, ok := props["mongodb.doc_length"]; ok {
w, err := strconv.Atoi(v)
if err != nil || w < MONGO_MIN_DOCUMENT_LENGTH {
return errors.New("mongodb.doc_length must be >= 64 bytes")
}
this.doc_length = w
} else {
this.doc_length = MONGO_DEFAULT_DOCUMENT_LENGTH
}
return
}