Skip to content

Instantly share code, notes, and snippets.

@DrGo
Forked from hutch/bmark_test.go
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrGo/7f6a4c334a424278a901 to your computer and use it in GitHub Desktop.
Save DrGo/7f6a4c334a424278a901 to your computer and use it in GitHub Desktop.
accompanies the blog post: Comparing JSON and XML as DataFormats… Again http://xampl.com/so/2012/06/22/1164/
package main
// run from the command line as:
// go test -bench=".*
import (
"testing"
)
func BenchmarkJSON(b *testing.B) {
json_bytes := []byte(`{ "book":{"type":"textbook","pages":"256","title":"Programming Pearls 2nd Edition","description":"The first edition of Programming Pearls was one of the most influential books I read early in my career...","rating":"4.5","coverType":"paperback","genre":"Computer Science","author":"Jon Bentley","publisher":"Addison-Wesley Professional","copyright":"1999"}}`)
for i := 0; i < b.N; i++ {
//book := load_json_book(json_bytes)
_, err := load_json_book(json_bytes)
if nil != err {
b.Fatalf("JSON parsing failed: %q", err)
}
}
}
func BenchmarkXML(b *testing.B) {
xml_bytes := []byte(`<book type='textbook' pages='256' title='Programming Pearls 2nd Edition' rating='4.5' coverType='paperback' genre='Computer Science' author='Jon Bentley' publisher='Addison-Wesley Professional' copyright='1999'><description>The first edition of Programming Pearls was one of the most influential books I read early in my career...</description></book>`)
for i := 0; i < b.N; i++ {
//book := load_xml_book(xml_bytes)
_, err := load_xml_book(xml_bytes)
if nil != err {
b.Fatalf("XML parsing failed: %q", err)
}
}
}
func BenchmarkPP(b *testing.B) {
xml_bytes := []byte(`<book type='textbook' pages='256' title='Programming Pearls 2nd Edition' rating='4.5' coverType='paperback' genre='Computer Science' author='Jon Bentley' publisher='Addison-Wesley Professional' copyright='1999'><description>The first edition of Programming Pearls was one of the most influential books I read early in my career...</description></book>`)
for i := 0; i < b.N; i++ {
_, err := load_xml_by_pull_parser(xml_bytes)
if nil != err {
b.Fatalf("XML PP parsing failed: %q", err)
}
}
}
{
"book": {
"type": "textbook",
"pages": "256",
"title": "Programming Pearls 2nd Edition",
"description": "The first edition of Programming Pearls was one of the most influential books I read early in my career...",
"rating": "4.5",
"coverType": "paperback",
"genre": "Computer Science",
"author": "Jon Bentley",
"publisher": "Addison-Wesley Professional",
"copyright": "1999"
}
}
<book>
<type>textbook</type>
<pages>256</pages>
<title>Programming Pearls 2nd Edition</title>
<description>The first edition of Programming Pearls was one of the most influential books I read early in my career...</description>
<rating>4.5</rating>
<coverType>paperback</coverType>
<genre>Computer Science</genre>
<author>Jon Bentley</author>
<publisher>Addison-Wesley Professional</publisher>
<copyright>1999</copyright>
</book>
package main
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"io"
)
type BookRef struct {
Book BookInfo
}
type BookInfo struct {
Type string `xml:"type,attr"`
Pages string `xml:"pages,attr"`
Title string `xml:"title,attr"`
Description string `xml:"description"`
Rating string `xml:"rating,attr"`
CoverType string `xml:"coverType,attr"`
Genre string `xml:"genre,attr"`
Author string `xml:"author,attr"`
Publisher string `xml:"publisher,attr"`
Copyright string `xml:"copyright,attr"`
}
func load_json_book(bytes []byte) (book *BookRef, err error) {
book = new(BookRef)
err = json.Unmarshal(bytes, book)
return
}
func load_xml_book(bytes []byte) (book *BookInfo, err error) {
book = new(BookInfo)
err = xml.Unmarshal(bytes, book)
return
}
func load_xml_by_pull_parser(xml_bytes []byte) (book *BookInfo, err error) {
book = new(BookInfo)
decoder := xml.NewDecoder(bytes.NewReader(xml_bytes))
collectingDescription := false
for {
in_token, err := decoder.Token()
if io.EOF == err {
break
}
if (nil == in_token) && (nil == err) {
break
}
switch token := in_token.(type) {
case xml.StartElement:
collectingDescription = false
switch token.Name.Local {
case "book":
for i := range token.Attr {
switch token.Attr[i].Name.Local {
case "type":
book.Type = token.Attr[i].Value
case "pages":
book.Pages = token.Attr[i].Value
case "title":
book.Title = token.Attr[i].Value
case "rating":
book.Rating = token.Attr[i].Value
case "coverType":
book.CoverType = token.Attr[i].Value
case "genre":
book.Genre = token.Attr[i].Value
case "author":
book.Author = token.Attr[i].Value
case "publisher":
book.Publisher = token.Attr[i].Value
case "copyright":
book.Copyright = token.Attr[i].Value
}
}
case "description":
book.Description = ""
collectingDescription = true
}
case xml.EndElement:
collectingDescription = false
case xml.CharData:
if collectingDescription {
//book.Description += string(token.Copy())
book.Description += string(token)
}
}
if nil != err {
break
}
}
return
}
func main() {
json_bytes := []byte(`{"book":{"type":"textbook","pages":"256","title":"Programming Pearls 2nd Edition","description":"The first edition of Programming Pearls was one of the most influential books I read early in my career...","rating":"4.5","coverType":"paperback","genre":"Computer Science","author":"Jon Bentley","publisher":"Addison-Wesley Professional","copyright":"1999"}}`)
xml_bytes := []byte(`<book type='textbook' pages='256' title='Programming Pearls 2nd Edition' rating='4.5' coverType='paperback' genre='Computer Science' author='Jon Bentley' publisher='Addison-Wesley Professional' copyright='1999'><description>The first edition of Programming Pearls was one of the most influential books I read early in my career...</description></book>`)
fmt.Printf("JSON `%v`\n", string(json_bytes))
fmt.Printf("the size of the JSON string in bytes: %v\n", len(json_bytes))
json_book, err := load_json_book(json_bytes)
if nil != err {
fmt.Printf("load_json_book:: that didn't work: %v\n", err)
} else {
fmt.Printf("the JSON book is \n%#v\n", json_book)
}
fmt.Println()
fmt.Printf("XML `%v`\n", string(xml_bytes))
fmt.Printf("the size of the XML string in bytes: %v\n", len(xml_bytes))
xml_book, err := load_xml_book(xml_bytes)
if nil != err {
fmt.Printf("load_xml_book:: that didn't work: %v\n", err)
} else {
fmt.Printf("the XML book is \n%#v\n", xml_book)
}
fmt.Println()
pp_xml_book, err := load_xml_by_pull_parser(xml_bytes)
if nil != err {
fmt.Printf("pp:: that didn't work: %v\n", err)
} else {
fmt.Printf("the pulled XML book is \n%#v\n", pp_xml_book)
}
}
<book type='textbook'
pages='256'
title='Programming Pearls 2nd Edition'
rating='4.5'
coverType='paperback'
genre='Computer Science'
author='Jon Bentley'
publisher='Addison-Wesley Professional'
copyright='1999'>
<description>The first edition of Programming Pearls was one of the most influential books I read early in my career...</description>
</book>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment