log_store_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package store
  2. import "testing"
  3. import "unsafe"
  4. import "bytes"
  5. import "encoding/binary"
  6. import "sync"
  7. import "time"
  8. import "os"
  9. import "io"
  10. import "log"
  11. import "strings"
  12. import "compress/gzip"
  13. import "encoding/json"
  14. type ohlc struct {
  15. Open float64
  16. High float64
  17. Low float64
  18. Close float64
  19. Time int32
  20. Ms int32
  21. }
  22. type ohlcsave struct {
  23. ohlc
  24. }
  25. //日志时间精确到秒
  26. func (save *ohlcsave) GetTime() int32 {
  27. return save.Time
  28. }
  29. func (save *ohlcsave) GetId() int64 {
  30. return 1
  31. }
  32. //全局变量,可以提高性能
  33. var sizeofohlcsave = int(unsafe.Sizeof(ohlcsave{}))
  34. //减少垃圾回收器的使用次数
  35. var fhBytes = sync.Pool{
  36. New: func() interface{} {
  37. buf := make([]byte, sizeofohlcsave)
  38. return &buf
  39. },
  40. }
  41. func (save *ohlcsave) GetData() []byte {
  42. bufp := fhBytes.Get().(*[]byte)
  43. defer fhBytes.Put(bufp)
  44. buf := bytes.NewBuffer(*bufp)
  45. buf.Reset()
  46. err := binary.Write(buf, binary.LittleEndian, save)
  47. if err != nil {
  48. return nil
  49. }
  50. return buf.Bytes()
  51. }
  52. func (save *ohlcsave) Size() int {
  53. return sizeofohlcsave
  54. }
  55. func (save *ohlcsave) LoadData(data []byte) LoggingSave {
  56. reader := bytes.NewBuffer(data)
  57. var ret ohlcsave
  58. err := binary.Read(reader, binary.LittleEndian, &ret)
  59. if err != nil {
  60. return nil
  61. }
  62. return &ret
  63. }
  64. func printDebug(fname string, empty LoggingSave) error {
  65. log.Println(fname)
  66. fp, err := os.Open(fname)
  67. if err != nil {
  68. return err
  69. }
  70. defer fp.Close()
  71. var lasttime, readonly, lastcount int32
  72. err = binary.Read(fp, binary.LittleEndian, &lasttime)
  73. if err != nil {
  74. return err
  75. }
  76. log.Println("lasttime", lasttime)
  77. err = binary.Read(fp, binary.LittleEndian, &readonly)
  78. if err != nil {
  79. return err
  80. }
  81. log.Println("readonly", readonly)
  82. err = binary.Read(fp, binary.LittleEndian, &lastcount)
  83. if err != nil {
  84. return err
  85. }
  86. log.Println("lastcount", lastcount)
  87. buf := make([]byte, empty.Size())
  88. for err == nil {
  89. _, err = io.ReadFull(fp, buf)
  90. if err != nil {
  91. if err != io.EOF {
  92. return err
  93. } else {
  94. return nil
  95. }
  96. }
  97. data := empty.LoadData(buf).(*ohlcsave)
  98. log.Println(data)
  99. }
  100. return nil
  101. }
  102. func printZip(fname string, empty LoggingSave) error {
  103. fnameZip := strings.Replace(fname, ".bin", ".gz", 1)
  104. log.Println(fnameZip)
  105. fpz, err := os.Open(fnameZip)
  106. if err != nil {
  107. return err
  108. }
  109. defer fpz.Close()
  110. gr, err := gzip.NewReader(fpz)
  111. if err != nil {
  112. return err
  113. }
  114. var lasttime, readonly, lastcount int32
  115. err = binary.Read(gr, binary.LittleEndian, &lasttime)
  116. if err != nil {
  117. return err
  118. }
  119. log.Println("lasttime", lasttime)
  120. err = binary.Read(gr, binary.LittleEndian, &readonly)
  121. if err != nil {
  122. return err
  123. }
  124. log.Println("readonly", readonly)
  125. err = binary.Read(gr, binary.LittleEndian, &lastcount)
  126. if err != nil {
  127. return err
  128. }
  129. log.Println("lastcount", lastcount)
  130. var data ohlcsave
  131. for err == nil {
  132. err = binary.Read(gr, binary.LittleEndian, &data)
  133. if err != nil {
  134. if err != io.EOF {
  135. return err
  136. } else {
  137. return nil
  138. }
  139. }
  140. log.Println(data)
  141. }
  142. return nil
  143. }
  144. func printIdx(fname string) ([fileDuration]int64, error) {
  145. var datas [fileDuration]int64
  146. fnameIdx := strings.Replace(fname, ".bin", ".idx", 1)
  147. log.Println(fnameIdx)
  148. f, err := os.Open(fnameIdx)
  149. if err != nil {
  150. return datas, err
  151. }
  152. defer f.Close()
  153. dec := json.NewDecoder(f)
  154. err = dec.Decode(&datas)
  155. if err != nil {
  156. return datas, err
  157. }
  158. log.Println(datas)
  159. return datas, nil
  160. }
  161. func TestWrite(t *testing.T) {
  162. ohlcs := &ohlcsave{}
  163. save, err := NewSaveWriter("path", "test", false, ohlcs, nil)
  164. if err != nil {
  165. t.Error(err)
  166. return
  167. }
  168. defer save.Close()
  169. err = printDebug(save.cur.Name(), ohlcs)
  170. if err != nil {
  171. log.Println(err)
  172. }
  173. save.PrintDataIndex()
  174. datas := save.GetData(0, 0, 2, 1000, ohlcs)
  175. for _, v := range datas {
  176. log.Println("GetData", v)
  177. }
  178. cur := time.Now()
  179. now := cur.Unix() - 0
  180. log.Println("cur", now, cur)
  181. data := ohlc{1, 1, 1, 1, int32(now), 0}
  182. s := &ohlcsave{data}
  183. err = save.Save(s)
  184. if err != nil {
  185. t.Error(err)
  186. return
  187. }
  188. save.Flush()
  189. }
  190. func BenchmarkWrite(b *testing.B) {
  191. ohlcs := &ohlcsave{}
  192. save, err := NewSaveWriter("path", "test", false, ohlcs, nil)
  193. if err != nil {
  194. b.Error(err)
  195. return
  196. }
  197. defer save.Close()
  198. for i := 0; i < b.N; i++ {
  199. now := time.Now().Unix()
  200. data := ohlc{1, 1, 1, 1, int32(now), int32(i)}
  201. s := &ohlcsave{data}
  202. err := save.Save(s)
  203. if err != nil {
  204. b.Error(err)
  205. return
  206. }
  207. }
  208. }