storagemanager_test.go 7.3 KB

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