go-gob序列化/反序列化与读写文件性能对比测试

测试目的:个人开源项目ZCache需对数据进行持久化存储,在此验证两种技术方案:gob序列化/反序列化和直接读写文件的性能

待测试代码

package main

import (
    gob "encoding/gob"
    "encoding/json"
    "fmt"
    "os"
)

type PersonInfo struct {
    Name    string
    age     int32
    Sex     bool
    Hobbies []string
}

func main() {

}

func writeFileByJson() {
    personInfoUint := PersonInfo{"David", 30, true, []string{"跑步", "读书", "看电影"}}
    var personInfo []PersonInfo
    for i := 0; i < 10000; i++ {
        personInfo = append(personInfo, personInfoUint)
    }

    // 创建文件
    filePtr, err := os.Create("person_info.json")
    if err != nil {
        fmt.Println("Create file failed", err.Error())
        return
    }
    defer filePtr.Close()

    // 带JSON缩进格式写文件
    data, err := json.MarshalIndent(personInfo, "", "  ")
    if err != nil {
        //fmt.Println("Encoder failed", err.Error())

    } else {
        //fmt.Println("Encoder success")
    }

    filePtr.Write(data)
}

func readFileByJson() {
    filePtr, err := os.Open("person_info.json")
    if err != nil {
        fmt.Printf("Open file failed [Err:%s]", err.Error())
        return
    }
    defer filePtr.Close()

    var person []PersonInfo

    // 创建json解码器
    decoder := json.NewDecoder(filePtr)
    err = decoder.Decode(&person)
    if err != nil {
        //fmt.Println("Decoder failed", err.Error())

    } else {
        //fmt.Println("Decoder success")
        //fmt.Println(person)
    }
}

func writeFileByGob() {
    personInfoUint := PersonInfo{"David", 30, true, []string{"跑步", "读书", "看电影"}}
    var personInfo []PersonInfo
    for i := 0; i < 10000; i++ {
        personInfo = append(personInfo, personInfoUint)
    }

    // 创建文件
    filePtr, err := os.Create("person_info.json")
    if err != nil {
        fmt.Printf("Create file failed %s", err.Error())
        return
    }
    defer filePtr.Close()

    enc := gob.NewEncoder(filePtr)
    enc.Encode(personInfo)
}

func readFileByGob() {
    var p []PersonInfo
    file, err := os.Open("person_info.json")
    if err != nil {
        fmt.Println(err)
    }
    dec := gob.NewDecoder(file)
    err2 := dec.Decode(&p)

    if err2 != nil {
        fmt.Println(err2)
        return
    }
}

性能测试代码

package main

import "testing"

func Benchmark_writeFileByJson(b *testing.B) {
        for i := 0; i < b.N; i++ {
                writeFileByJson()
        }
}

func Benchmark_readFileByJson(b *testing.B) {
        for i := 0; i < b.N; i++ {
                readFileByJson()
        }
}
func Benchmark_writeFileByGob(b *testing.B) {
        for i := 0; i < b.N; i++ {
                writeFileByGob()
        }
}
func Benchmark_readFileByGob(b *testing.B) {
        for i := 0; i < b.N; i++ {
                readFileByGob()
        }
}

  测试结果

$ go test -test.bench=".*" -count=10
goos: windows
goarch: amd64
pkg: test
Benchmark_writeFileByJson-8          100          20335627 ns/op
Benchmark_writeFileByJson-8          100          20435503 ns/op
Benchmark_writeFileByJson-8          100          21432701 ns/op
Benchmark_writeFileByJson-8          100          21533512 ns/op
Benchmark_writeFileByJson-8          100          21105956 ns/op
Benchmark_writeFileByJson-8          100          21467205 ns/op
Benchmark_writeFileByJson-8          100          20894453 ns/op
Benchmark_writeFileByJson-8          100          21093614 ns/op
Benchmark_writeFileByJson-8          100          19986887 ns/op
Benchmark_writeFileByJson-8          100          20235982 ns/op
Benchmark_readFileByJson-8            50          25751766 ns/op
Benchmark_readFileByJson-8            50          25531738 ns/op
Benchmark_readFileByJson-8            50          25213220 ns/op
Benchmark_readFileByJson-8            50          25152746 ns/op
Benchmark_readFileByJson-8            50          25572258 ns/op
Benchmark_readFileByJson-8            50          25232652 ns/op
Benchmark_readFileByJson-8            50          25112662 ns/op
Benchmark_readFileByJson-8            50          25172672 ns/op
Benchmark_readFileByJson-8            50          25152726 ns/op
Benchmark_readFileByJson-8            50          25252494 ns/op
Benchmark_writeFileByGob-8           100          10242501 ns/op
Benchmark_writeFileByGob-8           100          10382566 ns/op
Benchmark_writeFileByGob-8           100          10442381 ns/op
Benchmark_writeFileByGob-8           100          10232940 ns/op
Benchmark_writeFileByGob-8           100          10322401 ns/op
Benchmark_writeFileByGob-8           100          10203027 ns/op
Benchmark_writeFileByGob-8           100          10581712 ns/op
Benchmark_writeFileByGob-8           100          10850553 ns/op
Benchmark_writeFileByGob-8           100          10352555 ns/op
Benchmark_writeFileByGob-8           100          10342335 ns/op
Benchmark_readFileByGob-8            300           5558471 ns/op
Benchmark_readFileByGob-8            300           5595042 ns/op
Benchmark_readFileByGob-8            300           5548498 ns/op
Benchmark_readFileByGob-8            300           5571884 ns/op
Benchmark_readFileByGob-8            300           5711354 ns/op
Benchmark_readFileByGob-8            300           5661534 ns/op
Benchmark_readFileByGob-8            300           5638137 ns/op
Benchmark_readFileByGob-8            300           5661433 ns/op
Benchmark_readFileByGob-8            300           5757835 ns/op
Benchmark_readFileByGob-8            300           5678153 ns/op
PASS

测试结论:

写性能方面:gob比写文件快一倍

读性能方面:gob比读文件快4倍

综上所述:gob性能大幅由于直接读写文件