You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

443 lines
13 KiB

  1. package db
  2. import (
  3. "bytes"
  4. "container/list"
  5. "encoding/binary"
  6. "fmt"
  7. "io/ioutil"
  8. "math/rand"
  9. "os"
  10. "path/filepath"
  11. "reflect"
  12. "testing"
  13. "time"
  14. "github.com/guptarohit/asciigraph"
  15. )
  16. type testKeyType int64
  17. // Simple execution cmd
  18. // go test -run=XXX -bench=. -benchmem -cpuprofile=cpu.out -memprofile=mem.out -timeout 20m -benchtime=1m
  19. var valueLen = 256
  20. var testSetSize = int64(10000000)
  21. var batchSize = 100
  22. // parameters for drawing graph
  23. var graphCountingPeriod = 10000
  24. var graphWidth = 120
  25. var graphHeigh = 20
  26. var deviceType = "ssd"
  27. func init() {
  28. rand.Seed(time.Now().Unix())
  29. }
  30. func int64ToBytes(i testKeyType) []byte {
  31. buf := make([]byte, 8)
  32. binary.BigEndian.PutUint64(buf, uint64(i))
  33. return buf
  34. }
  35. func printGraph(statics list.List) {
  36. if statics.Len() == 0 {
  37. return
  38. }
  39. // convert list to slice
  40. var staticsSlice = make([]float64, statics.Len())
  41. i := 0
  42. for e := statics.Front(); e != nil; e = e.Next() {
  43. staticsSlice[i] = e.Value.(float64)
  44. i++
  45. }
  46. graph := asciigraph.Plot(staticsSlice, asciigraph.Width(graphWidth), asciigraph.Height(graphHeigh))
  47. fmt.Println(graph)
  48. }
  49. func dirSizeKB(path string) (int64, error) {
  50. var size int64
  51. err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
  52. if !info.IsDir() {
  53. size += info.Size()
  54. }
  55. return err
  56. })
  57. return size / 1024, err
  58. }
  59. func BenchmarkRandomWR(b *testing.B) {
  60. // generate a common random data set
  61. internal := map[testKeyType][]byte{}
  62. for i := 0; i < int(testSetSize); i++ {
  63. // generate a random value
  64. token := make([]byte, valueLen)
  65. internal[testKeyType(i)] = token
  66. }
  67. for dbType, dbConstructors := range dbImpls {
  68. // create db
  69. dbName := string(dbType)
  70. tmpDir, _ := ioutil.TempDir("", dbName)
  71. defer os.RemoveAll(tmpDir)
  72. dbInstance, _ := dbConstructors(tmpDir)
  73. var numOfWrite int64
  74. var idx testKeyType
  75. var statics list.List
  76. var startTime time.Time
  77. fmt.Printf("[%s]\ntestset_size: %d\nkey_len: %d\nval_len: %d\n",
  78. dbName, testSetSize, reflect.TypeOf(idx).Size(), valueLen)
  79. fmt.Println("device type: ssd")
  80. // write only
  81. b.Run(dbName+"-write", func(b *testing.B) {
  82. var i int
  83. for i = 0; i < b.N; i++ {
  84. if i != 0 && i%graphCountingPeriod == 0 {
  85. startTime = time.Now()
  86. }
  87. idx = testKeyType((int64(rand.Int()) % testSetSize)) // pick a random key
  88. rand.Read(internal[idx]) // generate a random data
  89. dbInstance.Set(
  90. int64ToBytes(testKeyType(idx)),
  91. internal[idx],
  92. )
  93. if i != 0 && i%graphCountingPeriod == 0 {
  94. endTime := time.Now().Sub(startTime).Seconds()
  95. statics.PushBack(endTime)
  96. }
  97. }
  98. numOfWrite += int64(i)
  99. })
  100. printGraph(statics)
  101. statics.Init()
  102. // read only
  103. b.Run(dbName+"-read", func(b *testing.B) {
  104. for i := 0; i < b.N; i++ {
  105. if i != 0 && i%graphCountingPeriod == 0 {
  106. startTime = time.Now()
  107. }
  108. idx = testKeyType((int64(rand.Int()) % testSetSize))
  109. originalVal := internal[idx]
  110. retrievedVal := dbInstance.Get(int64ToBytes(testKeyType(idx)))
  111. if len(retrievedVal) != 0 {
  112. if len(retrievedVal) != valueLen {
  113. b.Errorf("Expected length %X for %v, got %X",
  114. valueLen, idx, len(retrievedVal))
  115. break
  116. } else if !bytes.Equal(retrievedVal, originalVal) {
  117. b.Errorf("Expected %v for %v, got %v",
  118. originalVal, idx, retrievedVal)
  119. break
  120. }
  121. }
  122. if i != 0 && i%graphCountingPeriod == 0 {
  123. endTime := time.Now().Sub(startTime).Seconds()
  124. statics.PushBack(endTime)
  125. }
  126. }
  127. })
  128. printGraph(statics)
  129. statics.Init()
  130. // write and read
  131. b.Run(dbName+"-write-read", func(b *testing.B) {
  132. var i int
  133. for i = 0; i < b.N; i++ {
  134. if i != 0 && i%graphCountingPeriod == 0 {
  135. startTime = time.Now()
  136. }
  137. idx = testKeyType(int64(rand.Int()) % testSetSize) // pick a random key
  138. rand.Read(internal[idx]) // generate a random data
  139. dbInstance.Set(
  140. int64ToBytes(testKeyType(idx)),
  141. internal[idx],
  142. )
  143. originalVal := internal[idx]
  144. retrievedVal := dbInstance.Get(int64ToBytes(testKeyType(idx)))
  145. if len(retrievedVal) != 0 {
  146. if len(retrievedVal) != valueLen {
  147. b.Errorf("Expected length %X for %v, got %X",
  148. valueLen, idx, len(retrievedVal))
  149. break
  150. } else if !bytes.Equal(retrievedVal, originalVal) {
  151. b.Errorf("Expected %v for %v, got %v",
  152. originalVal, idx, retrievedVal)
  153. break
  154. }
  155. }
  156. if i != 0 && i%graphCountingPeriod == 0 {
  157. endTime := time.Now().Sub(startTime).Seconds()
  158. statics.PushBack(endTime)
  159. }
  160. }
  161. numOfWrite += int64(i)
  162. })
  163. printGraph(statics)
  164. statics.Init()
  165. // close
  166. dbInstance.Close()
  167. size, err := dirSizeKB(tmpDir)
  168. if err != nil {
  169. fmt.Println(err)
  170. } else {
  171. fmt.Printf("* Total size of %s db: %v kb, Size of 1 write: %v byte\n", dbName, size, size*1024/numOfWrite)
  172. }
  173. }
  174. }
  175. func BenchmarkRandomBatchWR(b *testing.B) {
  176. // generate a common random data set
  177. internal := map[testKeyType][]byte{}
  178. for i := 0; i < int(testSetSize); i++ {
  179. // generate a random value
  180. token := make([]byte, valueLen)
  181. internal[testKeyType(i)] = token
  182. }
  183. for dbType, dbConstructors := range dbImpls {
  184. // create db
  185. dbName := string(dbType)
  186. tmpDir, _ := ioutil.TempDir("", dbName)
  187. defer os.RemoveAll(tmpDir)
  188. dbInstance, _ := dbConstructors(tmpDir)
  189. var numOfWrite int64
  190. var idx testKeyType
  191. var statics list.List
  192. var startTime time.Time
  193. fmt.Printf("[%s]\ntestset_size: %d\nkey_len: %d\nval_len: %d\n",
  194. dbName, testSetSize, reflect.TypeOf(idx).Size(), valueLen)
  195. fmt.Println("device type: " + deviceType)
  196. fmt.Printf("batch size: %d\n", batchSize)
  197. // write only
  198. b.Run(dbName+"-batch-write", func(b *testing.B) {
  199. var i int
  200. for i = 0; i < b.N; i++ {
  201. if i != 0 && i%graphCountingPeriod == 0 {
  202. startTime = time.Now()
  203. }
  204. tx := dbInstance.NewTx()
  205. for j := 0; j < batchSize; j++ {
  206. idx = testKeyType((int64(rand.Int()) % testSetSize)) // pick a random key
  207. rand.Read(internal[idx]) // generate a random data
  208. tx.Set(
  209. int64ToBytes(testKeyType(idx)),
  210. internal[idx],
  211. )
  212. }
  213. tx.Commit()
  214. if i != 0 && i%graphCountingPeriod == 0 {
  215. endTime := time.Now().Sub(startTime).Seconds()
  216. statics.PushBack(endTime)
  217. }
  218. }
  219. numOfWrite += int64(i)
  220. })
  221. // print a graph
  222. printGraph(statics)
  223. statics.Init()
  224. // read only
  225. b.Run(dbName+"-read", func(b *testing.B) {
  226. for i := 0; i < b.N; i++ {
  227. if i != 0 && i%graphCountingPeriod == 0 {
  228. startTime = time.Now()
  229. }
  230. idx = testKeyType((int64(rand.Int()) % testSetSize))
  231. originalVal := internal[idx]
  232. retrievedVal := dbInstance.Get(int64ToBytes(testKeyType(idx)))
  233. if len(retrievedVal) != 0 {
  234. if len(retrievedVal) != valueLen {
  235. b.Errorf("Expected length %X for %v, got %X",
  236. valueLen, idx, len(retrievedVal))
  237. break
  238. } else if !bytes.Equal(retrievedVal, originalVal) {
  239. b.Errorf("Expected %v for %v, got %v",
  240. originalVal, idx, retrievedVal)
  241. break
  242. }
  243. }
  244. if i != 0 && i%graphCountingPeriod == 0 {
  245. endTime := time.Now().Sub(startTime).Seconds()
  246. statics.PushBack(endTime)
  247. }
  248. }
  249. })
  250. // close
  251. dbInstance.Close()
  252. printGraph(statics)
  253. size, err := dirSizeKB(tmpDir)
  254. if err != nil {
  255. fmt.Println(err)
  256. } else {
  257. fmt.Printf("* Total size of %s db: %v kb, Size of 1 write: %v byte\n", dbName, size, size*1024/numOfWrite)
  258. }
  259. }
  260. }
  261. /*
  262. [Refined Test Result]
  263. goos: windows
  264. goarch: amd64
  265. pkg: github.com/aergoio/aergo-lib/db
  266. [badgerdb-ssd-single]
  267. testset_size: 10000000
  268. key_len: 8
  269. val_len: 256
  270. device type: ssd
  271. BenchmarkRandomWR/badgerdb-write-12 30000000 29536 ns/op 4983 B/op 84 allocs/op
  272. BenchmarkRandomWR/badgerdb-read-12 20000000 47739 ns/op 13154 B/op 60 allocs/op
  273. BenchmarkRandomWR/badgerdb-write-read-12 20000000 41990 ns/op 10317 B/op 140 allocs/op
  274. * Total size of badgerdb db: 14544473 kb, Size of 1 write: 286 byte
  275. [leveldb-ssd-single]
  276. testset_size: 10000000
  277. key_len: 8
  278. val_len: 256
  279. device type: ssd
  280. BenchmarkRandomWR/leveldb-write-12 1000000 1006445 ns/op 532 B/op 5 allocs/op
  281. BenchmarkRandomWR/leveldb-read-12 50000000 18941 ns/op 1160 B/op 17 allocs/op
  282. BenchmarkRandomWR/leveldb-write-read-12 1000000 986440 ns/op 1272 B/op 11 allocs/op
  283. * Total size of leveldb db: 567386 kb, Size of 1 write: 250 byte
  284. [badgerdb-ssd-batch100]
  285. testset_size: 10000000
  286. key_len: 8
  287. val_len: 256
  288. device type: ssd
  289. batch size: 100
  290. BenchmarkRandomBatchWR/badgerdb-batch-write-12 2000000 612737 ns/op 383751 B/op 4576 allocs/op
  291. BenchmarkRandomBatchWR/badgerdb-read-12 20000000 53298 ns/op 22177 B/op 71 allocs/op
  292. * Total size of badgerdb db: 74546912 kb, Size of 1 write: 25359 byte
  293. [leveldb-ssd-batch100]
  294. testset_size: 10000000
  295. key_len: 8
  296. val_len: 256
  297. device type: ssd
  298. batch size: 100
  299. BenchmarkRandomBatchWR/leveldb-batch-write-12 300000 6778458 ns/op 127466 B/op 399 allocs/op
  300. BenchmarkRandomBatchWR/leveldb-read-12 5000000 141176 ns/op 4547 B/op 34 allocs/op
  301. * Total size of leveldb db: 2540271 kb, Size of 1 write: 8388 byte
  302. [badgerdb-ssd-batch1000]
  303. testset_size: 10000000
  304. key_len: 8
  305. val_len: 256
  306. device type: ssd
  307. batch size: 1000
  308. BenchmarkRandomBatchWR1000/badgerdb-batch-write-12 200000 5507060 ns/op 3504344 B/op 42743 allocs/op
  309. BenchmarkRandomBatchWR1000/badgerdb-read-12 20000000 52636 ns/op 21867 B/op 73 allocs/op
  310. * Total size of badgerdb db: 51315516 kb, Size of 1 write: 250103 byte
  311. [leveldb-ssd-batch100]
  312. testset_size: 10000000
  313. key_len: 8
  314. val_len: 256
  315. device type: ssd
  316. batch size: 1000
  317. BenchmarkRandomBatchWR1000/leveldb-batch-write-12 20000 70431430 ns/op 1081956 B/op 3906 allocs/op
  318. BenchmarkRandomBatchWR1000/leveldb-read-12 5000000 138074 ns/op 4194 B/op 33 allocs/op
  319. * Total size of leveldb db: 2529393 kb, Size of 1 write: 86046 byte
  320. [badgerdb-hdd-single]
  321. testset_size: 10000000
  322. key_len: 8
  323. val_len: 256
  324. device type: hdd
  325. BenchmarkHddRandomWR/badgerdb-write-12 30000000 27901 ns/op 4809 B/op 82 allocs/op
  326. BenchmarkHddRandomWR/badgerdb-read-12 20000000 45765 ns/op 14321 B/op 68 allocs/op
  327. BenchmarkHddRandomWR/badgerdb-write-read-12 20000000 39404 ns/op 8693 B/op 124 allocs/op
  328. * Total size of badgerdb db: 14697136 kb, Size of 1 write: 289 byte
  329. [leveldb-hdd-single]
  330. testset_size: 10000000
  331. key_len: 8
  332. val_len: 256
  333. device type: hdd
  334. BenchmarkHddRandomWR/leveldb-write-12 50000 20975160 ns/op 551 B/op 4 allocs/op
  335. BenchmarkHddRandomWR/leveldb-read-12 100000000 10375 ns/op 988 B/op 14 allocs/op
  336. BenchmarkHddRandomWR/leveldb-write-read-12 50000 21144257 ns/op 994 B/op 9 allocs/op
  337. * Total size of leveldb db: 31940 kb, Size of 1 write: 272 byte
  338. [badgerdb-hdd-batch100]
  339. testset_size: 10000000
  340. key_len: 8
  341. val_len: 256
  342. device type: hdd
  343. batch size: 100
  344. BenchmarkHddRandomBatchWR/badgerdb-batch-write-12 1000000 675351 ns/op 329398 B/op 4007 allocs/op
  345. BenchmarkHddRandomBatchWR/badgerdb-read-12 10000000 87933 ns/op 22713 B/op 85 allocs/op
  346. * Total size of badgerdb db: 24285947 kb, Size of 1 write: 24620 byte
  347. [leveldb-hdd-batch100]
  348. testset_size: 10000000
  349. key_len: 8
  350. val_len: 256
  351. device type: hdd
  352. batch size: 100
  353. BenchmarkHddRandomBatchWR/leveldb-batch-write-12 30000 45769594 ns/op 136783 B/op 399 allocs/op
  354. BenchmarkHddRandomBatchWR/leveldb-read-12 20000000 30579 ns/op 3386 B/op 19 allocs/op
  355. * Total size of leveldb db: 878403 kb, Size of 1 write: 22430 byte
  356. [badgerdb-hdd-batch1000]
  357. testset_size: 10000000
  358. key_len: 8
  359. val_len: 256
  360. device type: hdd
  361. batch size: 1000
  362. BenchmarkHddRandomBatchWR1000/badgerdb-batch-write-12 200000 6599637 ns/op 3724911 B/op 43392 allocs/op
  363. BenchmarkHddRandomBatchWR1000/badgerdb-read-12 10000000 99201 ns/op 31710 B/op 105 allocs/op
  364. * Total size of badgerdb db: 51157857 kb, Size of 1 write: 249335 byte
  365. [leveldb-hdd-batch1000]
  366. testset_size: 10000000
  367. key_len: 8
  368. val_len: 256
  369. device type: hdd
  370. batch size: 1000
  371. BenchmarkHddRandomBatchWR1000/leveldb-batch-write-12 10000 209360868 ns/op 1027546 B/op 3363 allocs/op
  372. BenchmarkHddRandomBatchWR1000/leveldb-read-12 10000000 87929 ns/op 3971 B/op 29 allocs/op
  373. * Total size of leveldb db: 1689120 kb, Size of 1 write: 171236 byte
  374. PASS
  375. ok github.com/aergoio/aergo-lib/db 33617.451s
  376. */