Decode XML performance in golang

1,504 views
Skip to first unread message

emarti...@gmail.com

unread,
Feb 8, 2017, 12:22:35 AM2/8/17
to golang-nuts
Hello,

I'm making a go code which is basically parsing a lot of small xml responses. I've noticed that right after I enable the parsing code, my R/S as per wrk, as well as response times drop to 15% for some reason. I'm testing with about 2000 concurrent clients and each one parses about 50 very small xmls (1 entry each).

Below is my code, does anyone have any experience/feedback with xml decoding performance on go?


package main


import (
    "encoding/xml"
    "bytes"
)


type DBXML struct{
    HostProperties  DBXML_HostProperties
    Listing            DBXML_Listing
}

type DBXML_Listing struct {
    sid string
    Original_title string `xml:"title"`
    Original_desc   string `xml:"desc"`
    Original_domain   string `xml:"display_url"`
    Original_bid   float64 `xml:"bid"`
    Original_URL   string `xml:"clickurl"`
}

type DBXML_HostProperties struct {
    Tags    []DBXML_Listing    `xml:"record"`
}

func (r *DBXML ) parse_response (data []byte) bool {
    reader := bytes.NewReader(data)
    decoder := xml.NewDecoder(reader)
    decoder.Decode(&r.HostProperties);

    //xml.Unmarshal(data, &r.HostProperties) // With this one i got even less performance.

}

Tong Sun

unread,
Feb 9, 2017, 8:33:13 PM2/9/17
to golang-nuts
Bump. I'm interested to know as well. 

Egon

unread,
Feb 10, 2017, 2:23:06 AM2/10/17
to golang-nuts
This gets you https://play.golang.org/p/J8hFZyyFF8 about 25% perf boost:

BenchmarkDecode-8          30000             43268 ns/op
BenchmarkUnmarshal-8       30000             43195 ns/op
BenchmarkManual-8          50000             30216 ns/op

Also, Decode/Unmarshal seem pretty equal.

If you don't need full xml spec, then you probably can implement a faster parser. (Maybe one already exists https://godoc.org/?q=xml)

And... if you can avoid XML for this, then avoid it. It's not ideal for this.

+ Egon
Reply all
Reply to author
Forward
0 new messages