123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package base
- import "testing"
- import "time"
- import "fmt"
- import "math/rand"
- import "unsafe"
- func TestTimezoneTrans(t *testing.T) {
- trans, err := NewTimezoneTrans("America/New_York", 7 * 3600, "UTC", 0, 0)
- if err != nil {
- fmt.Println(err)
- }
- //est time 2013-08-29 15:00:00
- est := "2013.08.30 12:41:48"
- ti, err := time.Parse("2006.01.02 15:04:05", est)
- if err != nil {
- fmt.Println(err)
- }
- offset := trans.Offset(int(ti.Unix()))
- newt := time.Unix(ti.Unix() + int64(offset), 0)
- //最终转换过的时间
- fmt.Println(newt)
- }
- var beginTime int64
- // Using a fixed seed will produce the same output on every run.
- var r = rand.New(rand.NewSource(time.Now().UnixNano()))
- var lastTick TickGo
- var count int64
- func init() {
- t, _ := time.Parse("2006.01.02 15:04:05", "2012.01.01 00:00:00")
- beginTime = t.Unix()
- lastTick.Time = int32(beginTime)
- firstprice := float32(1.0)
- if lastTick.Time % 2 == 0 {
- lastTick.Ask = firstprice + r.Float32() / float32(10.0)
- lastTick.Bid = firstprice + r.Float32() / float32(10.0)
- }
- }
- func randTick() TickGo {
- count++
- if count % 10 == 0 {
- beginTime++
- }
- lastTick.Time = int32(beginTime)
- randint := r.Int63()
- if randint % 2 == 0 {
- lastTick.Ask += r.Float32() / float32(10.0)
- lastTick.Bid += r.Float32() / float32(10.0)
- } else {
- lastTick.Ask -= r.Float32() / float32(10.0)
- lastTick.Bid -= r.Float32() / float32(10.0)
- }
- return lastTick
- }
- func BenchmarkRandTick(b *testing.B) {
- for i := 0; i < b.N; i++ {
- randTick()
- }
- }
- func BenchmarkCandleCalc(b *testing.B) {
- b.StopTimer()
- candle, err := NewCandle(60, 2, nil, 0)
- if err != nil {
- b.Error(err)
- return
- }
- trans, err := NewTimezoneTrans("America/New_York", 7 * 3600, "UTC", 0, 0)
- if err != nil {
- b.Error(err)
- }
- candle.SetTimezoneTrans(trans)
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- tk := randTick()
- num := candle.UpdateTick((*Tick)(unsafe.Pointer(&tk)))
- if num == 0 {
- ohlc := Ohlc{}
- candle.Next(&ohlc)
- var _ = ohlc.ToGOStruct()
- } else {
- for n := 0; n < num; n++ {
- ohlc := Ohlc{}
- candle.Next(&ohlc)
- var _ = ohlc.ToGOStruct()
- }
- }
- }
- }
- func BenchmarkCandleSimple(b *testing.B) {
- b.StopTimer()
- trans, err := NewTimezoneTrans("America/New_York", 7 * 3600, "UTC", 0, 0)
- if err != nil {
- b.Error(err)
- }
- candle, err := NewCandleSimple(60, 2, trans)
- if err != nil {
- b.Error(err)
- return
- }
- b.StartTimer()
- for i := 0; i < b.N; i++ {
- tk := randTick()
- num := candle.Set(tk)
- for i := 0; i <= num; i++ {
- _, _, err := candle.Next()
- if err != nil {
- b.Error(err)
- }
- }
- }
- }
|