Go JSON组件性能比较

2022-09-16 10:35:29 浏览数 (1)

Go语言中JSON相关的组件非常多,使用于不同的场景,但是各组件的性能如何,可以测试一探究竟。

测试数据

实际测试过程中users数组是大小为101的一个数据。

代码语言:txt复制
{
  "users": [
    {
      "name": "Elliot",
      "type": "Reader",
      "age": 23,
      "social": {
        "facebook": "https://facebook.com",
        "twitter": "https://twitter.com"
      }
    },
    {
      "name": "Fraser",
      "type": "Author",
      "age": 17,
      "social": {
        "facebook": "https://facebook.com",
        "twitter": "https://twitter.com"
      }
    }
  ]
}

测试结果

通过测试JSON数据的marshal和unmarshal操作,总体上的耗时比较如下:

  • marshal:Go官方EncodingJson(1) > Jsoniter(1.08) > GoJson(1.65) > 字节跳动Sonic(2.92)
  • unmarshal:Go官方EncodingJson(1) > Jsoniter(3.32) > GoJson(4.18) > 字节跳动Sonic(5.43)

字节跳动Sonic的总体性能较优,是Go官方性能的2~4倍,但是marshal时内存会多消耗15%

代码语言:shell复制
goos: linux
goarch: amd64
cpu: AMD EPYC 7K62 48-Core Processor
BenchmarkUnmarshalByEncodingJson
BenchmarkUnmarshalByEncodingJson-8                  4558            251439 ns/op           50320 B/op        540 allocs/op
BenchmarkUnmarshalByJsoniter
BenchmarkUnmarshalByJsoniter-8                     23265             51442 ns/op           37732 B/op        418 allocs/op
BenchmarkUnmarshalBySonic
BenchmarkUnmarshalBySonic-8                        25605             46509 ns/op           54089 B/op          7 allocs/op
BenchmarkUnmarshalByGojson
BenchmarkUnmarshalByGojson-8                       32930             36068 ns/op           41130 B/op        106 allocs/op
BenchmarkParallelUnmarshalByEncodingJson
BenchmarkParallelUnmarshalByEncodingJson-8         20946             57688 ns/op           50321 B/op        540 allocs/op
BenchmarkParallelUnmarshalByJsoniter
BenchmarkParallelUnmarshalByJsoniter-8             69572             17365 ns/op           37741 B/op        418 allocs/op
BenchmarkParallelUnmarshalBySonic
BenchmarkParallelUnmarshalBySonic-8               103156             10615 ns/op           47770 B/op          7 allocs/op
BenchmarkParallelUnmarshalByGojson
BenchmarkParallelUnmarshalByGojson-8               85515             13812 ns/op           41177 B/op        106 allocs/op
BenchmarkMarshalByEncodingJson
BenchmarkMarshalByEncodingJson-8                   32042             39473 ns/op           12365 B/op          2 allocs/op
BenchmarkMarshalByJsoniter
BenchmarkMarshalByJsoniter-8                       35865             33548 ns/op           12371 B/op          2 allocs/op
BenchmarkMarshalBySonic
BenchmarkMarshalBySonic-8                          82150             14503 ns/op           17289 B/op          5 allocs/op
BenchmarkMarshalByGojson
BenchmarkMarshalByGojson-8                         58357             20252 ns/op           12360 B/op          2 allocs/op
BenchmarkParallelMarshalByEncodingJson
BenchmarkParallelMarshalByEncodingJson-8          137030              8697 ns/op           12371 B/op          2 allocs/op
BenchmarkParallelMrshalByJsoniter
BenchmarkParallelMrshalByJsoniter-8               141614              7990 ns/op           12381 B/op          2 allocs/op
BenchmarkParallelMrshalBySonic
BenchmarkParallelMrshalBySonic-8                  408537              2978 ns/op           14314 B/op          5 allocs/op
BenchmarkParallelMrshalByGojson
BenchmarkParallelMrshalByGojson-8                 248948              5259 ns/op           12393 B/op          2 allocs/op

测试代码

代码语言:go复制
func UnmarshalByEncodingJson(str string) Users {
    var users Users
    json.Unmarshal([]byte(str), &users)
    //fmt.Println(users.Users[0].Name)
    return users
}

func UnmarshalByJsoniter(str string) Users {
    var users Users
    var json = jsoniter.ConfigCompatibleWithStandardLibrary
    json.Unmarshal([]byte(str), &users)
    //jsoniter.Unmarshal([]byte(strJson), &users)
    //fmt.Println(users.Users[0].Age)
    return users
}

func UnmarshalBySonic(str string) Users {
    var users Users
    sonic.Unmarshal([]byte(str), &users)
    //fmt.Println(users.Users[0].Age)
    return users
}

func UnmarshalByGojson(str string) Users {
    var users Users
    gojson.Unmarshal([]byte(str), &users)
    //fmt.Println(users.Users[0].Age)
    return users
}

func marshalByEncodingJson(users Users) []byte {
    val, err := json.Marshal(users)
    if err != nil {
        fmt.Println(err.Error())
    }
    //fmt.Println(string(val))
    return val
}

func marshalByJsoniter(users Users) []byte {
    val, err := jsoniter.Marshal(users)
    if err != nil {
        fmt.Println(err.Error())
    }
    //fmt.Println(string(val))
    return val
}

func marshalBySonic(users Users) []byte {
    val, err := sonic.Marshal(users)
    if err != nil {
        fmt.Println(err.Error())
    }
    //fmt.Println(string(val))
    return val
}

func marshalByGojson(users Users) []byte {
    val, err := gojson.Marshal(users)
    if err != nil {
        fmt.Println(err.Error())
    }
    //fmt.Println(string(val))
    return val
}

0 人点赞