1
0
Fork 0

Lexer benchmarks

This commit is contained in:
Gregory Eremin 2015-02-23 20:51:56 +07:00
parent a8137d5b87
commit 93023774d2
2 changed files with 47 additions and 24 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
main
big*.json

View File

@ -1,62 +1,84 @@
package lexer package lexer
import ( import (
"encoding/json"
"io/ioutil"
"os"
"runtime" "runtime"
"testing" "testing"
"github.com/localhots/punk/buffer" "github.com/localhots/punk/buffer"
) )
func BenchmarkRun(t *testing.B) {
f, _ := os.Open("big1.json")
b, _ := ioutil.ReadAll(f)
t.ReportAllocs()
for i := 0; i < t.N; i++ {
lex(b)
}
}
func BenchmarkStandardJSON(t *testing.B) {
f, _ := os.Open("big1.json")
b, _ := ioutil.ReadAll(f)
t.ReportAllocs()
for i := 0; i < t.N; i++ {
var res interface{}
json.Unmarshal(b, &res)
}
}
func TestEmpty(t *testing.T) { func TestEmpty(t *testing.T) {
compare(t, lex(""), []Item{ compare(t, lex([]byte("")), []Item{
{EOF, "\x00", 0, 0}, {EOF, "\x00", 0, 0},
}) })
} }
func TestNull(t *testing.T) { func TestNull(t *testing.T) {
compare(t, lex("null"), []Item{ compare(t, lex([]byte("null")), []Item{
{Null, "null", 0, 0}, {Null, "null", 0, 0},
{EOF, "\x00", 0, 0}, {EOF, "\x00", 0, 0},
}) })
} }
func TesBool(t *testing.T) { func TesBool(t *testing.T) {
compare(t, lex("true"), []Item{ compare(t, lex([]byte("true")), []Item{
{Bool, "true", 0, 0}, {Bool, "true", 0, 0},
{EOF, "\x00", 0, 0}, {EOF, "\x00", 0, 0},
}) })
compare(t, lex("false"), []Item{ compare(t, lex([]byte("false")), []Item{
{Bool, "false", 0, 0}, {Bool, "false", 0, 0},
{EOF, "\x00", 0, 0}, {EOF, "\x00", 0, 0},
}) })
} }
func TestString(t *testing.T) { func TestString(t *testing.T) {
compare(t, lex(`"foo"`), []Item{ compare(t, lex([]byte(`"foo"`)), []Item{
{String, "foo", 0, 0}, {String, "foo", 0, 0},
{EOF, "\x00", 0, 0}, {EOF, "\x00", 0, 0},
}) })
} }
func TestNumber(t *testing.T) { func TestNumber(t *testing.T) {
compare(t, lex("123"), []Item{ compare(t, lex([]byte("123")), []Item{
{Number, "123", 0, 0}, {Number, "123", 0, 0},
{EOF, "\x00", 0, 0}, {EOF, "\x00", 0, 0},
}) })
compare(t, lex("123.456"), []Item{ compare(t, lex([]byte("123.456")), []Item{
{Number, "123.456", 0, 0}, {Number, "123.456", 0, 0},
{EOF, "\x00", 0, 0}, {EOF, "\x00", 0, 0},
}) })
compare(t, lex("123.456.789"), []Item{ compare(t, lex([]byte("123.456.789")), []Item{
{Error, `Invalid number: "123.456.789"`, 0, 0}, {Error, `Invalid number: "123.456.789"`, 0, 0},
}) })
compare(t, lex("123."), []Item{ compare(t, lex([]byte("123.")), []Item{
{Error, `Invalid number: "123."`, 0, 0}, {Error, `Invalid number: "123."`, 0, 0},
}) })
} }
func TestArray(t *testing.T) { func TestArray(t *testing.T) {
compare(t, lex(`[1, "2", 3]`), []Item{ compare(t, lex([]byte(`[1, "2", 3]`)), []Item{
{BracketOpen, "[", 0, 0}, {BracketOpen, "[", 0, 0},
{Number, "1", 0, 0}, {Number, "1", 0, 0},
{Comma, ",", 0, 0}, {Comma, ",", 0, 0},
@ -69,7 +91,7 @@ func TestArray(t *testing.T) {
} }
func TestObject(t *testing.T) { func TestObject(t *testing.T) {
compare(t, lex(`{"a": 1, "b": 2}`), []Item{ compare(t, lex([]byte(`{"a": 1, "b": 2}`)), []Item{
{BraceOpen, "{", 0, 0}, {BraceOpen, "{", 0, 0},
{String, "a", 0, 0}, {String, "a", 0, 0},
{Colon, ":", 0, 0}, {Colon, ":", 0, 0},
@ -85,17 +107,16 @@ func TestObject(t *testing.T) {
// Yay! // Yay!
func TestEverything(t *testing.T) { func TestEverything(t *testing.T) {
input := ` input := []byte(`{
{ "foo": true,
"foo": true, "bar": false,
"bar": false, "zilch": null,
"zilch": null, "numbers": [1, 23, 4.56, 7.89],
"numbers": [1, 23, 4.56, 7.89], "bullshit": {
"bullshit": { "nothing": "anything"
"nothing": "anything" }!
}! }`)
}
`
compare(t, lex(input), []Item{ compare(t, lex(input), []Item{
{BraceOpen, "{", 0, 0}, {BraceOpen, "{", 0, 0},
{String, "foo", 0, 0}, {String, "foo", 0, 0},
@ -152,8 +173,8 @@ func compare(t *testing.T, reality, expectations []Item) {
} }
} }
func lex(jstr string) []Item { func lex(b []byte) []Item {
buf := buffer.NewBytesBuffer([]byte(jstr)) buf := buffer.NewBytesBuffer(b)
lex := New(buf) lex := New(buf)
go lex.Run() go lex.Run()