github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/go-themis/benchmark/main.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "fmt" 6 "math/rand" 7 "net/http" 8 _ "net/http/pprof" 9 "runtime" 10 "strconv" 11 "sync" 12 "time" 13 14 "github.com/insionng/yougam/libraries/ngaut/log" 15 "github.com/insionng/yougam/libraries/pingcap/go-hbase" 16 "github.com/insionng/yougam/libraries/pingcap/go-themis" 17 "github.com/insionng/yougam/libraries/pingcap/go-themis/oracle/oracles" 18 ) 19 20 var c hbase.HBaseClient 21 var tblName1 = "themis_1" 22 var tblName2 = "themis_2" 23 var o = oracles.NewLocalOracle() 24 25 var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") 26 27 func RandStringRunes(n int) string { 28 b := make([]rune, n) 29 for i := range b { 30 b[i] = letterRunes[rand.Intn(len(letterRunes))] 31 } 32 return string(b) 33 } 34 35 // some comments 36 func init() { 37 rand.Seed(time.Now().UnixNano()) 38 log.Errorf("create conn") 39 var err error 40 c, err = hbase.NewClient([]string{"localhost"}, "/hbase") 41 if err != nil { 42 log.Fatal(err) 43 } 44 log.Errorf("create conn done") 45 } 46 47 func createTable(tblName string) { 48 // create new hbase table for store 49 t := hbase.NewTableDesciptor(tblName) 50 cf := hbase.NewColumnFamilyDescriptor("cf") 51 cf.AddAttr("THEMIS_ENABLE", "true") 52 t.AddColumnDesc(cf) 53 err := c.CreateTable(t, nil) 54 if err != nil { 55 log.Fatal(err) 56 } 57 } 58 59 func dropTable(tblName string) { 60 c.DisableTable(tblName) 61 c.DropTable(tblName) 62 } 63 64 func main() { 65 runtime.GOMAXPROCS(runtime.NumCPU()) 66 log.SetLevelByString("debug") 67 log.Errorf("begin create table") 68 dropTable(tblName1) 69 createTable(tblName1) 70 dropTable(tblName2) 71 createTable(tblName2) 72 log.Errorf("create table finish") 73 74 go func() { 75 log.Error(http.ListenAndServe("localhost:8889", nil)) 76 }() 77 78 wg := &sync.WaitGroup{} 79 for i := 0; i < 10; i++ { 80 wg.Add(1) 81 go func() { 82 batchInsert(1, 50000) 83 wg.Done() 84 }() 85 } 86 wg.Wait() 87 } 88 89 func batchInsert(round int, rowCount int) { 90 for i := 0; i < round; i++ { 91 log.Errorf("begin batch insert") 92 ct := time.Now() 93 tx, _ := themis.NewTxn(c, o) 94 for j := 0; j < rowCount; j++ { 95 put := hbase.NewPut([]byte(RandStringRunes(20))) 96 put.AddValue([]byte("cf"), []byte("q"), bytes.Repeat([]byte{'A'}, 80)) 97 tx.Put(tblName1, put) 98 } 99 err := tx.Commit() 100 if err != nil { 101 log.Error(err) 102 } 103 log.Errorf("insert %d row data, consum time %s", rowCount, time.Since(ct)) 104 } 105 } 106 107 func insert(rowCount int) { 108 ct := time.Now() 109 wg := sync.WaitGroup{} 110 for i := 0; i < rowCount; i++ { 111 wg.Add(1) 112 go func(i int) { 113 defer wg.Done() 114 115 tx, _ := themis.NewTxn(c, o) 116 117 put := hbase.NewPut([]byte(fmt.Sprintf("Row_%d", i))) 118 put.AddValue([]byte("cf"), []byte("q"), []byte(strconv.Itoa(i))) 119 120 put2 := hbase.NewPut([]byte(fmt.Sprintf("SRow_%d", i))) 121 put2.AddValue([]byte("cf"), []byte("q"), []byte(strconv.Itoa(i))) 122 123 tx.Put(tblName1, put) 124 tx.Put(tblName2, put2) 125 126 err := tx.Commit() 127 if err != nil { 128 log.Error(err) 129 } 130 }(i) 131 } 132 133 wg.Wait() 134 log.Errorf("insert %d row data, consum time %s", rowCount, time.Since(ct)) 135 } 136 137 func randomGet(rowCount int) { 138 ct := time.Now() 139 wg := sync.WaitGroup{} 140 141 for i := 0; i < rowCount; i++ { 142 wg.Add(1) 143 go func(count int) { 144 defer wg.Done() 145 146 tx, _ := themis.NewTxn(c, o) 147 rowKey := fmt.Sprintf("Row_%d", rand.Intn(rowCount)) 148 get := hbase.NewGet([]byte(rowKey)) 149 value, err := tx.Get(tblName1, get) 150 if err != nil { 151 log.Errorf("get rowkey: %s, has a error", rowKey, err) 152 } else { 153 log.Errorf("get rowkey: %s, value:%s", rowKey, value) 154 } 155 }(rowCount) 156 } 157 158 wg.Wait() 159 log.Errorf("random get %d row data, consum time %s", rowCount, time.Since(ct)) 160 }