Skip to content

Commit f1dc5de

Browse files
Add: Benchmarks and update README
1 parent 0e7a20e commit f1dc5de

File tree

7 files changed

+710
-14
lines changed

7 files changed

+710
-14
lines changed

README.md

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,27 @@
22

33
- Decodes a "commented json" to "json". Provided, the input must be a valid jsonc document.
44
- Supports io.Reader
5+
- With this, we can use commented json files as configuration for go applications.
56

67
Inspired by [muhammadmuzzammil1998](https://github.com/muhammadmuzzammil1998/jsonc)
78

89
```jsonc
910
{
10-
/*
11+
/*
1112
some block comment
1213
*/
13-
"string": "foo", // a string
14-
"bool": false, // a boolean
15-
"number": 42, // a number
16-
// "object":{
17-
// "key":"val"
18-
// },
19-
"array": [
20-
// example of an array
21-
1,
22-
2,
23-
3
24-
]
14+
"string": "foo", // a string
15+
"bool": false, // a boolean
16+
"number": 42, // a number
17+
// "object":{
18+
// "key":"val"
19+
// },
20+
"array": [
21+
// example of an array
22+
1,
23+
2,
24+
3
25+
]
2526
}
2627
```
2728

@@ -33,7 +34,7 @@ Gets converted to (spaces omitted)
3334

3435
## Motivation
3536

36-
[jsonc](https://github.com/muhammadmuzzammil1998/jsonc) is great. But this package provides significant performance improvements and simple API to use it with standard library.
37+
[jsonc](https://github.com/muhammadmuzzammil1998/jsonc) is great. But this package provides significant performance improvements and simple API to use it with standard library. See [benchmarks](https://link)
3738

3839
## Usage
3940

@@ -45,6 +46,26 @@ go get github.com/akshaybharambe14/go-jsonc
4546

4647
```
4748

49+
## Benchmarks
50+
51+
Here is the performance comparison as compared to [JSONC](https://github.com/muhammadmuzzammil1998/jsonc)
52+
53+
go-jsonc avoids allocations to heap to gain the performance.
54+
55+
```text
56+
goos: windows
57+
goarch: amd64
58+
pkg: github.com/akshaybharambe14/go-jsonc/benchmarks
59+
BenchmarkOwnSmallJSONBytes-4 256599 4952 ns/op 353.00 MB/s 0 B/op 0 allocs/op
60+
BenchmarkOwnSmallJSONBytesReader-4 206823 5832 ns/op 299.70 MB/s 6224 B/op 5 allocs/op
61+
BenchmarkJSONCSmallJSONBytes-4 171474 6925 ns/op 252.41 MB/s 1792 B/op 1 allocs/op
62+
BenchmarkOwnBigJSONBytes-4 33517 35921 ns/op 462.26 MB/s 0 B/op 0 allocs/op
63+
BenchmarkOwnBigJSONBytesReader-4 105244 11292 ns/op 1470.45 MB/s 6224 B/op 5 allocs/op
64+
BenchmarkJSONCBigJSONBytes-4 19599 61422 ns/op 270.34 MB/s 18432 B/op 1 allocs/op
65+
PASS
66+
ok github.com/akshaybharambe14/go-jsonc/benchmarks 26.250s
67+
```
68+
4869
## Example
4970

5071
see [examples](https://github.com/akshaybharambe14/go-jsonc/tree/master/examples)

benchmarks/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Benchmarks
2+
3+
Here is the performance comparison as compared to [jsonc](https://github.com/muhammadmuzzammil1998/jsonc)
4+
5+
go-jsonc avoids allocations to heap to gain the performance.
6+
7+
```
8+
goos: windows
9+
goarch: amd64
10+
pkg: github.com/akshaybharambe14/go-jsonc/benchmarks
11+
BenchmarkOwnSmallJSONBytes-4 256599 4952 ns/op 353.00 MB/s 0 B/op 0 allocs/op
12+
BenchmarkOwnSmallJSONBytesReader-4 206823 5832 ns/op 299.70 MB/s 6224 B/op 5 allocs/op
13+
BenchmarkJSONCSmallJSONBytes-4 171474 6925 ns/op 252.41 MB/s 1792 B/op 1 allocs/op
14+
BenchmarkOwnBigJSONBytes-4 33517 35921 ns/op 462.26 MB/s 0 B/op 0 allocs/op
15+
BenchmarkOwnBigJSONBytesReader-4 105244 11292 ns/op 1470.45 MB/s 6224 B/op 5 allocs/op
16+
BenchmarkJSONCBigJSONBytes-4 19599 61422 ns/op 270.34 MB/s 18432 B/op 1 allocs/op
17+
PASS
18+
ok github.com/akshaybharambe14/go-jsonc/benchmarks 26.250s
19+
```

benchmarks/jsonc_bench_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package benchmarks
2+
3+
import (
4+
"bytes"
5+
"io/ioutil"
6+
"testing"
7+
8+
own "github.com/akshaybharambe14/go-jsonc"
9+
jsonc "muzzammil.xyz/jsonc"
10+
)
11+
12+
var jsonBytesSmall, _ = ioutil.ReadFile("./testdata/small.jsonc")
13+
var jsonBytesBig, _ = ioutil.ReadFile("./testdata/big.jsonc")
14+
15+
func BenchmarkOwnSmallJSONBytes(b *testing.B) {
16+
cnt := len(jsonBytesSmall)
17+
b.SetBytes(int64(cnt))
18+
19+
for i := 0; i < b.N; i++ {
20+
b.StopTimer()
21+
22+
ip := make([]byte, cnt)
23+
copy(ip, jsonBytesSmall)
24+
25+
b.StartTimer()
26+
27+
_, _ = own.DecodeBytes(ip)
28+
}
29+
}
30+
31+
func BenchmarkOwnSmallJSONBytesReader(b *testing.B) {
32+
b.SetBytes(int64(len(jsonBytesSmall)))
33+
34+
for i := 0; i < b.N; i++ {
35+
_, _ = ioutil.ReadAll(own.NewDecoder(bytes.NewBuffer(jsonBytesSmall)))
36+
}
37+
}
38+
39+
func BenchmarkJSONCSmallJSONBytes(b *testing.B) {
40+
b.SetBytes(int64(len(jsonBytesSmall)))
41+
42+
for i := 0; i < b.N; i++ {
43+
jsonc.ToJSON(jsonBytesSmall)
44+
}
45+
}
46+
47+
func BenchmarkOwnBigJSONBytes(b *testing.B) {
48+
cnt := len(jsonBytesBig)
49+
b.SetBytes(int64(cnt))
50+
51+
for i := 0; i < b.N; i++ {
52+
b.StopTimer()
53+
54+
ip := make([]byte, len(jsonBytesBig))
55+
copy(ip, jsonBytesBig)
56+
57+
b.StartTimer()
58+
59+
_, _ = own.DecodeBytes(ip)
60+
}
61+
}
62+
63+
func BenchmarkOwnBigJSONBytesReader(b *testing.B) {
64+
b.SetBytes(int64(len(jsonBytesBig)))
65+
66+
for i := 0; i < b.N; i++ {
67+
_, _ = ioutil.ReadAll(own.NewDecoder(bytes.NewBuffer(jsonBytesBig)))
68+
}
69+
}
70+
71+
func BenchmarkJSONCBigJSONBytes(b *testing.B) {
72+
b.SetBytes(int64(len(jsonBytesBig)))
73+
74+
for i := 0; i < b.N; i++ {
75+
_ = jsonc.ToJSON(jsonBytesBig)
76+
}
77+
}

0 commit comments

Comments
 (0)