main.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. _ "github.com/taosdata/driver-go/v3/taosSql"
  6. "log"
  7. "math/rand"
  8. "sync"
  9. "time"
  10. )
  11. var DateFmtYYYYMMDDHHmmss = "2006-01-02 15:04:05"
  12. func main() {
  13. var taosDSN = "root:taosdata@tcp(192.168.0.153:6030)/avata_1"
  14. taos, err := sql.Open("taosSql", taosDSN)
  15. if err != nil {
  16. log.Fatalln("failed to connect TDengine, err:", err)
  17. return
  18. }
  19. defer taos.Close()
  20. // 测试插入数据
  21. now := time.Now().Add(-10 * time.Hour)
  22. list := getObject()
  23. fmt.Println("开始 ", time.Now().Format(DateFmtYYYYMMDDHHmmss))
  24. wg := sync.WaitGroup{}
  25. for z := 0; z < 100; z++ {
  26. wg.Add(1)
  27. go func(z int) {
  28. defer wg.Done()
  29. p := z * 150
  30. c := p + 150
  31. data := ""
  32. for ; p < c; p++ {
  33. amount1 := 0.00
  34. amount2 := 0.00
  35. amount3 := 0.00
  36. amount4 := 0.00
  37. for i := 50; i > 1; i-- {
  38. day := now.AddDate(0, 0, -i)
  39. for j := 1; j < 11; j++ {
  40. //for j := 0; j < 5; j++ {
  41. //num := 1
  42. d := ""
  43. rand.Seed(time.Now().UnixNano())
  44. addMinute := rand.Intn(90)
  45. operation := rand.Intn(4) + 1
  46. money := rand.Intn(1000)
  47. operationType := 1
  48. if list[p].Pid != 0 {
  49. operation = 2
  50. }
  51. if operation == 1 {
  52. operationType = rand.Intn(3) + 1
  53. amount1 = amount1 + float64(money)
  54. d = fmt.Sprintf("amount_%d_%d_%d USING amounts TAGS (%d, %d, %d, %d, %d) values (%d,%f) ", list[p].Chain, list[p].Uid, list[p].Pid, list[p].Chain, list[p].Uid, list[p].Pid, operation, operationType, day.Add(time.Duration(j*addMinute)*time.Minute).UnixMilli(), amount1)
  55. }
  56. if operation == 2 {
  57. amount2 = amount2 + float64(money)*0.07
  58. d = fmt.Sprintf("amount_%d_%d_%d USING amounts TAGS (%d, %d, %d, %d, %d) values (%d,%f) ", list[p].Chain, list[p].Uid, list[p].Pid, list[p].Chain, list[p].Uid, list[p].Pid, operation, operationType, day.Add(time.Duration(j*addMinute)*time.Minute).UnixMilli(), amount2)
  59. }
  60. if operation == 3 {
  61. amount3 = amount3 + float64(money)
  62. d = fmt.Sprintf("amount_%d_%d_%d USING amounts TAGS (%d, %d, %d, %d, %d) values (%d,%f) ", list[p].Chain, list[p].Uid, list[p].Pid, list[p].Chain, list[p].Uid, list[p].Pid, operation, operationType, day.Add(time.Duration(j*addMinute)*time.Minute).UnixMilli(), amount3)
  63. }
  64. if operation == 4 {
  65. d = fmt.Sprintf("amount_%d_%d_%d USING amounts TAGS (%d, %d, %d, %d, %d) values (%d,%f) ", list[p].Chain, list[p].Uid, list[p].Pid, list[p].Chain, list[p].Uid, list[p].Pid, operation, operationType, day.Add(time.Duration(j*addMinute)*time.Minute).UnixMilli(), amount4)
  66. }
  67. //data = fmt.Sprintf(" amount_%d_%d_%d USING amounts TAGS (%d, %d, %d, %d, %d) values%s ", list[p].Chain, list[p].Uid, list[p].Pid, list[p].Chain, list[p].Uid, list[p].Pid, operation, operationType, data)
  68. data = fmt.Sprintf("%s %s", data, d)
  69. }
  70. }
  71. if (p+1)%10 == 0 {
  72. data = fmt.Sprintf("insert into %s", data)
  73. //fmt.Println(data)
  74. _, err = taos.Exec(data)
  75. if err != nil {
  76. fmt.Println("failed to insert, err:", err)
  77. return
  78. }
  79. data = ""
  80. }
  81. }
  82. fmt.Println("完成 ", time.Now().Format(DateFmtYYYYMMDDHHmmss), " z: ", z, " 当前cup: ", list[p])
  83. }(z)
  84. wg.Wait()
  85. fmt.Println("结束 ", time.Now().Format(DateFmtYYYYMMDDHHmmss))
  86. }
  87. }
  88. type TxObject struct {
  89. Chain int
  90. Uid int
  91. Pid int
  92. //Account string
  93. }
  94. func getObject() []TxObject {
  95. var list []TxObject
  96. uid := 1
  97. pid := 1
  98. //for i := 0; i < num; i++ {
  99. //3 条链
  100. for j := 1; j < 4; j++ {
  101. var chain, user, project int
  102. chain = j
  103. // 用户 1000
  104. for k := 1; k < 1001; k++ {
  105. //for k := 1; k < 21; k++ {
  106. user = uid
  107. // 项目 5
  108. for t := 1; t < 6; t++ {
  109. if pid%5 == 0 || pid%7 == 0 {
  110. project = 0
  111. } else {
  112. project = pid
  113. }
  114. pid++
  115. list = append(list, TxObject{
  116. Chain: chain,
  117. Uid: user,
  118. Pid: project,
  119. })
  120. }
  121. uid++
  122. }
  123. }
  124. //}
  125. return list
  126. }