package store import "testing" import "unsafe" import "bytes" import "encoding/binary" import "sync" import "time" import "os" import "io" import "log" import "strings" import "compress/gzip" import "encoding/json" type ohlc struct { Open float64 High float64 Low float64 Close float64 Time int32 Ms int32 } type ohlcsave struct { ohlc } //日志时间精确到秒 func (save *ohlcsave) GetTime() int32 { return save.Time } func (save *ohlcsave) GetId() int64 { return 1 } //全局变量,可以提高性能 var sizeofohlcsave = int(unsafe.Sizeof(ohlcsave{})) //减少垃圾回收器的使用次数 var fhBytes = sync.Pool{ New: func() interface{} { buf := make([]byte, sizeofohlcsave) return &buf }, } func (save *ohlcsave) GetData() []byte { bufp := fhBytes.Get().(*[]byte) defer fhBytes.Put(bufp) buf := bytes.NewBuffer(*bufp) buf.Reset() err := binary.Write(buf, binary.LittleEndian, save) if err != nil { return nil } return buf.Bytes() } func (save *ohlcsave) Size() int { return sizeofohlcsave } func (save *ohlcsave) LoadData(data []byte) LoggingSave { reader := bytes.NewBuffer(data) var ret ohlcsave err := binary.Read(reader, binary.LittleEndian, &ret) if err != nil { return nil } return &ret } func printDebug(fname string, empty LoggingSave) error { log.Println(fname) fp, err := os.Open(fname) if err != nil { return err } defer fp.Close() var lasttime, readonly, lastcount int32 err = binary.Read(fp, binary.LittleEndian, &lasttime) if err != nil { return err } log.Println("lasttime", lasttime) err = binary.Read(fp, binary.LittleEndian, &readonly) if err != nil { return err } log.Println("readonly", readonly) err = binary.Read(fp, binary.LittleEndian, &lastcount) if err != nil { return err } log.Println("lastcount", lastcount) buf := make([]byte, empty.Size()) for err == nil { _, err = io.ReadFull(fp, buf) if err != nil { if err != io.EOF { return err } else { return nil } } data := empty.LoadData(buf).(*ohlcsave) log.Println(data) } return nil } func printZip(fname string, empty LoggingSave) error { fnameZip := strings.Replace(fname, ".bin", ".gz", 1) log.Println(fnameZip) fpz, err := os.Open(fnameZip) if err != nil { return err } defer fpz.Close() gr, err := gzip.NewReader(fpz) if err != nil { return err } var lasttime, readonly, lastcount int32 err = binary.Read(gr, binary.LittleEndian, &lasttime) if err != nil { return err } log.Println("lasttime", lasttime) err = binary.Read(gr, binary.LittleEndian, &readonly) if err != nil { return err } log.Println("readonly", readonly) err = binary.Read(gr, binary.LittleEndian, &lastcount) if err != nil { return err } log.Println("lastcount", lastcount) var data ohlcsave for err == nil { err = binary.Read(gr, binary.LittleEndian, &data) if err != nil { if err != io.EOF { return err } else { return nil } } log.Println(data) } return nil } func printIdx(fname string) ([fileDuration]int64, error) { var datas [fileDuration]int64 fnameIdx := strings.Replace(fname, ".bin", ".idx", 1) log.Println(fnameIdx) f, err := os.Open(fnameIdx) if err != nil { return datas, err } defer f.Close() dec := json.NewDecoder(f) err = dec.Decode(&datas) if err != nil { return datas, err } log.Println(datas) return datas, nil } func TestWrite(t *testing.T) { ohlcs := &ohlcsave{} save, err := NewSaveWriter("path", "test", false, ohlcs, nil) if err != nil { t.Error(err) return } defer save.Close() err = printDebug(save.cur.Name(), ohlcs) if err != nil { log.Println(err) } save.PrintDataIndex() datas := save.GetData(0, 0, 2, 1000, ohlcs) for _, v := range datas { log.Println("GetData", v) } cur := time.Now() now := cur.Unix() - 0 log.Println("cur", now, cur) data := ohlc{1, 1, 1, 1, int32(now), 0} s := &ohlcsave{data} err = save.Save(s) if err != nil { t.Error(err) return } save.Flush() } func BenchmarkWrite(b *testing.B) { ohlcs := &ohlcsave{} save, err := NewSaveWriter("path", "test", false, ohlcs, nil) if err != nil { b.Error(err) return } defer save.Close() for i := 0; i < b.N; i++ { now := time.Now().Unix() data := ohlc{1, 1, 1, 1, int32(now), int32(i)} s := &ohlcsave{data} err := save.Save(s) if err != nil { b.Error(err) return } } }