咖啡伴侶

          呆在上海
          posts - 163, comments - 156, trackbacks - 0, articles - 2

          bson和json

          Posted on 2013-09-23 14:08 oathleo 閱讀(3298) 評論(0)  編輯  收藏 所屬分類: Golang
          測試1000個數據 每個數據10個字節,分別使用字節、json、bson方式 存儲,并用gzip壓縮

          結果bson比json還大一點,確實出乎意料

          個人結論是BSON對比json更加適合存儲,在傳輸上沒有太大優勢

            BSON相對JSon
          1.更快的遍歷速度
          2.操作更簡易
          3.增加了額外的數據類型

          raw data: 10000
          raw data gzip compress: 6553

          json string: 44524
          json byte: 44524
          json gzip compress: 8125

          bson byte: 46910
          bson gzip compress: 9721


          package main

          import (
              "bytes"
              "compress/gzip"
              "fmt"
              "labix.org/v2/mgo/bson"
              "math/rand"
          )

          type HisCollection struct {
              RTValues []RTValue
          }

          type RTValue struct {
              Time   int32
              Status int16
              Value  float32
          }

          func main() {
              fmt.Println("start")

              size := 1000
              col := make([]RTValue, size)

              for i := 0; i < size; i++ {
                  col[i] = RTValue{Time: int32(i), Status: 100, Value: rand.Float32()}
              }

              his := HisCollection{RTValues: col}
              data, err := bson.Marshal(&his)
              if err != nil {
                  panic(err)
              }
              //    fmt.Println(data)
              fmt.Println("bson byte:", len(data))

              var compress_data_buf bytes.Buffer
              writer := gzip.NewWriter(&compress_data_buf)
              defer writer.Close()

              writer.Write(data)
              writer.Flush()

              fmt.Println("bson gzip compress:",len(compress_data_buf.Bytes()))

          }

          package main

          import (
              "bytes"
              "compress/gzip"
              "fmt"
              "math/rand"
              "openplant/opnet"
          )

          func main() {
              var compress_data_buf bytes.Buffer
              writer := gzip.NewWriter(&compress_data_buf)
              defer writer.Close()

              size := 1000
              for i := 0; i < size; i++ {
                  writer.Write(opnet.WarpInt32ToByte(int32(i)))
                  writer.Write(opnet.WarpInt16ToByte(int16(100)))
                  writer.Write(opnet.WarpFloat32ToByte(rand.Float32()))
              }

              writer.Flush()

              fmt.Println("raw data:", 10000)

              fmt.Println("raw data gzip compress:", len(compress_data_buf.Bytes()))

          }
          111

          package main

          import (
              "bytes"
              "compress/gzip"
              "encoding/json"
              "fmt"
              "math/rand"
          )

          type HisCollection struct {
              RTValues []RTValue
          }

          type RTValue struct {
              Time   int32
              Status int16
              Value  float32
          }

          func main() {
              fmt.Println("start")

              size := 1000
              col := make([]RTValue, size)

              for i := 0; i < size; i++ {
                  col[i] = RTValue{Time: int32(i), Status: 100, Value: rand.Float32()}
              }

              his := HisCollection{RTValues: col}

              data, err := json.Marshal(&his)

              fmt.Println("json string:", string(data))
              fmt.Println("json string:", len(string(data)))

              if err != nil {
                  panic(err)
              }
              //    fmt.Println(data)
              fmt.Println("json byte:", len(data))

              var compress_data_buf bytes.Buffer
              writer := gzip.NewWriter(&compress_data_buf)
              defer writer.Close()

              writer.Write(data)
              writer.Flush()

              fmt.Println("json gzip compress:", len(compress_data_buf.Bytes()))

          }
          主站蜘蛛池模板: 拉萨市| 宁河县| 余姚市| 长岛县| 育儿| 高唐县| 奉节县| 鄂尔多斯市| 易门县| 东阳市| 青阳县| 宜都市| 涟源市| 道孚县| 崇州市| 郁南县| 进贤县| 昌乐县| 岢岚县| 洛扎县| 抚州市| 林甸县| 定结县| 蓬莱市| 巴楚县| 日土县| 阿坝县| 新巴尔虎右旗| 体育| 璧山县| 辛集市| 灵山县| 天祝| 肇州县| 鹤峰县| 四川省| 贵港市| 边坝县| 新津县| 常宁市| 天等县|