func benchTreeSync(b *testing.B, size, delta int) { b.StopTimer() tree1 := NewTree() tree2 := NewTree() var k []byte var v []byte for i := 0; i < size; i++ { k = murmur.HashString(fmt.Sprint(i)) v = []byte(fmt.Sprint(i)) tree1.Put(k, v, 1) tree2.Put(k, v, 1) } var s *Sync for i := 0; i < b.N/delta; i++ { for j := 0; j < delta; j++ { tree2.Del(murmur.HashString(fmt.Sprint(j))) } b.StartTimer() s = NewSync(tree1, tree2) s.Run() b.StopTimer() if bytes.Compare(tree1.Hash(), tree2.Hash()) != 0 { b.Fatalf("%v != %v", tree1.Hash(), tree2.Hash()) } } }
func TestSyncSubTreeVersions(t *testing.T) { tree1 := NewTree() tree3 := NewTree() n := 10 var k, sk []byte var v []byte for i := 0; i < n; i++ { k = []byte(murmur.HashString(fmt.Sprint(i))) v = []byte(fmt.Sprint(i)) if i%2 == 0 { tree3.Put(k, v, 1) tree1.Put(k, v, 1) } else { for j := 0; j < 10; j++ { sk = []byte(fmt.Sprint(j)) tree1.SubPut(k, sk, v, 1) if i == 1 && j == 3 { tree3.SubPut(k, sk, []byte("another value"), 2) } else { tree3.SubPut(k, sk, v, 1) } } } } tree2 := NewTree() tree2.SubPut([]byte(murmur.HashString(fmt.Sprint(1))), []byte(fmt.Sprint(3)), []byte("another value"), 2) s := NewSync(tree1, tree2) s.Run() if bytes.Compare(tree1.Hash(), tree2.Hash()) == 0 { t.Errorf("should not be equal") } if tree1.deepEqual(tree2) { t.Errorf("should not be equal") } if bytes.Compare(tree3.Hash(), tree2.Hash()) != 0 { t.Errorf("%v and %v have hashes\n%v\n%v\nand they should be equal!", tree3.Describe(), tree2.Describe(), tree3.Hash(), tree2.Hash()) } if !tree3.deepEqual(tree2) { t.Errorf("\n%v and \n%v are unequal", tree3.Describe(), tree2.Describe()) } tree1.SubPut([]byte(murmur.HashString(fmt.Sprint(1))), []byte(fmt.Sprint(3)), []byte("another value again"), 3) s.Run() if bytes.Compare(tree3.Hash(), tree2.Hash()) == 0 { t.Errorf("should not be equal") } if tree3.deepEqual(tree2) { t.Errorf("should not be equal") } if bytes.Compare(tree1.Hash(), tree2.Hash()) != 0 { t.Errorf("%v and %v have hashes\n%v\n%v\nand they should be equal!", tree1.Describe(), tree2.Describe(), tree1.Hash(), tree2.Hash()) } if !tree1.deepEqual(tree2) { t.Errorf("\n%v and \n%v are unequal", tree1.Describe(), tree2.Describe()) } }
func main() { f, err := os.Create("cpuprofile") if err != nil { panic(err.Error()) } f2, err := os.Create("memprofile") if err != nil { panic(err.Error()) } pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() defer pprof.WriteHeapProfile(f2) benchNode := NewNodeDir("127.0.0.1:1231", "127.0.0.1:1231", "") benchNode.MustStart() var k []byte for i := 0; i < 100000; i++ { k = murmur.HashString(fmt.Sprint(i)) benchNode.Put(common.Item{ Key: k, Value: k, }) } }
func TestSyncDestructiveMatching(t *testing.T) { tree1 := NewTree() tree2 := NewTree() tree3 := NewTree() n := 1000 var k []byte var v []byte for i := 0; i < n; i++ { k = murmur.HashString(fmt.Sprint(i)) v = []byte(fmt.Sprint(i)) tree1.Put(k, v, 1) tree2.Put(k, v, 1) tree3.Put(k, v, 1) } NewSync(tree1, tree2).Destroy().Run() if !tree2.deepEqual(tree3) { t.Errorf("should be equal") } if tree1.Size() != 0 { t.Errorf("should be empty!") } tree4 := NewTree() if !tree1.deepEqual(tree4) { t.Errorf("should be equal!") } }
func TestSyncSubTree(t *testing.T) { tree1 := NewTree() n := 10 var k, sk []byte var v []byte for i := 0; i < n; i++ { k = []byte(murmur.HashString(fmt.Sprint(i))) v = []byte(fmt.Sprint(i)) if i%2 == 0 { tree1.Put(k, v, 1) } else { for j := 0; j < 10; j++ { sk = []byte(fmt.Sprint(j)) tree1.SubPut(k, sk, v, 1) } } } tree2 := NewTree() s := NewSync(tree1, tree2) s.Run() if bytes.Compare(tree1.Hash(), tree2.Hash()) != 0 { t.Errorf("%v and %v have hashes\n%v\n%v\nand they should be equal!", tree1.Describe(), tree2.Describe(), tree1.Hash(), tree2.Hash()) } if !tree1.deepEqual(tree2) { t.Errorf("\n%v and \n%v are unequal", tree1.Describe(), tree2.Describe()) } }
func benchTree(b *testing.B, n int, put, get bool) { fillBenchTree(b, n) b.StopTimer() oldprocs := runtime.GOMAXPROCS(runtime.NumCPU()) defer runtime.GOMAXPROCS(oldprocs) var keys [][]byte var vals [][]byte for i := 0; i < b.N; i++ { keys = append(keys, murmur.HashString(fmt.Sprint(rand.Int63()))) vals = append(vals, []byte(fmt.Sprint(rand.Int63()))) } var k []byte var v []byte b.StartTimer() for i := 0; i < b.N; i++ { k = benchmarkTestKeys[i%len(benchmarkTestKeys)] v = benchmarkTestValues[i%len(benchmarkTestValues)] if put { benchmarkTestTree.Put(k, v, 1) } if get { j, _, existed := benchmarkTestTree.Get(k) if bytes.Compare(j, v) != 0 { b.Fatalf("%v should contain %v, but got %v, %v", benchmarkTestTree.Describe(), v, j, existed) } } } }
func main() { f, err := os.Create("cpuprofile") if err != nil { panic(err.Error()) } f2, err := os.Create("memprofile") if err != nil { panic(err.Error()) } pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() defer pprof.WriteHeapProfile(f2) m := radix.NewTree() var k []byte for i := 0; i < 100000; i++ { k = murmur.HashString(fmt.Sprint(i)) m.Put(k, k, 1) x, _, _ := m.Get(k) if bytes.Compare(x, k) != 0 { panic("humbug") } } }
func testSubClear(t *testing.T, c testClient) { var key []byte var value []byte subTree := []byte("apa") for i := 0; i < 10; i++ { key = murmur.HashString(fmt.Sprint(i)) value = murmur.HashString(fmt.Sprint(i)) c.SSubPut(subTree, key, value) } if c.SubSize(subTree) != 10 { t.Errorf("wrong size") } c.SSubClear(subTree) if c.SubSize(subTree) != 0 { t.Errorf("wrong size") } }
func main() { a := (s(3, 30, 50, 20, 27)) fmt.Println(a) fmt.Println(a) conn := client.MustConn("localhost:9191") bytes := common.MustJSONEncode(a) conn.Put(murmur.HashString("hi"), bytes) }
func testGetPutDel(t *testing.T, c testClient) { var key []byte var value []byte for i := 0; i < 1000; i++ { key = murmur.HashString(fmt.Sprint(i)) value = murmur.HashString(fmt.Sprint(i)) if v, e := c.Get(key); v != nil || e { t.Errorf("shouldn't exist") } c.SPut(key, value) if v, e := c.Get(key); bytes.Compare(value, v) != 0 || !e { t.Fatalf("should exist, but got %v => %v, %v", key, v, e) } c.SDel(key) if v, e := c.Get(key); v != nil || e { t.Errorf("shouldn't exist, but got %v => %v, %v", key, v, e) } } }
func fillBenchTree(b *testing.B, n int) { b.StopTimer() for len(benchmarkTestKeys) < n { benchmarkTestKeys = append(benchmarkTestKeys, murmur.HashString(fmt.Sprint(len(benchmarkTestKeys)))) benchmarkTestValues = append(benchmarkTestValues, []byte(fmt.Sprint(len(benchmarkTestValues)))) } for benchmarkTestTree.Size() < n { benchmarkTestTree.Put(benchmarkTestKeys[benchmarkTestTree.Size()], benchmarkTestValues[benchmarkTestTree.Size()], 1) } b.StartTimer() }
func main() { // connect to the default local server conn := client.MustConn("localhost:9191") // create a user // try to fetch the user again data, _ := conn.Get(murmur.HashString("hi")) var found []int // to unserialize it common.MustJSONDecode(data, &found) fmt.Printf("stored and found ", found) }
func TestSyncRandomLimits(t *testing.T) { tree1 := NewTree() n := 10 var k []byte var v []byte for i := 0; i < n; i++ { k = murmur.HashString(fmt.Sprint(i)) v = []byte(fmt.Sprint(i)) tree1.Put(k, v, 1) } var keys [][]byte tree1.Each(func(key []byte, byteValue []byte, timestamp int64) bool { keys = append(keys, key) return true }) var fromKey []byte var toKey []byte var tree2 *Tree var tree3 *Tree var s *Sync for fromIndex, _ := range keys { for toIndex, _ := range keys { if fromIndex != toIndex { fromKey = keys[fromIndex] toKey = keys[toIndex] if bytes.Compare(fromKey, toKey) < 0 { tree2 = NewTree() tree1.Each(func(key []byte, byteValue []byte, timestamp int64) bool { if common.BetweenIE(key, fromKey, toKey) { tree2.Put(key, byteValue, 1) } return true }) tree3 = NewTree() s = NewSync(tree1, tree3).From(fromKey).To(toKey) s.Run() if !tree3.deepEqual(tree2) { t.Errorf("when syncing from %v to %v, %v and %v have hashes\n%v\n%v\nand they should be equal!", common.HexEncode(fromKey), common.HexEncode(toKey), tree3.Describe(), tree2.Describe(), tree3.Hash(), tree2.Hash()) } } } } } }
func BenchmarkClientAndServer(b *testing.B) { oldprocs := runtime.GOMAXPROCS(runtime.NumCPU()) defer runtime.GOMAXPROCS(oldprocs) b.StopTimer() if benchNode == nil { benchNode = NewNode("127.0.0.1:1231", "127.0.0.1:1231") benchNode.MustStart() } c := client.MustConn("127.0.0.1:1231") c.Clear() var bs [][]byte for i := 0; i < b.N; i++ { bs = append(bs, murmur.HashString(fmt.Sprint(i))) } b.StartTimer() for i := 0; i < b.N; i++ { c.Put(bs[i], bs[i]) } }
func BenchmarkServer(b *testing.B) { oldprocs := runtime.GOMAXPROCS(runtime.NumCPU()) defer runtime.GOMAXPROCS(oldprocs) b.StopTimer() if benchNode == nil { benchNode = NewNode("127.0.0.1:1231", "127.0.0.1:1231") benchNode.MustStart() } benchNode.Clear() var bs [][]byte for i := 0; i < b.N; i++ { bs = append(bs, murmur.HashString(fmt.Sprint(i))) } b.StartTimer() for i := 0; i < b.N; i++ { benchNode.Put(common.Item{ Key: bs[i], Value: bs[i], }) } }