-
Notifications
You must be signed in to change notification settings - Fork 623
/
wiki_test.go
304 lines (266 loc) · 7.55 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//go:build all || cassandra
// +build all cassandra
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Content before git sha 34fdeebefcbf183ed7f916f931aa0586fdaa1b40
* Copyright (c) 2016, The Gocql authors,
* provided under the BSD-3-Clause License.
* See the NOTICE file distributed with this work for additional information.
*/
package gocql
import (
"fmt"
"reflect"
"sort"
"testing"
"time"
"gopkg.in/inf.v0"
)
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{
{
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"),
},
},
{
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
table string
}
func CreateSchema(session *Session, tb testing.TB, table string) *WikiTest {
table = "wiki_" + table
if err := createTable(session, fmt.Sprintf("DROP TABLE IF EXISTS gocql_test.%s", table)); err != nil {
tb.Fatal("CreateSchema:", err)
}
err := createTable(session, fmt.Sprintf(`CREATE TABLE gocql_test.%s (
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)
)`, table))
if err != nil {
tb.Fatal("CreateSchema:", err)
}
return &WikiTest{
session: session,
tb: tb,
table: table,
}
}
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(fmt.Sprintf(`INSERT INTO %s
(title, revid, body, views, protected, modified, rating, tags, attachments)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, w.table),
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(fmt.Sprintf(`SELECT title, revid, body, views, protected,
modified,tags, attachments, rating
FROM %s WHERE title = ? AND revid = ? LIMIT 1`, w.table),
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(fmt.Sprintf(`SELECT COUNT(*) FROM %s`, w.table)).Scan(&count); err != nil {
w.tb.Error("GetPageCount", err)
}
return count
}
func TestWikiCreateSchema(t *testing.T) {
session := createSession(t)
defer session.Close()
CreateSchema(session, t, "create")
}
func BenchmarkWikiCreateSchema(b *testing.B) {
b.StopTimer()
session := createSession(b)
defer func() {
b.StopTimer()
session.Close()
}()
b.StartTimer()
for i := 0; i < b.N; i++ {
CreateSchema(session, b, "bench_create")
}
}
func TestWikiCreatePages(t *testing.T) {
session := createSession(t)
defer session.Close()
w := CreateSchema(session, t, "create_pages")
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 := CreateSchema(session, b, "bench_create_pages")
b.StartTimer()
w.CreatePages(b.N)
}
func BenchmarkWikiSelectAllPages(b *testing.B) {
b.StopTimer()
session := createSession(b)
defer func() {
b.StopTimer()
session.Close()
}()
w := CreateSchema(session, b, "bench_select_all")
w.CreatePages(100)
b.StartTimer()
var page WikiPage
for i := 0; i < b.N; i++ {
iter := session.Query(fmt.Sprintf(`SELECT title, revid, body, views, protected,
modified, tags, attachments, rating
FROM %s`, w.table)).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 := CreateSchema(session, b, "bench_select_single")
pages := make([]WikiPage, 100)
w.CreatePages(len(pages))
iter := session.Query(fmt.Sprintf(`SELECT title, revid FROM %s`, w.table)).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 := CreateSchema(session, b, "bench_page_count")
const 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 := CreateSchema(session, t, "crud")
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)
}
}
}