【问题标题】:An uncertain issue about CPU cache or GC or any else关于 CPU 缓存或 GC 或任何其他问题的不确定问题
【发布时间】:2017-12-28 21:35:10
【问题描述】:

我正在为 golang 编写一个任意 JSON 解析器,该项目即将完成。但是我发现了一个关于性能的令人困惑的问题:

我想测试解析大 (100Mb) JSON 字符串的性能,我使用测试文件本身在内存中初始化 JSON 结构并将封送的 JSON 字符串写入文件,然后从中读取,如果文件已经存在,不会在内存中初始化,直接从文件中读取。性能完全不同:直接从文件读取时,解析时间大约是两倍。同时,我正在测试解析 normal(1Kb) JSON string 和 deep(2Mb) JSON string 的性能,这两个几乎不受影响。

为什么?是CPU缓存吗?还是GC?还是别的?

https://github.com/acrazing/cheapjson 的代码,我在https://github.com/acrazing/cheapjson/issues/1 开了一个关于这个问题的问题。您可以从此处获取有关基准测试的更多详细信息。

【问题讨论】:

  • 我无法重现您的问题。
  • @peterSO 你能告诉我你是怎么做测试的吗?

标签: json memory go garbage-collection cpu


【解决方案1】:

我无法重现您的问题:https://github.com/acrazing/cheapjson/issues/1。我的基准测试结果:

$ go version
go version devel +b817359 Sat Jul 22 01:29:58 2017 +0000 linux/amd64
$ rm -rf data
$ ls -la data
ls: cannot access 'data': No such file or directory
$ go test -v -run=! -bench=. -benchmem parser_test.go
2017/07/23 02:42:09 big input size: 117265882, normal input size: 763, deep input size: 2134832
goos: linux
goarch: amd64
BenchmarkUnmarshalBigInput-4               1    1107362634 ns/op    278192256 B/op   8280859 allocs/op
BenchmarkSimpleJsonBigInput-4              1    2680878194 ns/op    569595680 B/op   7575252 allocs/op
BenchmarkUnmarshalNormalInput-4       300000          5685 ns/op        4622 B/op        129 allocs/op
BenchmarkSimpleJsonNormalInput-4      200000          9565 ns/op        5602 B/op         87 allocs/op
BenchmarkUnmarshalDeepInput-4           1000       2085186 ns/op      372922 B/op       5134 allocs/op
BenchmarkSimpleJsonDeepInput-4           100      12311435 ns/op     8911102 B/op       6117 allocs/op
PASS
ok      command-line-arguments  15.067s
$ ls -la datatotal 116620
drwxr-xr-x 2 peter peter      4096 Jul 23 02:42 .
drwxr-xr-x 5 peter peter      4096 Jul 23 02:42 ..
-rwxr-xr-x 1 peter peter 117265882 Jul 23 02:42 big.json
-rwxr-xr-x 1 peter peter   2134832 Jul 23 02:42 deep.json
-rwxr-xr-x 1 peter peter       763 Jul 23 02:42 normal.json
$ go test -v -run=! -bench=. -benchmem parser_test.go
2017/07/23 02:42:31 big input size: 117265882, normal input size: 763, deep input size: 2134832
goos: linux
goarch: amd64
BenchmarkUnmarshalBigInput-4               1    1140498937 ns/op    278220704 B/op   8280995 allocs/op
BenchmarkSimpleJsonBigInput-4              1    2685285322 ns/op    569592608 B/op   7575242 allocs/op
BenchmarkUnmarshalNormalInput-4       300000          5685 ns/op        4622 B/op        129 allocs/op
BenchmarkSimpleJsonNormalInput-4      200000          9633 ns/op        5601 B/op         87 allocs/op
BenchmarkUnmarshalDeepInput-4           1000       2086891 ns/op      372927 B/op       5134 allocs/op
BenchmarkSimpleJsonDeepInput-4           100      12387413 ns/op     8911084 B/op       6117 allocs/op
PASS
ok      command-line-arguments  11.903s
$ ls -la data
total 116624
drwxr-xr-x 2 peter peter      4096 Jul 23 02:42 .
drwxr-xr-x 5 peter peter      4096 Jul 23 02:42 ..
-rwxr-xr-x 1 peter peter 117265882 Jul 23 02:42 big.json
-rwxr-xr-x 1 peter peter   2134832 Jul 23 02:42 deep.json
-rwxr-xr-x 1 peter peter       763 Jul 23 02:42 normal.json
$ 

我在四台不同的机器和两个操作系统上得到了类似的结果:三个 Linux 和一个 Windows。

【讨论】:

  • 太棒了,我也无法在我的 MacOS 上再次重现此问题。但我确信我之前测试过很多次,结果与问题相同。我将在后续答案中发布屏幕截图。
【解决方案2】:

这是旧的基准测试结果,时间是 11:01

这是目前的最终测试结果,结果无法重现,太棒了。

【讨论】:

    猜你喜欢
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 2020-06-02
    • 2012-06-12
    • 2011-05-23
    • 2013-05-28
    相关资源
    最近更新 更多