Created
June 17, 2016 01:57
-
-
Save stanroze/38adf4d8c6fd0a8f7ada60dc6e72f616 to your computer and use it in GitHub Desktop.
Benchmarking etree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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