print.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. randc "crypto/rand"
  6. "encoding/json"
  7. "fmt"
  8. "log"
  9. "math/big"
  10. "math/rand"
  11. "sort"
  12. "sync"
  13. "time"
  14. "unsafe"
  15. "github.com/gin-gonic/gin"
  16. "go.mongodb.org/mongo-driver/bson"
  17. "go.mongodb.org/mongo-driver/mongo"
  18. )
  19. func Printr(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  20. loc, _ := time.LoadLocation("Local")
  21. date := time.Now().Format("2006-01-02")
  22. startDate := date + " 00:00:00"
  23. startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", startDate, loc)
  24. endDate := date + " 23:59:59"
  25. endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", endDate, loc)
  26. fmt.Println(startTime.Unix())
  27. fmt.Println(endTime.Unix())
  28. return "success", nil
  29. }
  30. // 生成百万数据
  31. func GenData(_ *gin.Context, apictx *ApiSession) (interface{}, error) {
  32. var wg sync.WaitGroup
  33. for i := 0; i < 100; i++ {
  34. wg.Add(1)
  35. go insertData(apictx, &wg)
  36. }
  37. wg.Wait()
  38. return true, nil
  39. }
  40. // 聚合查询百万数据
  41. func SearchData(_ *gin.Context, apictx *ApiSession) (interface{}, error) {
  42. dataStatResult := make(chan bson.M)
  43. var output ResultSlice
  44. var wg sync.WaitGroup
  45. // 82个gorutine 执行聚合
  46. for i := 18; i < 100; i++ {
  47. wg.Add(1)
  48. go DataAggregate(apictx, i, dataStatResult, &wg)
  49. }
  50. // 从管道中获取聚合结果 保存在数组中
  51. for value := range dataStatResult {
  52. output = append(output, OutPut{
  53. Age: value["age"].(int32),
  54. Sex: value["sex"].(int32),
  55. Total: value["total"].(int32),
  56. AvgSalary: value["avgSalary"].(float64),
  57. })
  58. if len(output) == 164 { // 如果大于164,不会跳出;如果小于164,wg.done != wg.add ,wg.wait阻塞
  59. break
  60. }
  61. }
  62. wg.Wait()
  63. //倒序排列
  64. sort.Sort(output)
  65. for _, v := range output {
  66. result, err := json.Marshal(&v)
  67. if err != nil {
  68. fmt.Println("json.marshal failed, err:", err)
  69. return nil, err
  70. }
  71. fmt.Println(string(result))
  72. }
  73. return output, nil
  74. }
  75. func insertData(apictx *ApiSession, wg *sync.WaitGroup) {
  76. collectName := "aggregate-test"
  77. rowNum := 10000
  78. for i := 0; i < rowNum; i++ {
  79. repo.RepoAddDoc(apictx.CreateRepoCtx(), collectName, &model.AggregateTest{
  80. Name: randName(6),
  81. Age: randAge(),
  82. Sex: randSex(),
  83. Salary: randSalary(),
  84. })
  85. }
  86. wg.Done()
  87. }
  88. func randSex() *int {
  89. result, _ := randc.Int(randc.Reader, big.NewInt(2))
  90. sex := int(result.Int64())
  91. return &sex
  92. }
  93. func randAge() int {
  94. result, _ := randc.Int(randc.Reader, big.NewInt(82))
  95. return int(result.Int64() + 18)
  96. }
  97. func randSalary() int {
  98. result, _ := randc.Int(randc.Reader, big.NewInt(10000))
  99. return int(result.Int64() + 2000)
  100. }
  101. const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  102. var src = rand.NewSource(time.Now().UnixNano())
  103. const (
  104. // 6 bits to represent a letter index
  105. letterIdBits = 6
  106. // All 1-bits as many as letterIdBits
  107. letterIdMask = 1<<letterIdBits - 1
  108. letterIdMax = 63 / letterIdBits
  109. )
  110. func randName(n int) string {
  111. b := make([]byte, n)
  112. // A rand.Int63() generates 63 random bits, enough for letterIdMax letters!
  113. for i, cache, remain := n-1, src.Int63(), letterIdMax; i >= 0; {
  114. if remain == 0 {
  115. cache, remain = src.Int63(), letterIdMax
  116. }
  117. if idx := int(cache & letterIdMask); idx < len(letters) {
  118. b[i] = letters[idx]
  119. i--
  120. }
  121. cache >>= letterIdBits
  122. remain--
  123. }
  124. return *(*string)(unsafe.Pointer(&b))
  125. }
  126. func genPipeline(age int) (bson.D, bson.D, bson.D) {
  127. matchStage := bson.D{
  128. {"$match", bson.D{
  129. {"age",
  130. bson.D{
  131. {"$eq", age},
  132. }},
  133. }},
  134. }
  135. groupStage := bson.D{
  136. {"$group", bson.D{
  137. {"_id", bson.D{
  138. {"age", "$age"},
  139. {"sex", "$sex"},
  140. }},
  141. {"age", bson.D{
  142. {"$first", "$age"},
  143. }},
  144. {"sex", bson.D{
  145. {"$first", "$sex"},
  146. }},
  147. {"total", bson.D{
  148. {"$sum", 1},
  149. }},
  150. {"avgSalary", bson.D{
  151. {"$avg", "$salary"},
  152. }},
  153. }},
  154. }
  155. projectStage := bson.D{
  156. {"$project", bson.D{
  157. {"_id", 0},
  158. {"age", 1},
  159. {"sex", 1},
  160. {"total", 1},
  161. {"avgSalary", 1},
  162. }},
  163. }
  164. return matchStage, groupStage, projectStage
  165. }
  166. func DataAggregate(apictx *ApiSession, age int, resultChan chan bson.M, wg *sync.WaitGroup) {
  167. matchStage, groupStage, projectStage := genPipeline(age)
  168. // opts := options.Aggregate().SetMaxTime(15 * time.Second)
  169. cursor, err := apictx.Svc.Mongo.GetCollection("aggregate-test").Aggregate(apictx.CreateRepoCtx().Ctx, mongo.Pipeline{matchStage, groupStage, projectStage})
  170. if err != nil {
  171. log.Println(err)
  172. }
  173. //打印文档内容
  174. var results []bson.M
  175. if err = cursor.All(apictx.CreateRepoCtx().Ctx, &results); err != nil {
  176. log.Println(err)
  177. }
  178. log.Printf("%#v\n", results)
  179. for _, result := range results {
  180. resultChan <- result
  181. }
  182. wg.Done()
  183. }
  184. type OutPut struct {
  185. Age int32 `json:"age"`
  186. Sex int32 `json:"sex"`
  187. Total int32 `json:"total"`
  188. AvgSalary float64 `json:"avg_salary"`
  189. }
  190. type ResultSlice []OutPut
  191. func (a ResultSlice) Len() int { // 重写 Len() 方法
  192. return len(a)
  193. }
  194. func (a ResultSlice) Swap(i, j int) { // 重写 Swap() 方法
  195. a[i], a[j] = a[j], a[i]
  196. }
  197. func (a ResultSlice) Less(i, j int) bool { // 重写 Less() 方法, 从大到小排序
  198. return a[j].Age < a[i].Age
  199. }