av_charge_statis_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package charge
  2. import (
  3. "bytes"
  4. "context"
  5. "strconv"
  6. "testing"
  7. "time"
  8. model "go-common/app/job/main/growup/model/charge"
  9. xtime "go-common/library/time"
  10. . "github.com/smartystreets/goconvey/convey"
  11. )
  12. func Test_AvChargeStatis(t *testing.T) {
  13. Convey("AvChargeStatis", t, func() {
  14. _, err := d.AvChargeStatis(context.Background(), 0, 2000)
  15. So(err, ShouldBeNil)
  16. })
  17. }
  18. func Test_InsertAvChargeStatisBatch(t *testing.T) {
  19. Convey("InsertAvChargeStatisBatch", t, func() {
  20. c := context.Background()
  21. d.db.Exec(c, "DELETE FROM av_charge_statis where av_id = 11")
  22. avChargeStatis := []*model.AvChargeStatis{}
  23. value := &model.AvChargeStatis{
  24. AvID: 11,
  25. MID: 11,
  26. TagID: 11,
  27. }
  28. avChargeStatis = append(avChargeStatis, value)
  29. vals := assembleAvChargeStatis(avChargeStatis)
  30. count, err := d.InsertAvChargeStatisBatch(c, vals)
  31. So(err, ShouldBeNil)
  32. So(count, ShouldEqual, 1)
  33. d.db.Exec(c, "DELETE FROM av_charge_statis where av_id = 11")
  34. })
  35. }
  36. func benchmarkInsertAvChargeStatisBatch(size int64, b *testing.B) {
  37. avChargeStatis := make([]*model.AvChargeStatis, size)
  38. var i int64
  39. for i = 0; i < size; i++ {
  40. avChargeStatis[i] = &model.AvChargeStatis{
  41. AvID: i,
  42. MID: i,
  43. TagID: i,
  44. IsOriginal: int(i),
  45. UploadTime: xtime.Time(time.Now().Unix()),
  46. TotalCharge: i,
  47. }
  48. }
  49. vals := assembleAvChargeStatis(avChargeStatis)
  50. for n := 0; n < b.N; n++ {
  51. d.InsertAvChargeStatisBatch(context.Background(), vals)
  52. }
  53. }
  54. func BenchmarkInsertAvChargeStatisBatch100(b *testing.B) { benchmarkInsertAvChargeStatisBatch(100, b) }
  55. func BenchmarkInsertAvChargeStatisBatch1000(b *testing.B) { benchmarkInsertAvChargeStatisBatch(1000, b) }
  56. func BenchmarkInsertAvChargeStatisBatch10000(b *testing.B) {
  57. benchmarkInsertAvChargeStatisBatch(10000, b)
  58. }
  59. func assembleAvChargeStatis(avChargeStatis []*model.AvChargeStatis) (vals string) {
  60. var buf bytes.Buffer
  61. for _, row := range avChargeStatis {
  62. buf.WriteString("(")
  63. buf.WriteString(strconv.FormatInt(row.AvID, 10))
  64. buf.WriteByte(',')
  65. buf.WriteString(strconv.FormatInt(row.MID, 10))
  66. buf.WriteByte(',')
  67. buf.WriteString(strconv.FormatInt(row.TagID, 10))
  68. buf.WriteByte(',')
  69. buf.WriteString(strconv.Itoa(row.IsOriginal))
  70. buf.WriteByte(',')
  71. buf.WriteString(strconv.FormatInt(row.TotalCharge, 10))
  72. buf.WriteByte(',')
  73. buf.WriteString(row.UploadTime.Time().Format(_layout))
  74. buf.WriteString(")")
  75. buf.WriteByte(',')
  76. }
  77. if buf.Len() > 0 {
  78. buf.Truncate(buf.Len() - 1)
  79. }
  80. vals = buf.String()
  81. buf.Reset()
  82. return
  83. }