123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- 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
- }
- }
- }
|