Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dummy XML marshalling benchmark in Go
package marshalling
import (
"encoding/xml"
)
type Foo struct {
Id int `xml:"id,attr"`
Name string `xml:"name,attr"`
Email string `xml:"email,attr"`
Age int `xml:"age,attr"`
Address string `xml:"address,attr"`
City string `xml:"city,attr"`
Country string `xml:"country,attr"`
Password string `xml:"password,attr"`
}
func (foo *Foo) Marshal() {
xml.Marshal(foo)
}
func MarshalStatic() {
xml.Marshal(Foo{1, "John Doe", "john@email.org", 32, "742 Evergreen Terrace", "Springfield", "USA", "donuts"})
}
func MarshalLiteral() {
blah := struct {
XMLName xml.Name `xml:"blah"`
Id int `xml:"id,attr"`
Name string `xml:"name,attr"`
Email string `xml:"email,attr"`
Age int `xml:"age,attr"`
Address string `xml:"address,attr"`
City string `xml:"city,attr"`
Country string `xml:"country,attr"`
Password string `xml:"password,attr"`
}{
Id: 1,
Name: "John Doe",
Email: "john@email.org",
Age: 32,
Address: "742 Evergreen Terrace",
City: "Springfield",
Country: "USA",
Password: "donuts",
}
xml.Marshal(blah)
}
package marshalling
import (
"testing"
)
func BenchmarkMarshalLiteral(b *testing.B) {
for i := 0; i < b.N; i++ {
MarshalLiteral()
}
}
func BenchmarkMarshalStatic(b *testing.B) {
for i := 0; i < b.N; i++ {
MarshalStatic()
}
}
func BenchmarkMarshalStruct(b *testing.B) {
foo := Foo{1, "John Doe", "john@email.org", 32, "742 Evergreen Terrace", "Springfield", "USA", "donuts"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
foo.Marshal()
}
}
λ for i in {1..5}; do go test -bench .; done
testing: warning: no tests to run
PASS
BenchmarkMarshalLiteral 200000 11951 ns/op
BenchmarkMarshalStatic 100000 12452 ns/op
BenchmarkMarshalStruct 100000 12244 ns/op
ok _/Users/carlos/code/personal/labs/go/xml-marshalling 5.238s
testing: warning: no tests to run
PASS
BenchmarkMarshalLiteral 200000 11526 ns/op
BenchmarkMarshalStatic 100000 12195 ns/op
BenchmarkMarshalStruct 200000 12144 ns/op
ok _/Users/carlos/code/personal/labs/go/xml-marshalling 6.320s
testing: warning: no tests to run
PASS
BenchmarkMarshalLiteral 200000 11603 ns/op
BenchmarkMarshalStatic 100000 12422 ns/op
BenchmarkMarshalStruct 100000 12120 ns/op
ok _/Users/carlos/code/personal/labs/go/xml-marshalling 5.150s
testing: warning: no tests to run
PASS
BenchmarkMarshalLiteral 200000 11875 ns/op
BenchmarkMarshalStatic 100000 12620 ns/op
BenchmarkMarshalStruct 100000 12525 ns/op
ok _/Users/carlos/code/personal/labs/go/xml-marshalling 5.284s
testing: warning: no tests to run
PASS
BenchmarkMarshalLiteral 200000 11874 ns/op
BenchmarkMarshalStatic 100000 12317 ns/op
BenchmarkMarshalStruct 100000 12217 ns/op
ok _/Users/carlos/code/personal/labs/go/xml-marshalling 5.201s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.