Skip to content

Instantly share code, notes, and snippets.

@stanroze
Created June 17, 2016 01:57
Show Gist options
  • Save stanroze/38adf4d8c6fd0a8f7ada60dc6e72f616 to your computer and use it in GitHub Desktop.
Save stanroze/38adf4d8c6fd0a8f7ada60dc6e72f616 to your computer and use it in GitHub Desktop.
Benchmarking etree
package stanza
import (
"encoding/xml"
"testing"
"github.com/ThomsonReutersEikon/etree"
)
type Email struct {
Where string `xml:"where,attr"`
Addr string
}
type Address struct {
City, State string
}
type Result struct {
XMLName xml.Name `xml:"Person"`
Name string `xml:"FullName"`
Phone string
Email []Email
Groups []string `xml:"Group>Value"`
Address
}
var data = `
<Person>
<FullName>Grace R. Emlin</FullName>
<Company>Example Inc.</Company>
<Email where="home">
<Addr>gre@example.com</Addr>
</Email>
<Email where='work'>
<Addr>gre@work.com</Addr>
</Email>
<Email where='work'>
<Addr>gre@work.com</Addr>
</Email>
<Email where='work'>
<Addr>gre@work.com</Addr>
</Email>
<Email where='work'>
<Addr>gre@work.com</Addr>
</Email>
<Group>
<Value>Friends</Value>
<Value>Squash</Value>
<Value>Friends</Value>
<Value>Squash</Value>
<Value>Friends</Value>
<Value>Squash</Value>
<Value>Friends</Value>
<Value>Squash</Value>
<Value>Friends</Value>
<Value>Squash</Value>
</Group>
<City>Hanga Roa</City>
<State>Easter Island</State>
</Person>
`
func etreeParse() {
doc := etree.NewDocument()
doc.ReadFromString(data)
doc.FindElement("//Email/Addr")
}
func xmlMarshal() {
v := Result{Name: "none", Phone: "none"}
xml.Unmarshal([]byte(data), &v)
}
func BenchmarkEtree(b *testing.B) {
// run the Fib function b.N times
for n := 0; n < b.N; n++ {
etreeParse()
}
}
func BenchmarkMarshal(b *testing.B) {
for n := 0; n < b.N; n++ {
xmlMarshal()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment