forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 2
/
wiki_test.go
276 lines (249 loc) · 6.53 KB
/
wiki_test.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright (c) 2014 The gocql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gocql
import (
"fmt"
"reflect"
"sort"
"speter.net/go/exp/math/dec/inf"
"testing"
"time"
)
type WikiPage struct {
Title string
RevId UUID
Body string
Views int64
Protected bool
Modified time.Time
Rating *inf.Dec
Tags []string
Attachments map[string]WikiAttachment
}
type WikiAttachment []byte
var wikiTestData = []*WikiPage{
&WikiPage{
Title: "Frontpage",
RevId: TimeUUID(),
Body: "Welcome to this wiki page!",
Rating: inf.NewDec(131, 3),
Modified: time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
Tags: []string{"start", "important", "test"},
Attachments: map[string]WikiAttachment{
"logo": WikiAttachment("\x00company logo\x00"),
"favicon": WikiAttachment("favicon.ico"),
},
},
&WikiPage{
Title: "Foobar",
RevId: TimeUUID(),
Body: "foo::Foo f = new foo::Foo(foo::Foo::INIT);",
Modified: time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
},
}
type WikiTest struct {
session *Session
tb testing.TB
}
func (w *WikiTest) CreateSchema() {
if err := w.session.Query(`DROP TABLE wiki_page`).Exec(); err != nil && err.Error() != "unconfigured columnfamily wiki_page" {
w.tb.Fatal("CreateSchema:", err)
}
err := createTable(w.session, `CREATE TABLE wiki_page (
title varchar,
revid timeuuid,
body varchar,
views bigint,
protected boolean,
modified timestamp,
rating decimal,
tags set<varchar>,
attachments map<varchar, blob>,
PRIMARY KEY (title, revid)
)`)
if *clusterSize > 1 {
// wait for table definition to propogate
time.Sleep(250 * time.Millisecond)
}
if err != nil {
w.tb.Fatal("CreateSchema:", err)
}
}
func (w *WikiTest) CreatePages(n int) {
var page WikiPage
t0 := time.Now()
for i := 0; i < n; i++ {
page.Title = fmt.Sprintf("generated_%d", (i&16)+1)
page.Modified = t0.Add(time.Duration(i-n) * time.Minute)
page.RevId = UUIDFromTime(page.Modified)
page.Body = fmt.Sprintf("text %d", i)
if err := w.InsertPage(&page); err != nil {
w.tb.Error("CreatePages:", err)
}
}
}
func (w *WikiTest) InsertPage(page *WikiPage) error {
return w.session.Query(`INSERT INTO wiki_page
(title, revid, body, views, protected, modified, rating, tags, attachments)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
page.Title, page.RevId, page.Body, page.Views, page.Protected,
page.Modified, page.Rating, page.Tags, page.Attachments).Exec()
}
func (w *WikiTest) SelectPage(page *WikiPage, title string, revid UUID) error {
return w.session.Query(`SELECT title, revid, body, views, protected,
modified,tags, attachments, rating
FROM wiki_page WHERE title = ? AND revid = ? LIMIT 1`,
title, revid).Scan(&page.Title, &page.RevId,
&page.Body, &page.Views, &page.Protected, &page.Modified, &page.Tags,
&page.Attachments, &page.Rating)
}
func (w *WikiTest) GetPageCount() int {
var count int
if err := w.session.Query(`SELECT COUNT(*) FROM wiki_page`).Scan(&count); err != nil {
w.tb.Error("GetPageCount", err)
}
return count
}
func TestWikiCreateSchema(t *testing.T) {
session := createSession(t)
defer session.Close()
w := WikiTest{session, t}
w.CreateSchema()
}
func BenchmarkWikiCreateSchema(b *testing.B) {
b.StopTimer()
session := createSession(b)
defer func() {
b.StopTimer()
session.Close()
}()
w := WikiTest{session, b}
b.StartTimer()
for i := 0; i < b.N; i++ {
w.CreateSchema()
}
}
func TestWikiCreatePages(t *testing.T) {
session := createSession(t)
defer session.Close()
w := WikiTest{session, t}
w.CreateSchema()
numPages := 5
w.CreatePages(numPages)
if count := w.GetPageCount(); count != numPages {
t.Errorf("expected %d pages, got %d pages.", numPages, count)
}
}
func BenchmarkWikiCreatePages(b *testing.B) {
b.StopTimer()
session := createSession(b)
defer func() {
b.StopTimer()
session.Close()
}()
w := WikiTest{session, b}
w.CreateSchema()
b.StartTimer()
w.CreatePages(b.N)
}
func BenchmarkWikiSelectAllPages(b *testing.B) {
b.StopTimer()
session := createSession(b)
defer func() {
b.StopTimer()
session.Close()
}()
w := WikiTest{session, b}
w.CreateSchema()
w.CreatePages(100)
b.StartTimer()
var page WikiPage
for i := 0; i < b.N; i++ {
iter := session.Query(`SELECT title, revid, body, views, protected,
modified, tags, attachments, rating
FROM wiki_page`).Iter()
for iter.Scan(&page.Title, &page.RevId, &page.Body, &page.Views,
&page.Protected, &page.Modified, &page.Tags, &page.Attachments,
&page.Rating) {
// pass
}
if err := iter.Close(); err != nil {
b.Error(err)
}
}
}
func BenchmarkWikiSelectSinglePage(b *testing.B) {
b.StopTimer()
session := createSession(b)
defer func() {
b.StopTimer()
session.Close()
}()
w := WikiTest{session, b}
w.CreateSchema()
pages := make([]WikiPage, 100)
w.CreatePages(len(pages))
iter := session.Query(`SELECT title, revid FROM wiki_page`).Iter()
for i := 0; i < len(pages); i++ {
if !iter.Scan(&pages[i].Title, &pages[i].RevId) {
pages = pages[:i]
break
}
}
if err := iter.Close(); err != nil {
b.Error(err)
}
b.StartTimer()
var page WikiPage
for i := 0; i < b.N; i++ {
p := &pages[i%len(pages)]
if err := w.SelectPage(&page, p.Title, p.RevId); err != nil {
b.Error(err)
}
}
}
func BenchmarkWikiSelectPageCount(b *testing.B) {
b.StopTimer()
session := createSession(b)
defer func() {
b.StopTimer()
session.Close()
}()
w := WikiTest{session, b}
w.CreateSchema()
numPages := 10
w.CreatePages(numPages)
b.StartTimer()
for i := 0; i < b.N; i++ {
if count := w.GetPageCount(); count != numPages {
b.Errorf("expected %d pages, got %d pages.", numPages, count)
}
}
}
func TestWikiTypicalCRUD(t *testing.T) {
session := createSession(t)
defer session.Close()
w := WikiTest{session, t}
w.CreateSchema()
for _, page := range wikiTestData {
if err := w.InsertPage(page); err != nil {
t.Error("InsertPage:", err)
}
}
if count := w.GetPageCount(); count != len(wikiTestData) {
t.Errorf("count: expected %d, got %d\n", len(wikiTestData), count)
}
for _, original := range wikiTestData {
page := new(WikiPage)
if err := w.SelectPage(page, original.Title, original.RevId); err != nil {
t.Error("SelectPage:", err)
continue
}
sort.Sort(sort.StringSlice(page.Tags))
sort.Sort(sort.StringSlice(original.Tags))
if !reflect.DeepEqual(page, original) {
t.Errorf("page: expected %#v, got %#v\n", original, page)
}
}
}