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.

94 lines
2.0 KiB

  1. package benchmarks
  2. import (
  3. "crypto/rand"
  4. "fmt"
  5. "runtime"
  6. "testing"
  7. "time"
  8. "github.com/arnaucube/kesto/treeinterface/arbotree"
  9. asmtree "github.com/p4u/asmt"
  10. "go.vocdoni.io/dvote/censustree"
  11. "go.vocdoni.io/dvote/censustree/gravitontree"
  12. )
  13. // func BenchmarkAddBatch(b *testing.B) {
  14. func TestAddBatch(t *testing.T) {
  15. nLeafs := 100_000
  16. fmt.Printf("nCPU: %d, nLeafs: %d\n", runtime.NumCPU(), nLeafs)
  17. // prepare inputs
  18. var ks, vs [][]byte
  19. for i := 0; i < nLeafs; i++ {
  20. k := randomBytes(32)
  21. v := randomBytes(32)
  22. ks = append(ks, k)
  23. vs = append(vs, v)
  24. }
  25. tree1 := &asmtree.Tree{}
  26. benchmarkAdd(t, "asmtree", tree1, ks, vs)
  27. tree1 = &asmtree.Tree{}
  28. benchmarkAddBatch(t, "asmtree", tree1, ks, vs)
  29. tree2 := &gravitontree.Tree{}
  30. benchmarkAdd(t, "gravitontree", tree2, ks, vs)
  31. tree2 = &gravitontree.Tree{}
  32. benchmarkAddBatch(t, "gravitontree", tree2, ks, vs)
  33. tree3 := &arbotree.Tree{}
  34. benchmarkAdd(t, "arbo", tree3, ks, vs)
  35. tree3 = &arbotree.Tree{}
  36. benchmarkAddBatch(t, "arbo", tree3, ks, vs)
  37. }
  38. func benchmarkAdd(t *testing.T, name string, tree censustree.Tree, ks, vs [][]byte) {
  39. storage := t.TempDir()
  40. err := tree.Init("test1", storage)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. start := time.Now()
  45. for i := 0; i < len(ks); i++ {
  46. if err := tree.Add(ks[i], vs[i]); err != nil {
  47. t.Fatal(err)
  48. }
  49. }
  50. printRes(t, name+".Add loop", time.Since(start))
  51. }
  52. func benchmarkAddBatch(t *testing.T, name string, tree censustree.Tree, ks, vs [][]byte) {
  53. storage := t.TempDir()
  54. err := tree.Init("test1", storage)
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. start := time.Now()
  59. invalids, err := tree.AddBatch(ks, vs)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. if len(invalids) != 0 {
  64. t.Fatal("len(invalids)!=0")
  65. }
  66. printRes(t, name+".AddBatch", time.Since(start))
  67. }
  68. func printRes(t *testing.T, name string, duration time.Duration) {
  69. fmt.Printf(" %s: %s \n", name, duration)
  70. }
  71. func randomBytes(n int) []byte {
  72. b := make([]byte, n)
  73. _, err := rand.Read(b)
  74. if err != nil {
  75. panic(err)
  76. }
  77. return b
  78. }