wiki_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // Copyright (c) 2014 The gocql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gocql
  5. import (
  6. "fmt"
  7. "reflect"
  8. "sort"
  9. "speter.net/go/exp/math/dec/inf"
  10. "testing"
  11. "time"
  12. )
  13. var cf func() *ClusterConfig
  14. func init() {
  15. if true == *flagRunSslTest {
  16. cf = createCluster
  17. createCluster = createSslCluster
  18. }
  19. }
  20. var createSslCluster = func() *ClusterConfig {
  21. cluster := cf()
  22. cluster.SslOpts = &SslOptions{
  23. CertPath: "resources/conf/pki/gocql.crt",
  24. KeyPath: "resources/conf/pki/gocql.key",
  25. CaPath: "resources/conf/pki/ca.crt",
  26. EnableHostVerification: false,
  27. }
  28. return cluster
  29. }
  30. type WikiPage struct {
  31. Title string
  32. RevId UUID
  33. Body string
  34. Views int64
  35. Protected bool
  36. Modified time.Time
  37. Rating *inf.Dec
  38. Tags []string
  39. Attachments map[string]WikiAttachment
  40. }
  41. type WikiAttachment []byte
  42. var wikiTestData = []*WikiPage{
  43. &WikiPage{
  44. Title: "Frontpage",
  45. RevId: TimeUUID(),
  46. Body: "Welcome to this wiki page!",
  47. Rating: inf.NewDec(131, 3),
  48. Modified: time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  49. Tags: []string{"start", "important", "test"},
  50. Attachments: map[string]WikiAttachment{
  51. "logo": WikiAttachment("\x00company logo\x00"),
  52. "favicon": WikiAttachment("favicon.ico"),
  53. },
  54. },
  55. &WikiPage{
  56. Title: "Foobar",
  57. RevId: TimeUUID(),
  58. Body: "foo::Foo f = new foo::Foo(foo::Foo::INIT);",
  59. Modified: time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  60. },
  61. }
  62. type WikiTest struct {
  63. session *Session
  64. tb testing.TB
  65. }
  66. func (w *WikiTest) CreateSchema() {
  67. if err := w.session.Query(`DROP TABLE wiki_page`).Exec(); err != nil && err.Error() != "unconfigured columnfamily wiki_page" {
  68. w.tb.Fatal("CreateSchema:", err)
  69. }
  70. err := createTable(w.session, `CREATE TABLE wiki_page (
  71. title varchar,
  72. revid timeuuid,
  73. body varchar,
  74. views bigint,
  75. protected boolean,
  76. modified timestamp,
  77. rating decimal,
  78. tags set<varchar>,
  79. attachments map<varchar, blob>,
  80. PRIMARY KEY (title, revid)
  81. )`)
  82. if *clusterSize > 1 {
  83. // wait for table definition to propogate
  84. time.Sleep(250 * time.Millisecond)
  85. }
  86. if err != nil {
  87. w.tb.Fatal("CreateSchema:", err)
  88. }
  89. }
  90. func (w *WikiTest) CreatePages(n int) {
  91. var page WikiPage
  92. t0 := time.Now()
  93. for i := 0; i < n; i++ {
  94. page.Title = fmt.Sprintf("generated_%d", (i&16)+1)
  95. page.Modified = t0.Add(time.Duration(i-n) * time.Minute)
  96. page.RevId = UUIDFromTime(page.Modified)
  97. page.Body = fmt.Sprintf("text %d", i)
  98. if err := w.InsertPage(&page); err != nil {
  99. w.tb.Error("CreatePages:", err)
  100. }
  101. }
  102. }
  103. func (w *WikiTest) InsertPage(page *WikiPage) error {
  104. return w.session.Query(`INSERT INTO wiki_page
  105. (title, revid, body, views, protected, modified, rating, tags, attachments)
  106. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
  107. page.Title, page.RevId, page.Body, page.Views, page.Protected,
  108. page.Modified, page.Rating, page.Tags, page.Attachments).Exec()
  109. }
  110. func (w *WikiTest) SelectPage(page *WikiPage, title string, revid UUID) error {
  111. return w.session.Query(`SELECT title, revid, body, views, protected,
  112. modified,tags, attachments, rating
  113. FROM wiki_page WHERE title = ? AND revid = ? LIMIT 1`,
  114. title, revid).Scan(&page.Title, &page.RevId,
  115. &page.Body, &page.Views, &page.Protected, &page.Modified, &page.Tags,
  116. &page.Attachments, &page.Rating)
  117. }
  118. func (w *WikiTest) GetPageCount() int {
  119. var count int
  120. if err := w.session.Query(`SELECT COUNT(*) FROM wiki_page`).Scan(&count); err != nil {
  121. w.tb.Error("GetPageCount", err)
  122. }
  123. return count
  124. }
  125. func TestWikiCreateSchema(t *testing.T) {
  126. session := createSession(t)
  127. defer session.Close()
  128. w := WikiTest{session, t}
  129. w.CreateSchema()
  130. }
  131. func BenchmarkWikiCreateSchema(b *testing.B) {
  132. b.StopTimer()
  133. session := createSession(b)
  134. defer func() {
  135. b.StopTimer()
  136. session.Close()
  137. }()
  138. w := WikiTest{session, b}
  139. b.StartTimer()
  140. for i := 0; i < b.N; i++ {
  141. w.CreateSchema()
  142. }
  143. }
  144. func TestWikiCreatePages(t *testing.T) {
  145. session := createSession(t)
  146. defer session.Close()
  147. w := WikiTest{session, t}
  148. w.CreateSchema()
  149. numPages := 5
  150. w.CreatePages(numPages)
  151. if count := w.GetPageCount(); count != numPages {
  152. t.Errorf("expected %d pages, got %d pages.", numPages, count)
  153. }
  154. }
  155. func BenchmarkWikiCreatePages(b *testing.B) {
  156. b.StopTimer()
  157. session := createSession(b)
  158. defer func() {
  159. b.StopTimer()
  160. session.Close()
  161. }()
  162. w := WikiTest{session, b}
  163. w.CreateSchema()
  164. b.StartTimer()
  165. w.CreatePages(b.N)
  166. }
  167. func BenchmarkWikiSelectAllPages(b *testing.B) {
  168. b.StopTimer()
  169. session := createSession(b)
  170. defer func() {
  171. b.StopTimer()
  172. session.Close()
  173. }()
  174. w := WikiTest{session, b}
  175. w.CreateSchema()
  176. w.CreatePages(100)
  177. b.StartTimer()
  178. var page WikiPage
  179. for i := 0; i < b.N; i++ {
  180. iter := session.Query(`SELECT title, revid, body, views, protected,
  181. modified, tags, attachments, rating
  182. FROM wiki_page`).Iter()
  183. for iter.Scan(&page.Title, &page.RevId, &page.Body, &page.Views,
  184. &page.Protected, &page.Modified, &page.Tags, &page.Attachments,
  185. &page.Rating) {
  186. // pass
  187. }
  188. if err := iter.Close(); err != nil {
  189. b.Error(err)
  190. }
  191. }
  192. }
  193. func BenchmarkWikiSelectSinglePage(b *testing.B) {
  194. b.StopTimer()
  195. session := createSession(b)
  196. defer func() {
  197. b.StopTimer()
  198. session.Close()
  199. }()
  200. w := WikiTest{session, b}
  201. w.CreateSchema()
  202. pages := make([]WikiPage, 100)
  203. w.CreatePages(len(pages))
  204. iter := session.Query(`SELECT title, revid FROM wiki_page`).Iter()
  205. for i := 0; i < len(pages); i++ {
  206. if !iter.Scan(&pages[i].Title, &pages[i].RevId) {
  207. pages = pages[:i]
  208. break
  209. }
  210. }
  211. if err := iter.Close(); err != nil {
  212. b.Error(err)
  213. }
  214. b.StartTimer()
  215. var page WikiPage
  216. for i := 0; i < b.N; i++ {
  217. p := &pages[i%len(pages)]
  218. if err := w.SelectPage(&page, p.Title, p.RevId); err != nil {
  219. b.Error(err)
  220. }
  221. }
  222. }
  223. func BenchmarkWikiSelectPageCount(b *testing.B) {
  224. b.StopTimer()
  225. session := createSession(b)
  226. defer func() {
  227. b.StopTimer()
  228. session.Close()
  229. }()
  230. w := WikiTest{session, b}
  231. w.CreateSchema()
  232. numPages := 10
  233. w.CreatePages(numPages)
  234. b.StartTimer()
  235. for i := 0; i < b.N; i++ {
  236. if count := w.GetPageCount(); count != numPages {
  237. b.Errorf("expected %d pages, got %d pages.", numPages, count)
  238. }
  239. }
  240. }
  241. func TestWikiTypicalCRUD(t *testing.T) {
  242. session := createSession(t)
  243. defer session.Close()
  244. w := WikiTest{session, t}
  245. w.CreateSchema()
  246. for _, page := range wikiTestData {
  247. if err := w.InsertPage(page); err != nil {
  248. t.Error("InsertPage:", err)
  249. }
  250. }
  251. if count := w.GetPageCount(); count != len(wikiTestData) {
  252. t.Errorf("count: expected %d, got %d\n", len(wikiTestData), count)
  253. }
  254. for _, original := range wikiTestData {
  255. page := new(WikiPage)
  256. if err := w.SelectPage(page, original.Title, original.RevId); err != nil {
  257. t.Error("SelectPage:", err)
  258. continue
  259. }
  260. sort.Sort(sort.StringSlice(page.Tags))
  261. sort.Sort(sort.StringSlice(original.Tags))
  262. if !reflect.DeepEqual(page, original) {
  263. t.Errorf("page: expected %#v, got %#v\n", original, page)
  264. }
  265. }
  266. }