測試目的:個人開源項目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性能大幅由於直接讀寫文件