On Wed, Dec 25, 2013 at 8:08 PM, Xun Liu <pas...@gmail.com> wrote:
> I'm in the process of converting some Python code into Go. Since Json
> encoding/decoding is one of the most common operations in the code so I did
> some simple json benchmarking for two packages.
What % of the work time of your program is spent doing json encoding /
decoding? As a percentage, as profiled, is this one of your hotspots?
> Any pointers on how to improve Go's json performance are appreciated.
Don't. Premature optimization. Are the fraction of a milisecond that
the encoding / decoding take per really what need to be optimized in
your code? Have you profiled at all?
jsonStr = '{"field_1": true, "field_2": true, "field_3": "XXXXYX", "field_4": true, "field_5": true, "field_6": true, "field_7": false, "field_8": true, "field_9": false, "field_10": false, "field_11": {"p": {}, "m": {}, "d": ["5", "3", "4", "8", "9", "7", "1", "2", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22"]}, "field_12": false, "field_13": false, "field_14": "LLLLLL", "field_15": true, "field_16": "en-US", "field_17": "2013-11-26T05:49:06.236458", "field_18": "Shengzhou", "field_19": true, "field_20": false, "field_21": false, "field_22": "xxxxx...@hotmail.com", "field_23": "http://www.xxxxxxx.com/yyyyyyyyy", "field_24": true, "field_25": true, "field_26": "0001-01-01T00:00:00", "field_27": true, "field_28": true, "field_29": false, "field_30": true, "field_31": "", "field_32": true, "field_33": true, "field_34": "wrqer@#$!WEF#@#DSDFFksdniwo3hOwR8ruCeNF.3IkeOe80Yko8n7HvpsrZm", "field_35": false, "field_36": true, "field_37": "0001-01-01T00:00:00", "field_38": "String values encode as JSON strings. InvalidUTF8Error will be returned if an invalid UTF-8 sequence is encountered", "field_39": "female", "field_40": true, "field_41": true, "field_42": "insadesmaenideni", "field_43": "xyxyxyxyx_1233305355583", "field_44": "US", "field_45": {}, "field_46": false, "field_47": 1353270713, "field_48": "2013-11-12T15:38:33.052768", "field_49": "2012-02-29T22:46:18", "field_50": true}'
NUM = 1000
def dec():
for i in xrange(NUM):
d = simplejson.loads(jsonStr)
t0 = time.time()
dec()
t1 = time.time()
taken_ms = (t1 - t0) * 1000
print "Loads Taken", taken_ms, taken_ms / NUM
o = simplejson.loads(jsonStr)
def enc():
for i in xrange(NUM):
simplejson.dumps(o)
t0 = time.time()
enc()
t1 = time.time()
taken_ms = (t1 - t0) * 1000
print "Dumps Taken", taken_ms, taken_ms / NUM
Typical Output:
Loads Taken 17.8289413452 0.0178289413452
Dumps Taken 31.9888591766 0.0319888591766
Go code:
var JsonBytes = []byte(`{"field_1": true, "field_2": true, "field_3": "XXXXYX", "field_4": true, "field_5": true, "field_6": true, "field_7": false, "field_8": true, "field_9": false, "field_10": false, "field_11": {"p": {}, "m": {}, "d": ["5", "3", "4", "8", "9", "7", "1", "2", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22"]}, "field_12": false, "field_13": false, "field_14": "LLLLLL", "field_15": true, "field_16": "en-US", "field_17": "2013-11-26T05:49:06.236458", "field_18": "Shengzhou", "field_19": true, "field_20": false, "field_21": false, "field_22": "xxxxx...@hotmail.com", "field_23": "http://www.xxxxxxx.com/yyyyyyyyy", "field_24": true, "field_25": true, "field_26": "0001-01-01T00:00:00", "field_27": true, "field_28": true, "field_29": false, "field_30": true, "field_31": "", "field_32": true, "field_33": true, "field_34": "wrqer@#$!WEF#@#DSDFFksdniwo3hOwR8ruCeNF.3IkeOe80Yko8n7HvpsrZm", "field_35": false, "field_36": true, "field_37": "0001-01-01T00:00:00", "field_38": "String values encode as JSON strings. InvalidUTF8Error will be returned if an invalid UTF-8 sequence is encountered", "field_39": "female", "field_40": true, "field_41": true, "field_42": "insadesmaenideni", "field_43": "xyxyxyxyx_1233305355583", "field_44": "US", "field_45": {}, "field_46": false, "field_47": 1353270713, "field_48": "2013-11-12T15:38:33.052768", "field_49": "2012-02-29T22:46:18", "field_50": true}`)
var JsonObj map[string]interface{}
func init() {
json.Unmarshal(JsonBytes, &JsonObj)
}
func BenchmarkJsonDecode(b *testing.B) {
for i := 0; i < b.N; i++ {
for j := 0; j < 1000; j++ {
var r map[string]interface{}
//var r struct{}
json.Unmarshal(JsonBytes, &r)
}
}
}
func BenchmarkJsonEncode(b *testing.B) {
for i := 0; i < b.N; i++ {
for j := 0; j < 1000; j++ {
json.Marshal(JsonObj)
}
}
}
Output
BenchmarkJsonDecode 20 98819415 ns/op
BenchmarkJsonEncode 20 82070594 ns/op
It only helps the decoding performance by ~5%
BenchmarkJsonDecode 20 93115410 ns/op
The similar performance differences are observed using my production data.
My go version:
go version go1.2 linux/amd64
Any pointers on how to improve Go's json performance are appreciated.
Thanks,
Xun