Advertisement
Guest User

Untitled

a guest
Jul 19th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "log"
  6. "os"
  7. "sync"
  8. "sync/atomic"
  9. "time"
  10. "path/filepath"
  11. "flag"
  12. "math/rand"
  13. "github.com/tarantool/go-tarantool"
  14. )
  15.  
  16. const (
  17. HEX_LETTERS_DIGITS = "abcdef0123456789"
  18. )
  19.  
  20. type Flags struct {
  21. conc int
  22. total int
  23. keylen int
  24. }
  25.  
  26. func usage() {
  27. fmt.Printf("Usage %s (raw|getinsert|put)\n", filepath.Base(os.Args[0]))
  28. os.Exit(1)
  29. }
  30.  
  31. func randomString(keylen int) string {
  32. l := len(HEX_LETTERS_DIGITS)
  33. b := make([]byte, keylen)
  34. for i := 0; i < keylen; i++ {
  35. b[i] = HEX_LETTERS_DIGITS[rand.Intn(l)]
  36. }
  37. return string(b)
  38. }
  39.  
  40. type WorkerFunc func(*tarantool.Connection, int, int, *Progress)
  41.  
  42. type Progress struct {
  43. total int32
  44. count int32
  45. }
  46.  
  47. func NewProgress(total int) *Progress {
  48. return &Progress{total: int32(total)}
  49. }
  50.  
  51. func (p *Progress) increment() {
  52. new_count := atomic.AddInt32(&p.count, 1)
  53. if (new_count % (p.total / 10)) == 0 {
  54. log.Printf("Completed %d requests\n", new_count)
  55. }
  56. }
  57.  
  58. func microTime() int64 {
  59. return time.Now().UnixNano() / int64(time.Microsecond)
  60. }
  61.  
  62. func runRaw(client *tarantool.Connection, keylen int, numreq int, p *Progress) {
  63. schema := client.Schema
  64. space1 := schema.Spaces["data"]
  65.  
  66. for i := 0; i < numreq; i++ {
  67. key := randomString(keylen)
  68. val := randomString(keylen)
  69. ts_ping := microTime()
  70. client.Insert(space1.Id, []interface{}{key, val, ts_ping})
  71. // if err != nil && err.Code != 0x8003 {
  72. // log.Fatalf("Error inserting: %s", err.Error())
  73. // }
  74. p.increment()
  75. }
  76. }
  77.  
  78. func runFuncGetInsert(client *tarantool.Connection, keylen int, numreq int, p *Progress) {
  79.  
  80. for i := 0; i < numreq; i++ {
  81. key := randomString(keylen)
  82. val := randomString(keylen)
  83. ts_ping := microTime()
  84. _, err := client.Call("GoHandler_GetInsert", []interface{}{key, val, ts_ping})
  85. if err != nil {
  86. log.Fatalf("Error calling function: %s", err.Error())
  87. }
  88. p.increment()
  89. }
  90.  
  91. // res, err := client.Call("GoHandler_GetVal", []interface{}{0})
  92. res, err := client.Call("GoHandler_GetList", []interface{}{0})
  93. if err != nil {
  94. log.Fatalf("Error calling function: %s", err.Error())
  95. }
  96. log.Printf("id = %d, code = %d\n%v\n", res.RequestId, res.Code, res.Data)
  97.  
  98. res_tuples := res.Tuples()
  99. for i, row := range res_tuples {
  100. log.Printf("[%4d] = %s\n", i, row)
  101. }
  102. }
  103.  
  104. func runFuncPut(client *tarantool.Connection, keylen int, numreq int, p *Progress) {
  105.  
  106. for i := 0; i < numreq; i++ {
  107. key := randomString(keylen)
  108. val := randomString(keylen)
  109. ts_ping := microTime()
  110. _, err := client.Call("GoHandler_Put", []interface{}{key, val, ts_ping})
  111. if err != nil {
  112. log.Fatalf("Error calling function: %s", err.Error())
  113. }
  114. p.increment()
  115. }
  116. }
  117.  
  118. func runHarnessWorker(worker WorkerFunc, keylen int, numreq int, p *Progress) {
  119. server := "127.0.0.1:3401"
  120. opts := tarantool.Opts{
  121. Timeout: 500 * time.Millisecond,
  122. Reconnect: 1 * time.Second,
  123. MaxReconnects: 3,
  124. User: "push",
  125. Pass: "push_pass",
  126. }
  127.  
  128. client, err := tarantool.Connect(server, opts)
  129. if err != nil {
  130. log.Fatalf("Failed to connect: %s", err.Error())
  131. }
  132.  
  133. defer client.Close()
  134.  
  135. log.Printf("Connected to %s, will run %d requests\n", server, numreq)
  136.  
  137. worker(client, keylen, numreq, p)
  138. }
  139.  
  140. func runHarness(flags Flags, worker WorkerFunc) {
  141. rand.Seed(time.Now().UTC().UnixNano())
  142.  
  143. log.Printf("Key length: %d\n", flags.keylen)
  144.  
  145. var wg sync.WaitGroup
  146. wg.Add(flags.conc)
  147.  
  148. now := time.Now()
  149. progress := NewProgress(flags.total)
  150.  
  151. for i := 0; i < flags.conc; i++ {
  152. numreq := flags.total / flags.conc
  153. keylen := flags.keylen
  154. go func(keylen, numreq int) {
  155. defer wg.Done()
  156. runHarnessWorker(worker, keylen, numreq, progress)
  157. }(keylen, numreq)
  158. }
  159.  
  160. wg.Wait()
  161.  
  162. since := time.Since(now)
  163.  
  164. log.Printf("Elapsed time: %s\n", since)
  165. log.Printf("Ops per second: %.2f\n", float64(flags.total)/since.Seconds())
  166. }
  167.  
  168. func main() {
  169. var flags Flags
  170. flag.IntVar(&flags.conc, "c", 20, "Concurrency")
  171. flag.IntVar(&flags.total, "n", 100000, "Total count")
  172. flag.IntVar(&flags.keylen, "l", 40, "Key length")
  173. flag.Parse()
  174.  
  175. nargs := flag.NArg()
  176. args := flag.Args()
  177.  
  178. if nargs != 1 {
  179. usage()
  180. }
  181. if flags.conc < 1 || flags.total < 1 || flags.keylen < 10 {
  182. usage()
  183. }
  184.  
  185. fmt.Printf("Now: %d\n", microTime())
  186.  
  187. command := args[0]
  188. if command == "raw" {
  189. log.Printf("Raw test, c = %d, n = %d\n", flags.conc, flags.total)
  190. runHarness(flags, runRaw)
  191. } else if command == "getinsert" {
  192. log.Printf("Func get/insert test, c = %d, n = %d\n", flags.conc, flags.total)
  193. runHarness(flags, runFuncGetInsert)
  194. } else if command == "put" {
  195. log.Printf("Func put test, c = %d, n = %d\n", flags.conc, flags.total)
  196. runHarness(flags, runFuncPut)
  197. } else {
  198. usage()
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement