File tree 2 files changed +80
-1
lines changed
2 files changed +80
-1
lines changed Original file line number Diff line number Diff line change
1
+ package json
2
+
3
+ import (
4
+ "encoding/json"
5
+ "math"
6
+ "math/big"
7
+ "testing"
8
+ "time"
9
+ )
10
+
11
+ type Data struct {
12
+ String string
13
+ Time time.Time
14
+ Int int
15
+ Int8 int8
16
+ Int16 int16
17
+ Int32 int32
18
+ Int64 int64
19
+ Boolean bool
20
+ Float32 float32
21
+ Float64 float64
22
+ BigInt big.Int
23
+ BigFloat big.Float
24
+ }
25
+
26
+ func BenchmarkJsonMarshal (b * testing.B ) {
27
+ for n := 0 ; n < b .N ; n ++ {
28
+ var d = Data {
29
+ String : "" ,
30
+ Time : time .Now (),
31
+ Int : math .MaxInt32 ,
32
+ Int8 : math .MaxInt8 ,
33
+ Int16 : math .MaxInt16 ,
34
+ Int32 : math .MaxInt32 ,
35
+ Int64 : math .MaxInt64 ,
36
+ Boolean : false ,
37
+ Float32 : math .MaxFloat32 ,
38
+ Float64 : math .MaxFloat64 ,
39
+ BigInt : * big .NewInt (math .MaxInt64 ),
40
+ BigFloat : * big .NewFloat (math .MaxFloat64 ),
41
+ }
42
+
43
+ _ , err := json .Marshal (d )
44
+ if err != nil {
45
+ b .Error (err )
46
+ b .Fail ()
47
+ return
48
+ }
49
+ }
50
+ }
51
+
52
+ func BenchmarkJsonUnmarshal (b * testing.B ) {
53
+ str := `
54
+ {
55
+ "String": "",
56
+ "Time": "2019-10-30T16:41:29.853426+07:00",
57
+ "Int": 2147483647,
58
+ "Int8": 127,
59
+ "Int16": 32767,
60
+ "Int32": 2147483647,
61
+ "Int64": 9223372036854775807,
62
+ "Boolean": false,
63
+ "Float32": 3.4028235e+38,
64
+ "Float64": 1.7976931348623157e+308,
65
+ "BigInt": 9999999999999999999,
66
+ "BigFloat": "2.7976931348623157e+308"
67
+ }
68
+ `
69
+
70
+ for n := 0 ; n < b .N ; n ++ {
71
+ var d Data
72
+ err := json .Unmarshal ([]byte (str ), & d )
73
+ if err != nil {
74
+ b .Error (err )
75
+ b .Fail ()
76
+ return
77
+ }
78
+ }
79
+ }
Original file line number Diff line number Diff line change 1
1
#! /bin/bash
2
2
3
- declare -a benchs=(base64 between concat contains foreach hash index math parse random regexp)
3
+ declare -a benchs=(base64 between concat contains foreach hash index json math parse random regexp)
4
4
5
5
cat > README.md << - EOM
6
6
# Go Benchmarks
You can’t perform that action at this time.
0 commit comments