base_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package base
  2. import "testing"
  3. import "time"
  4. import "fmt"
  5. import "math/rand"
  6. import "unsafe"
  7. func TestTimezoneTrans(t *testing.T) {
  8. trans, err := NewTimezoneTrans("America/New_York", 7 * 3600, "UTC", 0, 0)
  9. if err != nil {
  10. fmt.Println(err)
  11. }
  12. //est time 2013-08-29 15:00:00
  13. est := "2013.08.30 12:41:48"
  14. ti, err := time.Parse("2006.01.02 15:04:05", est)
  15. if err != nil {
  16. fmt.Println(err)
  17. }
  18. offset := trans.Offset(int(ti.Unix()))
  19. newt := time.Unix(ti.Unix() + int64(offset), 0)
  20. //最终转换过的时间
  21. fmt.Println(newt)
  22. }
  23. var beginTime int64
  24. // Using a fixed seed will produce the same output on every run.
  25. var r = rand.New(rand.NewSource(time.Now().UnixNano()))
  26. var lastTick TickGo
  27. var count int64
  28. func init() {
  29. t, _ := time.Parse("2006.01.02 15:04:05", "2012.01.01 00:00:00")
  30. beginTime = t.Unix()
  31. lastTick.Time = int32(beginTime)
  32. firstprice := float32(1.0)
  33. if lastTick.Time % 2 == 0 {
  34. lastTick.Ask = firstprice + r.Float32() / float32(10.0)
  35. lastTick.Bid = firstprice + r.Float32() / float32(10.0)
  36. }
  37. }
  38. func randTick() TickGo {
  39. count++
  40. if count % 10 == 0 {
  41. beginTime++
  42. }
  43. lastTick.Time = int32(beginTime)
  44. randint := r.Int63()
  45. if randint % 2 == 0 {
  46. lastTick.Ask += r.Float32() / float32(10.0)
  47. lastTick.Bid += r.Float32() / float32(10.0)
  48. } else {
  49. lastTick.Ask -= r.Float32() / float32(10.0)
  50. lastTick.Bid -= r.Float32() / float32(10.0)
  51. }
  52. return lastTick
  53. }
  54. func BenchmarkRandTick(b *testing.B) {
  55. for i := 0; i < b.N; i++ {
  56. randTick()
  57. }
  58. }
  59. func BenchmarkCandleCalc(b *testing.B) {
  60. b.StopTimer()
  61. candle, err := NewCandle(60, 2, nil, 0)
  62. if err != nil {
  63. b.Error(err)
  64. return
  65. }
  66. trans, err := NewTimezoneTrans("America/New_York", 7 * 3600, "UTC", 0, 0)
  67. if err != nil {
  68. b.Error(err)
  69. }
  70. candle.SetTimezoneTrans(trans)
  71. b.StartTimer()
  72. for i := 0; i < b.N; i++ {
  73. tk := randTick()
  74. num := candle.UpdateTick((*Tick)(unsafe.Pointer(&tk)))
  75. if num == 0 {
  76. ohlc := Ohlc{}
  77. candle.Next(&ohlc)
  78. var _ = ohlc.ToGOStruct()
  79. } else {
  80. for n := 0; n < num; n++ {
  81. ohlc := Ohlc{}
  82. candle.Next(&ohlc)
  83. var _ = ohlc.ToGOStruct()
  84. }
  85. }
  86. }
  87. }
  88. func BenchmarkCandleSimple(b *testing.B) {
  89. b.StopTimer()
  90. trans, err := NewTimezoneTrans("America/New_York", 7 * 3600, "UTC", 0, 0)
  91. if err != nil {
  92. b.Error(err)
  93. }
  94. candle, err := NewCandleSimple(60, 2, trans)
  95. if err != nil {
  96. b.Error(err)
  97. return
  98. }
  99. b.StartTimer()
  100. for i := 0; i < b.N; i++ {
  101. tk := randTick()
  102. num := candle.Set(tk)
  103. for i := 0; i <= num; i++ {
  104. _, _, err := candle.Next()
  105. if err != nil {
  106. b.Error(err)
  107. }
  108. }
  109. }
  110. }