storagemanager_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package storage
  11. import (
  12. "errors"
  13. "flag"
  14. "fmt"
  15. "os"
  16. "strconv"
  17. "testing"
  18. "time"
  19. "devt.de/krotik/common/fileutil"
  20. )
  21. const DBDIR = "storagemanagertest"
  22. // Main function for all tests in this package
  23. func TestMain(m *testing.M) {
  24. flag.Parse()
  25. // Setup
  26. if res, _ := fileutil.PathExists(DBDIR); res {
  27. os.RemoveAll(DBDIR)
  28. }
  29. err := os.Mkdir(DBDIR, 0770)
  30. if err != nil {
  31. fmt.Print("Could not create test directory:", err.Error())
  32. os.Exit(1)
  33. }
  34. // Run the tests
  35. res := m.Run()
  36. // Teardown
  37. err = os.RemoveAll(DBDIR)
  38. if err != nil {
  39. fmt.Print("Could not remove test directory:", err.Error())
  40. }
  41. os.Exit(res)
  42. }
  43. var enableConcurrencyTest = false
  44. func TestStorageManagerConcurrency(t *testing.T) {
  45. // Disabled for normal testing
  46. if !enableConcurrencyTest {
  47. return
  48. }
  49. var retChans []chan error
  50. threads := 50
  51. ops := 1000
  52. dsm := NewDiskStorageManager(DBDIR+"/ctest_dsm", false, false, true, false)
  53. cdsm := NewCachedDiskStorageManager(dsm, 50000)
  54. sm := cdsm
  55. start := time.Now()
  56. for i := 1; i < threads+1; i++ {
  57. retChan := make(chan error)
  58. retChans = append(retChans, retChan)
  59. // Kick off thread
  60. fmt.Println("Id:", strconv.Itoa(i), " start")
  61. go runConcurrencyTest(strconv.Itoa(i), sm, ops, retChan)
  62. }
  63. // Wait for threads to complete
  64. for i := 0; i < threads; i++ {
  65. retChan := retChans[i]
  66. err := <-retChan
  67. if err != nil {
  68. fmt.Println("Id:", strconv.Itoa(i), " Error:", err)
  69. } else {
  70. fmt.Println("Id:", strconv.Itoa(i), " ok")
  71. }
  72. }
  73. elapsed := time.Since(start)
  74. fmt.Println("Total time:", elapsed)
  75. sm.Close()
  76. }
  77. var enablePerformanceTest = false
  78. func TestStorageManagerPerformance(t *testing.T) {
  79. // Disabled for normal testing
  80. if !enablePerformanceTest {
  81. return
  82. }
  83. // Test multiple read/write operations in concurrent threads
  84. start := time.Now()
  85. // Last iteration here shows the cache running out of available entries -
  86. // Since we ask for the same elements in the same order we completely
  87. // loose the benefit of the cache (i.e. oldest elements are removed first)
  88. for i := 1000; i < 51001; i += 5000 {
  89. dsm := NewDiskStorageManager(DBDIR+"/ptest_dsm", false, false, true, false)
  90. cdsm := NewCachedDiskStorageManager(dsm, 50000)
  91. runPerformanceTest("1", cdsm, i)
  92. }
  93. elapsed := time.Since(start)
  94. fmt.Println("Total time:", elapsed)
  95. }
  96. func runConcurrencyTest(id string, sm Manager, ops int, retChan chan error) {
  97. errorChan := make(chan error)
  98. tc := &testclient{make([]uint64, 0)}
  99. // Insert, Fetch, Update, Fetch some data
  100. start := time.Now()
  101. go tc.clientInsert(id, sm, ops, errorChan)
  102. res := <-errorChan
  103. if res != nil {
  104. retChan <- res
  105. }
  106. go tc.clientFetch(id, "test", sm, ops, errorChan)
  107. res = <-errorChan
  108. if res != nil {
  109. retChan <- res
  110. }
  111. go tc.clientUpdate(id, "t35ter", sm, ops, errorChan)
  112. res = <-errorChan
  113. if res != nil {
  114. retChan <- res
  115. }
  116. go tc.clientFetch(id, "t35ter", sm, ops, errorChan)
  117. res = <-errorChan
  118. if res != nil {
  119. retChan <- res
  120. }
  121. elapsed := time.Since(start).Nanoseconds() / (1000 * 1000)
  122. fmt.Println("Id:", id, " Strings:", ops, " Time:", elapsed)
  123. retChan <- nil
  124. }
  125. func runPerformanceTest(id string, sm Manager, ops int) {
  126. var elapsed1, elapsed2, elapsed3, elapsed4, elapsed5, elapsed6, elapsed7 int64
  127. errorChan := make(chan error)
  128. tc := &testclient{make([]uint64, 0)}
  129. // Insert some data
  130. start := time.Now()
  131. go tc.clientInsert(id, sm, ops, errorChan)
  132. res := <-errorChan
  133. if res != nil {
  134. fmt.Println("tc.clientInsert:", res)
  135. }
  136. elapsed1 = time.Since(start).Nanoseconds() / (1000 * 1000)
  137. // Read data back
  138. start = time.Now()
  139. go tc.clientFetch(id, "test", sm, ops, errorChan)
  140. res = <-errorChan
  141. if res != nil {
  142. fmt.Println("tc.clientFetch:", res)
  143. }
  144. elapsed2 = time.Since(start).Nanoseconds() / (1000 * 1000)
  145. // Read data back a 2nd time
  146. start = time.Now()
  147. go tc.clientFetch(id, "test", sm, ops, errorChan)
  148. res = <-errorChan
  149. if res != nil {
  150. fmt.Println("tc.clientFetch:", res)
  151. }
  152. elapsed3 = time.Since(start).Nanoseconds() / (1000 * 1000)
  153. // Update the data without reallocation
  154. start = time.Now()
  155. go tc.clientUpdate(id, "t35t", sm, ops, errorChan)
  156. res = <-errorChan
  157. if res != nil {
  158. fmt.Println("tc.clientUpdate:", res)
  159. }
  160. elapsed4 = time.Since(start).Nanoseconds() / (1000 * 1000)
  161. // Read data back a 3nd time
  162. start = time.Now()
  163. go tc.clientFetch(id, "t35t", sm, ops, errorChan)
  164. res = <-errorChan
  165. if res != nil {
  166. fmt.Println("tc.clientFetch:", res)
  167. }
  168. elapsed5 = time.Since(start).Nanoseconds() / (1000 * 1000)
  169. // Update the data with reallocation
  170. start = time.Now()
  171. go tc.clientUpdate(id, "teststring", sm, ops, errorChan)
  172. res = <-errorChan
  173. if res != nil {
  174. fmt.Println("tc.clientUpdate:", res)
  175. }
  176. elapsed6 = time.Since(start).Nanoseconds() / (1000 * 1000)
  177. // Read data back a 4th time
  178. start = time.Now()
  179. go tc.clientFetch(id, "teststring", sm, ops, errorChan)
  180. <-errorChan
  181. elapsed7 = time.Since(start).Nanoseconds() / (1000 * 1000)
  182. fmt.Println("Strings,", ops, ",Insert,", elapsed1, ",Fetch1,", elapsed2, ",Fetch2,",
  183. elapsed3, ",Update,", elapsed4, ",Fetch3,", elapsed5, ",Update Realloc,",
  184. elapsed6, ",Fetch,", elapsed7)
  185. sm.Close()
  186. }
  187. type testclient struct {
  188. locs []uint64
  189. }
  190. /*
  191. clientInsert inserts some test data.
  192. */
  193. func (tc *testclient) clientInsert(id string, sm Manager, ops int, errorChan chan error) {
  194. // Write stull
  195. for i := 0; i < ops; i++ {
  196. loc, err := sm.Insert(fmt.Sprint("test-", id, i))
  197. if err != nil {
  198. errorChan <- errors.New(fmt.Sprint("Error during insert thread:", id, " iteration:", i, " error:", err.Error()))
  199. return
  200. }
  201. tc.locs = append(tc.locs, loc)
  202. }
  203. // Flush changes to disk
  204. if err := sm.Flush(); err != nil {
  205. errorChan <- errors.New(fmt.Sprint("Error during flush thread:", id, " error:", err.Error()))
  206. return
  207. }
  208. errorChan <- nil
  209. }
  210. /*
  211. clientFetch reads back test data.
  212. */
  213. func (tc *testclient) clientFetch(id string, teststring string, sm Manager, ops int, errorChan chan error) {
  214. var obj interface{}
  215. var res string
  216. var err error
  217. for i := 0; i < ops; i++ {
  218. obj, _ = sm.FetchCached(tc.locs[i])
  219. if obj == nil {
  220. err = sm.Fetch(tc.locs[i], &res)
  221. } else {
  222. res = obj.(string)
  223. }
  224. if err != nil {
  225. errorChan <- errors.New(fmt.Sprint("Error during fetch thread:", id, " iteration:", i, " error:", err.Error()))
  226. return
  227. }
  228. if res != fmt.Sprint(teststring, "-", id, i) {
  229. errorChan <- errors.New(fmt.Sprint("Unexpected fetch result thread:", id, " iteration:", i, " result:", res))
  230. return
  231. }
  232. }
  233. errorChan <- nil
  234. }
  235. /*
  236. clientUpdate updates test data without requiring relocation.
  237. */
  238. func (tc *testclient) clientUpdate(id string, teststring string, sm Manager, ops int, errorChan chan error) {
  239. // Write stull
  240. for i := 0; i < ops; i++ {
  241. err := sm.Update(tc.locs[i], fmt.Sprint(teststring, "-", id, i))
  242. if err != nil {
  243. errorChan <- errors.New(fmt.Sprint("Error during update thread:", id, " iteration:", i, " error:", err.Error()))
  244. return
  245. }
  246. }
  247. // Flush changes to disk
  248. if err := sm.Flush(); err != nil {
  249. errorChan <- errors.New(fmt.Sprint("Error during flush thread:", id, " error:", err.Error()))
  250. return
  251. }
  252. errorChan <- nil
  253. }