Created
July 24, 2015 18:27
-
-
Save mongrelion/e37eed39f9b0aa79d706 to your computer and use it in GitHub Desktop.
Dummy XML marshalling benchmark in Go
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 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", "[email protected]", 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: "[email protected]", | |
Age: 32, | |
Address: "742 Evergreen Terrace", | |
City: "Springfield", | |
Country: "USA", | |
Password: "donuts", | |
} | |
xml.Marshal(blah) | |
} |
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 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", "[email protected]", 32, "742 Evergreen Terrace", "Springfield", "USA", "donuts"} | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
foo.Marshal() | |
} | |
} |
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
λ 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