testperf.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math/rand"
  6. "os"
  7. "git.wecise.com/wecise/odb-go/odb"
  8. "git.wecise.com/wecise/odb-go/odbc"
  9. "gitee.com/wecisecode/util/logger"
  10. )
  11. type PerfConfig struct {
  12. Schema string `json:"schema"`
  13. Rate float64 `json:"rate"`
  14. Keyspace string `json:"keyspace"`
  15. }
  16. func main() {
  17. config_file := "config.json"
  18. if len(os.Args) >= 2 {
  19. config_file = os.Args[1]
  20. }
  21. conf := &PerfConfig{}
  22. if content, err := os.ReadFile(config_file); err != nil {
  23. logger.Errorf("read config file : %s, error: %v", config_file, err)
  24. } else {
  25. if err := json.Unmarshal([]byte(content), conf); err != nil {
  26. logger.Errorf("config error: %v", err)
  27. } else {
  28. logger.Infof("From: %v", conf)
  29. }
  30. }
  31. logger.SetConsole(true)
  32. // 根据参数配置获取 ODBC 连接
  33. // 参数设置为程序默认配置
  34. // 可以通过命令行,环境变量 或 与应用同名的.conf配置文件 设置
  35. // 远程连接需要在白名单中增加本地IP
  36. odbc := odbc.ODBC(&odb.Config{
  37. Hosts: []string{"47.92.151.165:11001"},
  38. Keyspace: conf.Keyspace,
  39. User: fmt.Sprint("测试客户端"),
  40. Pass: "********",
  41. PoolSize: 20,
  42. Debug: true,
  43. })
  44. // 结束前关闭 ODBC 连接
  45. defer odbc.Close()
  46. /* _, err := odbc.Query( conf.Schema ).Do()
  47. if err != nil {
  48. logger.Info("Query error:" + err.Error())
  49. return
  50. }*/
  51. mql1 := `insert into c14490 (id, name) values (?, ? )`
  52. count_1 := int(450 * conf.Rate)
  53. for i := 0; i < count_1; i++ {
  54. _, err := odbc.Query(mql1, fmt.Sprintf("c14490:c%d", i), fmt.Sprintf("c14490_%d", i)).Do()
  55. if err != nil {
  56. logger.Errorf("%v", err)
  57. return
  58. }
  59. }
  60. mql2 := `insert into c14409 (id, name) values (?, ? )`
  61. count_2 := int(24000 * conf.Rate)
  62. for i := 0; i < count_2; i++ {
  63. _, err := odbc.Query(mql2, fmt.Sprintf("c14409:c%d", i), fmt.Sprintf("c14409_%d", i)).Do()
  64. if err != nil {
  65. logger.Errorf("%v", err)
  66. return
  67. }
  68. }
  69. mql3 := `insert into c19900 (id, name) values (?, ? )`
  70. count_3 := int(120000 * conf.Rate)
  71. for i := 0; i < count_3; i++ {
  72. _, err := odbc.Query(mql3, fmt.Sprintf("c19900:c%d", i), fmt.Sprintf("c19900_%d", i)).Do()
  73. if err != nil {
  74. logger.Errorf("%v", err)
  75. return
  76. }
  77. }
  78. mql4 := `insert into cnode (id, name) values (?, ? )`
  79. count_c := int(500000 * conf.Rate)
  80. for i := 0; i < count_c; i++ {
  81. _, err := odbc.Query(mql4, fmt.Sprintf("cnode:c%d", i), fmt.Sprintf("cnode_%d", i)).Do()
  82. if err != nil {
  83. logger.Errorf("%v", err)
  84. return
  85. }
  86. }
  87. mql5 := `insert into cnode (id, name) values (?, ? )`
  88. count_t := int(120000 * conf.Rate)
  89. for i := 0; i < count_t; i++ {
  90. _, err := odbc.Query(mql5, fmt.Sprintf("tnode:c%d", i), fmt.Sprintf("tnode_%d", i)).Do()
  91. if err != nil {
  92. logger.Errorf("%v", err)
  93. return
  94. }
  95. }
  96. //两种连接,每个52
  97. for i := 0; i < count_1; i++ {
  98. // connect
  99. //{
  100. // "from":"GigabitEthernet1",
  101. // "to" :"GigabitEthernet1"
  102. //}
  103. // connectip
  104. //{
  105. // "from":"192.168.1.#11",
  106. // "to" :"192.169.1.#11"
  107. //}
  108. // c14490 ==> 52 ==> c14409
  109. for j := 0; j < 52; j++ {
  110. todevice := (j + i) % count_2
  111. from := rand.Intn(999) + 1
  112. to := rand.Intn(100) + 1
  113. _, err := odbc.Query(fmt.Sprintf(`create ('c14490:c%d')-[:connect{"from":"GigabitEthernet%d","to":"GigabitEthernet%d"}]->('c14409:c%d')`, i, from, to, todevice)).Do()
  114. if err != nil {
  115. logger.Errorf("%v", err)
  116. return
  117. }
  118. }
  119. for j := 0; j < 52; j++ {
  120. todevice := (j + i) % count_2
  121. from := rand.Intn(999) + 1
  122. to := rand.Intn(99) + 1
  123. _, err := odbc.Query(fmt.Sprintf(`create ('c14490:c%d')-[:connectip{"from":"192.168.1.%d","to":"192.168.1.%d"}]->('c14409:c%d')`, i, from, to, todevice)).Do()
  124. if err != nil {
  125. logger.Errorf("%v", err)
  126. return
  127. }
  128. }
  129. }
  130. //一种连接,每个2个
  131. for i := 0; i < count_2; i++ {
  132. for j := 0; j < 2; j++ {
  133. todevice := (j + i) % count_3
  134. from := rand.Intn(99) + 1
  135. to := rand.Intn(49) + 1
  136. _, err := odbc.Query(fmt.Sprintf(`create ('c14409:c%d')-[:connect{"from":"GigabitEthernet%d","to":"GigabitEthernet%d"}]->('c19900:c%d')`, i, from, to, todevice)).Do()
  137. if err != nil {
  138. logger.Errorf("%v", err)
  139. return
  140. }
  141. }
  142. }
  143. //一种连接,连接两种设备
  144. for i := 0; i < count_3; i++ {
  145. todevice := i % count_c
  146. from := rand.Intn(49) + 1
  147. to := rand.Intn(9) + 1
  148. _, err := odbc.Query(fmt.Sprintf(`create ('c19900:c%d')-[:connect{"from":"GigabitEthernet%d","to":"GigabitEthernet%d"}]->('cnode:c%d')`, i, from, to, todevice)).Do()
  149. if err != nil {
  150. logger.Errorf("%v", err)
  151. return
  152. }
  153. todevice = i % count_t
  154. _, err = odbc.Query(fmt.Sprintf(`create ('c19900:c%d')-[:connect{"from":"GigabitEthernet%d","to":"GigabitEthernet%d"}]->('tnode:c%d')`, i, from, to, todevice)).Do()
  155. if err != nil {
  156. logger.Errorf("%v", err)
  157. return
  158. }
  159. }
  160. }