1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-09-16 09:06:36 +02:00

Added some benchmarks (#139)

This commit is contained in:
Tim Voronov
2018-10-24 19:40:57 -04:00
committed by GitHub
parent ebea64da37
commit 19dbf12296
18 changed files with 932 additions and 230 deletions

View File

@@ -35,6 +35,7 @@ stages:
- compile
- test
- e2e
- bench
jobs:
include:
@@ -57,4 +58,7 @@ jobs:
script:
- make e2e
after_script:
- killall google-chrome-stable
- killall google-chrome-stable
- stage: bench
script:
- make bench

View File

@@ -21,7 +21,7 @@ install:
dep ensure
test:
go test -race ${DIR_PKG}/...
go test -v -race ${DIR_PKG}/...
cover:
go test -race -coverprofile=coverage.txt -covermode=atomic ${DIR_PKG}/...
@@ -29,6 +29,9 @@ cover:
e2e:
go run ${DIR_E2E}/main.go --tests ${DIR_E2E}/tests --pages ${DIR_E2E}/pages
bench:
go test -run=XXX -bench=. ${DIR_PKG}/...
generate:
go generate ${DIR_PKG}/...

View File

@@ -329,3 +329,103 @@ func TestArrayOperator(t *testing.T) {
})
})
}
func BenchmarkArrayOperatorALL(b *testing.B) {
p := compiler.New().MustCompile(`
RETURN [1,2,3] ALL IN [1,2,3]
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkArrayOperatorALL2(b *testing.B) {
p := compiler.New().MustCompile(`
RETURN [1,2,4] ALL IN [1,2,3]
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkArrayOperatorANY(b *testing.B) {
p := compiler.New().MustCompile(`
RETURN [1,2,3] ANY IN [1,2,3]
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkArrayOperatorANY2(b *testing.B) {
p := compiler.New().MustCompile(`
RETURN [4,5,6] ANY IN [1,2,3]
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkArrayOperatorANY3(b *testing.B) {
p := compiler.New().MustCompile(`
RETURN [4,5,6] ANY NOT IN [1,2,3]
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkArrayOperatorANY4(b *testing.B) {
p := compiler.New().MustCompile(`
RETURN [1,2,3 ] ANY == 2
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkArrayOperatorNONE(b *testing.B) {
p := compiler.New().MustCompile(`
RETURN [1,2,3] NONE IN [1,2,3]
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkArrayOperatorNONE2(b *testing.B) {
p := compiler.New().MustCompile(`
RETURN [4,5,6] NONE IN [1,2,3]
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkArrayOperatorNONE3(b *testing.B) {
p := compiler.New().MustCompile(`
RETURN [4,5,6] NONE NOT IN [1,2,3]
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkArrayOperatorNONE4(b *testing.B) {
p := compiler.New().MustCompile(`
RETURN [1,2,3] NONE < 99
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}

View File

@@ -12,14 +12,14 @@ func TestEqualityOperators(t *testing.T) {
Convey("Should compile RETURN 2 > 1", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 2 > 1
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "true")

View File

@@ -11,7 +11,7 @@ func TestForFilter(t *testing.T) {
Convey("Should compile query with FILTER i > 2", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
FOR i IN [ 1, 2, 3, 4, 1, 3 ]
FILTER i > 2
RETURN i
@@ -19,7 +19,7 @@ func TestForFilter(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -29,7 +29,7 @@ func TestForFilter(t *testing.T) {
Convey("Should compile query with FILTER i > 1 AND i < 3", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
FOR i IN [ 1, 2, 3, 4, 1, 3 ]
FILTER i > 1 AND i < 4
RETURN i
@@ -37,7 +37,7 @@ func TestForFilter(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -47,7 +47,7 @@ func TestForFilter(t *testing.T) {
Convey("Should compile query with multiple FILTER statements", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET users = [
{
active: true,
@@ -73,7 +73,7 @@ func TestForFilter(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -83,7 +83,7 @@ func TestForFilter(t *testing.T) {
Convey("Should compile query with multiple FILTER statements", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET users = [
{
active: true,
@@ -115,7 +115,7 @@ func TestForFilter(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -125,7 +125,7 @@ func TestForFilter(t *testing.T) {
Convey("Should compile query with left side expression", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET users = [
{
active: true,
@@ -155,7 +155,7 @@ func TestForFilter(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -165,7 +165,7 @@ func TestForFilter(t *testing.T) {
Convey("Should compile query with multiple left side expression", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET users = [
{
active: true,
@@ -205,7 +205,7 @@ func TestForFilter(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -215,7 +215,7 @@ func TestForFilter(t *testing.T) {
Convey("Should compile query with multiple left side expression and with binary operator", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET users = [
{
active: true,
@@ -255,7 +255,7 @@ func TestForFilter(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -265,7 +265,7 @@ func TestForFilter(t *testing.T) {
Convey("Should compile query with multiple left side expression and with binary operator 2", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET users = [
{
active: true,
@@ -305,10 +305,160 @@ func TestForFilter(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `[]`)
})
}
func BenchmarkFilter(b *testing.B) {
p := compiler.New().MustCompile(`
LET users = [
{
active: true,
age: 31,
gender: "m"
},
{
active: true,
age: 29,
gender: "f"
},
{
active: true,
age: 36,
gender: "m"
},
{
active: false,
age: 69,
gender: "m"
}
]
FOR u IN users
FILTER u.age < 35
RETURN u
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkFilter2(b *testing.B) {
p := compiler.New().MustCompile(`
LET users = [
{
active: true,
age: 31,
gender: "m"
},
{
active: true,
age: 29,
gender: "f"
},
{
active: true,
age: 36,
gender: "m"
},
{
active: false,
age: 69,
gender: "m"
}
]
FOR u IN users
FILTER u.active == true
FILTER u.age < 35
RETURN u
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkFilter3(b *testing.B) {
p := compiler.New().MustCompile(`
LET users = [
{
active: true,
age: 31,
gender: "m"
},
{
active: true,
age: 29,
gender: "f"
},
{
active: true,
age: 36,
gender: "m"
},
{
active: false,
age: 69,
gender: "m"
}
]
FOR u IN users
FILTER u.active == true
LIMIT 2
FILTER u.gender == "m"
RETURN u
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkFilter4(b *testing.B) {
p := compiler.New().MustCompile(`
LET users = [
{
active: true,
married: true,
age: 31,
gender: "m"
},
{
active: true,
married: false,
age: 25,
gender: "f"
},
{
active: true,
married: false,
age: 36,
gender: "m"
},
{
active: false,
married: true,
age: 69,
gender: "m"
},
{
active: true,
married: true,
age: 45,
gender: "f"
}
]
FOR u IN users
FILTER !u.active AND u.married
LIMIT 2
RETURN u
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}

View File

@@ -92,3 +92,14 @@ func TestForTernaryExpression(t *testing.T) {
So(string(out2), ShouldEqual, `[1,2,3,4,5]`)
})
}
func BenchmarkForTernary(b *testing.B) {
p := compiler.New().MustCompile(`
LET foo = FALSE
RETURN foo ? TRUE : (FOR i IN 1..5 RETURN i*2)
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}

View File

@@ -14,15 +14,15 @@ func TestFor(t *testing.T) {
Convey("Should compile FOR i IN [] RETURN i", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
FOR i IN []
RETURN i
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "[]")
@@ -31,15 +31,15 @@ func TestFor(t *testing.T) {
Convey("Should compile FOR i IN [1, 2, 3] RETURN i", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
FOR i IN [1, 2, 3]
RETURN i
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "[1,2,3]")
@@ -48,15 +48,15 @@ func TestFor(t *testing.T) {
Convey("Should compile FOR i, k IN [1, 2, 3] RETURN k", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
FOR i, k IN [1, 2, 3]
RETURN k
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "[0,1,2]")
@@ -65,15 +65,15 @@ func TestFor(t *testing.T) {
Convey("Should compile FOR i IN ['foo', 'bar', 'qaz'] RETURN i", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
FOR i IN ['foo', 'bar', 'qaz']
RETURN i
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "[\"foo\",\"bar\",\"qaz\"]")
@@ -82,15 +82,15 @@ func TestFor(t *testing.T) {
Convey("Should compile FOR i IN {a: 'bar', b: 'foo', c: 'qaz'} RETURN i.name", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
FOR i IN {a: 'bar', b: 'foo', c: 'qaz'}
RETURN i
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -111,15 +111,15 @@ func TestFor(t *testing.T) {
Convey("Should compile FOR i, k IN {a: 'foo', b: 'bar', c: 'qaz'} RETURN k", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
FOR i, k IN {a: 'foo', b: 'bar', c: 'qaz'}
RETURN k
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -139,15 +139,15 @@ func TestFor(t *testing.T) {
Convey("Should compile FOR i IN [{name: 'foo'}, {name: 'bar'}, {name: 'qaz'}] RETURN i.name", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
FOR i IN [{name: 'foo'}, {name: 'bar'}, {name: 'qaz'}]
RETURN i.name
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "[\"foo\",\"bar\",\"qaz\"]")
@@ -156,7 +156,7 @@ func TestFor(t *testing.T) {
Convey("Should compile nested FOR operators", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
FOR prop IN ["a"]
FOR val IN [1, 2, 3]
RETURN {[prop]: val}
@@ -164,7 +164,7 @@ func TestFor(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -174,7 +174,7 @@ func TestFor(t *testing.T) {
Convey("Should compile deeply nested FOR operators", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
FOR prop IN ["a"]
FOR val IN [1, 2, 3]
FOR val2 IN [1, 2, 3]
@@ -183,7 +183,7 @@ func TestFor(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -193,7 +193,7 @@ func TestFor(t *testing.T) {
Convey("Should compile query with a sub query", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
FOR val IN [1, 2, 3]
RETURN (
FOR prop IN ["a", "b", "c"]
@@ -203,7 +203,7 @@ func TestFor(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -213,7 +213,7 @@ func TestFor(t *testing.T) {
Convey("Should compile query with variable in a body", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
FOR val IN [1, 2, 3]
LET sub = (
FOR prop IN ["a", "b", "c"]
@@ -225,7 +225,7 @@ func TestFor(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -235,14 +235,14 @@ func TestFor(t *testing.T) {
Convey("Should compile query with RETURN DISTINCT", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
FOR i IN [ 1, 2, 3, 4, 1, 3 ]
RETURN DISTINCT i
`)
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -252,7 +252,7 @@ func TestFor(t *testing.T) {
Convey("Should compile query with LIMIT 2", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
FOR i IN [ 1, 2, 3, 4, 1, 3 ]
LIMIT 2
RETURN i
@@ -260,7 +260,7 @@ func TestFor(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -272,7 +272,7 @@ func TestFor(t *testing.T) {
// 4 is offset
// 2 is count
prog, err := c.Compile(`
p, err := c.Compile(`
FOR i IN [ 1,2,3,4,5,6,7,8 ]
LIMIT 4, 2
RETURN i
@@ -280,7 +280,7 @@ func TestFor(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -290,7 +290,7 @@ func TestFor(t *testing.T) {
Convey("Should compile query with SORT statement", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET users = [
{
active: true,
@@ -315,7 +315,7 @@ func TestFor(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -325,7 +325,7 @@ func TestFor(t *testing.T) {
Convey("Should compile query with SORT DESC statement", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET users = [
{
active: true,
@@ -350,7 +350,7 @@ func TestFor(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -360,7 +360,7 @@ func TestFor(t *testing.T) {
Convey("Should compile query with SORT statement with multiple expressions", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET users = [
{
active: true,
@@ -390,10 +390,220 @@ func TestFor(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `[{"active":true,"age":29,"gender":"f"},{"active":true,"age":31,"gender":"f"},{"active":true,"age":31,"gender":"m"},{"active":true,"age":36,"gender":"m"}]`)
})
}
func BenchmarkForEmpty(b *testing.B) {
p := compiler.New().MustCompile(`
FOR i IN []
RETURN i
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkForArray(b *testing.B) {
p := compiler.New().MustCompile(`
FOR i IN [1,2,3]
RETURN i
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkForObject(b *testing.B) {
p := compiler.New().MustCompile(`
FOR i IN {a: 'bar', b: 'foo', c: 'qaz'}
RETURN i
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkForNested(b *testing.B) {
p := compiler.New().MustCompile(`
FOR prop IN ["a"]
FOR val IN [1, 2, 3]
RETURN {[prop]: val}
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkForNested2(b *testing.B) {
p := compiler.New().MustCompile(`
FOR prop IN ["a"]
FOR val IN [1, 2, 3]
FOR val2 IN ["b"]
RETURN { [prop]: [val, val2] }
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkForSub(b *testing.B) {
p := compiler.New().MustCompile(`
FOR val IN [1, 2, 3]
RETURN (
FOR prop IN ["a", "b", "c"]
RETURN { [prop]: val }
)
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkForSub2(b *testing.B) {
p := compiler.New().MustCompile(`
FOR val IN [1, 2, 3]
LET sub = (
FOR prop IN ["a", "b", "c"]
RETURN { [prop]: val }
)
RETURN sub
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkForDistinct(b *testing.B) {
p := compiler.New().MustCompile(`
FOR i IN [ 1, 2, 3, 4, 1, 3 ]
RETURN DISTINCT i
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkForLimit(b *testing.B) {
p := compiler.New().MustCompile(`
FOR i IN [ 1,2,3,4,5,6,7,8 ]
LIMIT 2
RETURN i
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkForLimitOffset(b *testing.B) {
p := compiler.New().MustCompile(`
FOR i IN [ 1,2,3,4,5,6,7,8 ]
LIMIT 4, 2
RETURN i
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkForSort(b *testing.B) {
p := compiler.New().MustCompile(`
LET users = [
{
active: true,
age: 31,
gender: "m"
},
{
active: true,
age: 29,
gender: "f"
},
{
active: true,
age: 36,
gender: "m"
}
]
FOR u IN users
SORT u.age
RETURN u
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkForSort2(b *testing.B) {
p := compiler.New().MustCompile(`
LET users = [
{
active: true,
age: 31,
gender: "m"
},
{
active: true,
age: 29,
gender: "f"
},
{
active: true,
age: 36,
gender: "m"
}
]
FOR u IN users
SORT u.age, u.gender
RETURN u
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkForSortDesc(b *testing.B) {
p := compiler.New().MustCompile(`
LET users = [
{
active: true,
age: 31,
gender: "m"
},
{
active: true,
age: 29,
gender: "f"
},
{
active: true,
age: 36,
gender: "m"
}
]
FOR u IN users
SORT u.age DESC
RETURN u
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}

View File

@@ -3,6 +3,8 @@ package compiler_test
import (
"context"
"github.com/MontFerret/ferret/pkg/compiler"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
@@ -11,13 +13,13 @@ func TestFunctionCall(t *testing.T) {
Convey("Should compile RETURN TYPENAME(1)", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN TYPENAME(1)
`)
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -27,14 +29,14 @@ func TestFunctionCall(t *testing.T) {
Convey("Should compile WAIT(10) RETURN 1", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
WAIT(10)
RETURN 1
`)
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -44,7 +46,7 @@ func TestFunctionCall(t *testing.T) {
Convey("Should compile LET duration = 10 WAIT(duration) RETURN 1", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET duration = 10
WAIT(duration)
@@ -54,7 +56,7 @@ func TestFunctionCall(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -64,7 +66,7 @@ func TestFunctionCall(t *testing.T) {
Convey("Should compile function call inside FOR IN statement", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
FOR i IN [1, 2, 3, 4]
LET duration = 10
@@ -75,10 +77,73 @@ func TestFunctionCall(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `[2,4,6,8]`)
})
}
func BenchmarkFunctionCallArg1(b *testing.B) {
c := compiler.New()
c.RegisterFunction("TEST", func(ctx context.Context, args ...core.Value) (core.Value, error) {
return values.None, nil
})
p := c.MustCompile(`
RETURN TYPENAME(1)
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkFunctionCallArg2(b *testing.B) {
c := compiler.New()
c.RegisterFunction("TEST", func(ctx context.Context, args ...core.Value) (core.Value, error) {
return values.None, nil
})
p := c.MustCompile(`
RETURN TYPENAME(1, 2)
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkFunctionCallArg3(b *testing.B) {
c := compiler.New()
c.RegisterFunction("TEST", func(ctx context.Context, args ...core.Value) (core.Value, error) {
return values.None, nil
})
p := c.MustCompile(`
RETURN TYPENAME(1, 2, 3)
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkFunctionEmpty(b *testing.B) {
c := compiler.New()
c.RegisterFunction("TEST", func(ctx context.Context, args ...core.Value) (core.Value, error) {
return values.None, nil
})
p := c.MustCompile(`
RETURN TEST()
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}

View File

@@ -11,13 +11,13 @@ func TestInOperator(t *testing.T) {
Convey("1 IN [1,2,3] should return true", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 1 IN [1,2,3]
`)
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `true`)
@@ -26,13 +26,13 @@ func TestInOperator(t *testing.T) {
Convey("4 IN [1,2,3] should return false", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 4 IN [1,2,3]
`)
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `false`)
@@ -41,15 +41,35 @@ func TestInOperator(t *testing.T) {
Convey("4 NOT IN [1,2,3] should return true", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 4 NOT IN [1,2,3]
`)
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `true`)
})
}
func BenchmarkInOperator(b *testing.B) {
p := compiler.New().MustCompile(`
RETURN 1 IN [1,2,3]
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkInOperatorNot(b *testing.B) {
p := compiler.New().MustCompile(`
RETURN 4 NOT IN [1,2,3]
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}

View File

@@ -12,15 +12,15 @@ func TestLet(t *testing.T) {
Convey("Should compile LET i = NONE RETURN i", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET i = NONE
RETURN i
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "null")
@@ -29,15 +29,15 @@ func TestLet(t *testing.T) {
Convey("Should compile LET i = TRUE RETURN i", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET i = TRUE
RETURN i
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "true")
@@ -46,15 +46,15 @@ func TestLet(t *testing.T) {
Convey("Should compile LET i = 1 RETURN i", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET i = 1
RETURN i
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "1")
@@ -63,15 +63,15 @@ func TestLet(t *testing.T) {
Convey("Should compile LET i = 1.1 RETURN i", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET i = 1.1
RETURN i
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "1.1")
@@ -80,15 +80,15 @@ func TestLet(t *testing.T) {
Convey("Should compile LET i = 'foo' RETURN i", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET i = "foo"
RETURN i
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "\"foo\"")
@@ -97,15 +97,15 @@ func TestLet(t *testing.T) {
Convey("Should compile LET i = [] RETURN i", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET i = []
RETURN i
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "[]")
@@ -114,15 +114,15 @@ func TestLet(t *testing.T) {
Convey("Should compile LET i = [1, 2, 3] RETURN i", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET i = [1, 2, 3]
RETURN i
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "[1,2,3]")
@@ -131,15 +131,15 @@ func TestLet(t *testing.T) {
Convey("Should compile LET i = {} RETURN i", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET i = {}
RETURN i
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "{}")
@@ -148,15 +148,15 @@ func TestLet(t *testing.T) {
Convey("Should compile LET i = {a: 'foo', b: 1, c: TRUE, d: [], e: {}} RETURN i", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET i = {a: 'foo', b: 1, c: TRUE, d: [], e: {}}
RETURN i
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "{\"a\":\"foo\",\"b\":1,\"c\":true,\"d\":[],\"e\":{}}")
@@ -165,15 +165,15 @@ func TestLet(t *testing.T) {
Convey("Should compile LET i = (FOR i IN [1,2,3] RETURN i) RETURN i", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET i = (FOR i IN [1,2,3] RETURN i)
RETURN i
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "[1,2,3]")
@@ -182,7 +182,7 @@ func TestLet(t *testing.T) {
Convey("Should compile LET i = { items: [1,2,3]} FOR el IN i.items RETURN i", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET obj = { items: [1,2,3] }
FOR i IN obj.items
@@ -190,9 +190,9 @@ func TestLet(t *testing.T) {
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "[1,2,3]")

View File

@@ -12,14 +12,14 @@ func TestLogicalOperators(t *testing.T) {
Convey("Should compile RETURN 2 > 1 AND 1 > 0", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 2 > 1 AND 1 > 0
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "true")
@@ -28,14 +28,14 @@ func TestLogicalOperators(t *testing.T) {
Convey("Should compile RETURN 2 > 1 OR 1 < 0", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 2 > 1 OR 1 < 0
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "true")
@@ -44,13 +44,13 @@ func TestLogicalOperators(t *testing.T) {
Convey("1 || 7 should return 1", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 1 || 7
`)
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "1")
@@ -59,13 +59,13 @@ func TestLogicalOperators(t *testing.T) {
Convey("NONE || 'foo' should return 'foo'", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN NONE || 'foo'
`)
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `"foo"`)
@@ -74,13 +74,13 @@ func TestLogicalOperators(t *testing.T) {
Convey("NONE && true should return null", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN NONE && true
`)
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `null`)
@@ -89,13 +89,13 @@ func TestLogicalOperators(t *testing.T) {
Convey("'' && true should return ''", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN '' && true
`)
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `""`)
@@ -104,13 +104,13 @@ func TestLogicalOperators(t *testing.T) {
Convey("true && 23 should return '23", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN true && 23
`)
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `23`)
@@ -119,13 +119,13 @@ func TestLogicalOperators(t *testing.T) {
Convey("NOT TRUE should return false", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN NOT TRUE
`)
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `false`)
@@ -134,7 +134,7 @@ func TestLogicalOperators(t *testing.T) {
Convey("NOT u.valid should return true", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET u = { valid: false }
RETURN NOT u.valid
@@ -142,9 +142,29 @@ func TestLogicalOperators(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `true`)
})
}
func BenchmarkLogicalOperatorsAnd(b *testing.B) {
p := compiler.New().MustCompile(`
RETURN 2 > 1 AND 1 > 0
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkLogicalOperatorsOr(b *testing.B) {
p := compiler.New().MustCompile(`
RETURN 2 > 1 OR 1 < 0
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}

View File

@@ -13,14 +13,14 @@ func TestMathOperators(t *testing.T) {
Convey("Should compile RETURN 1 + 1", func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 1 + 1
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "2")
@@ -29,14 +29,14 @@ func TestMathOperators(t *testing.T) {
Convey("Should compile RETURN 1 - 1", func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 1 - 1
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "0")
@@ -45,14 +45,14 @@ func TestMathOperators(t *testing.T) {
Convey("Should compile RETURN 2*2", func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 2*2
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "4")
@@ -61,14 +61,14 @@ func TestMathOperators(t *testing.T) {
Convey("Should compile RETURN 4/2", func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 4/2
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "2")
@@ -77,14 +77,14 @@ func TestMathOperators(t *testing.T) {
Convey("Should compile RETURN 5 % 2", func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 5 % 2
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "1")
@@ -95,14 +95,14 @@ func TestMathOperators(t *testing.T) {
Convey("Should compile RETURN 1.2 + 1", func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 1.2 + 1
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "2.2")
@@ -111,14 +111,14 @@ func TestMathOperators(t *testing.T) {
Convey("Should compile RETURN 1.1 - 1", func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 1.1 - 1
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "0.10000000000000009")
@@ -127,14 +127,14 @@ func TestMathOperators(t *testing.T) {
Convey("Should compile RETURN 2.1*2", func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 2.1*2
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "4.2")
@@ -143,14 +143,14 @@ func TestMathOperators(t *testing.T) {
Convey("Should compile RETURN 4.4/2", func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 4.4/2
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "2.2")
@@ -159,14 +159,14 @@ func TestMathOperators(t *testing.T) {
Convey("Should compile RETURN 5.5 % 2", func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 5.5 % 2
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "1")
@@ -177,14 +177,14 @@ func TestMathOperators(t *testing.T) {
Convey("Should concat two strings RETURN 'Foo' + 'Bar'", func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 'Foo' + 'Bar'
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, `"FooBar"`)

View File

@@ -13,7 +13,7 @@ func TestMember(t *testing.T) {
Convey("Array by literal", func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET arr = [1,2,3,4]
RETURN arr[1]
@@ -21,7 +21,7 @@ func TestMember(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -31,7 +31,7 @@ func TestMember(t *testing.T) {
Convey("Array by variable", func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET arr = [1,2,3,4]
LET idx = 1
@@ -40,7 +40,7 @@ func TestMember(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -50,7 +50,7 @@ func TestMember(t *testing.T) {
Convey("Object by literal", func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET obj = { foo: "bar", qaz: "wsx"}
RETURN obj["qaz"]
@@ -58,7 +58,7 @@ func TestMember(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -68,7 +68,7 @@ func TestMember(t *testing.T) {
Convey("Object by literal with property defined as a string", func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET obj = { "foo": "bar", "qaz": "wsx"}
RETURN obj["qaz"]
@@ -76,7 +76,7 @@ func TestMember(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -86,7 +86,7 @@ func TestMember(t *testing.T) {
Convey("Object by literal with property defined as a multi line string", func() {
c := compiler.New()
prog, err := c.Compile(fmt.Sprintf(`
p, err := c.Compile(fmt.Sprintf(`
LET obj = { "foo": "bar", %s: "wsx"}
RETURN obj["qaz"]
@@ -94,7 +94,7 @@ func TestMember(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -104,7 +104,7 @@ func TestMember(t *testing.T) {
Convey("Object by variable", func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET obj = { foo: "bar", qaz: "wsx"}
LET key = "qaz"
@@ -113,7 +113,7 @@ func TestMember(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -121,3 +121,39 @@ func TestMember(t *testing.T) {
})
})
}
func BenchmarkMemberArray(b *testing.B) {
p := compiler.New().MustCompile(`
LET arr = [1]
RETURN arr[0]
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkMemberObject(b *testing.B) {
p := compiler.New().MustCompile(`
LET obj = { "foo": "bar"}
RETURN obj.foo
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkMemberObjectComputed(b *testing.B) {
p := compiler.New().MustCompile(`
LET obj = { "foo": "bar"}
RETURN obj["foo"]
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}

View File

@@ -11,13 +11,13 @@ func TestRangeOperator(t *testing.T) {
Convey("Should compile RETURN 1..10", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 1..10
`)
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -27,14 +27,14 @@ func TestRangeOperator(t *testing.T) {
Convey("Should compile FOR i IN 1..10 RETURN i * 2", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
FOR i IN 1..10
RETURN i * 2
`)
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -44,7 +44,7 @@ func TestRangeOperator(t *testing.T) {
Convey("Should compile LET arr = 1..10 FOR i IN arr RETURN i * 2", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
LET arr = 1..10
FOR i IN arr
RETURN i * 2
@@ -52,7 +52,7 @@ func TestRangeOperator(t *testing.T) {
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -89,3 +89,13 @@ func TestRangeOperator(t *testing.T) {
So(string(out3), ShouldEqual, `[2,4,6,8,10,12,14,16,18,20]`)
})
}
func BenchmarkRangeOperator(b *testing.B) {
p := compiler.New().MustCompile(`
RETURN 1..10
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}

View File

@@ -12,14 +12,14 @@ func TestReturn(t *testing.T) {
Convey("Should compile RETURN NONE", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN NONE
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "null")
@@ -28,14 +28,14 @@ func TestReturn(t *testing.T) {
Convey("Should compile RETURN TRUE", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN TRUE
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "true")
@@ -44,14 +44,14 @@ func TestReturn(t *testing.T) {
Convey("Should compile RETURN 1", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 1
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "1")
@@ -60,14 +60,14 @@ func TestReturn(t *testing.T) {
Convey("Should compile RETURN 1.1", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 1.1
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "1.1")
@@ -76,14 +76,14 @@ func TestReturn(t *testing.T) {
Convey("Should compile RETURN 'foo'", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN 'foo'
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "\"foo\"")
@@ -92,14 +92,14 @@ func TestReturn(t *testing.T) {
Convey("Should compile RETURN \"foo\"", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN "foo"
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "\"foo\"")
@@ -108,14 +108,14 @@ func TestReturn(t *testing.T) {
Convey("Should compile RETURN \"\"", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN ""
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "\"\"")
@@ -124,14 +124,14 @@ func TestReturn(t *testing.T) {
Convey("Should compile RETURN []", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN []
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "[]")
@@ -140,14 +140,14 @@ func TestReturn(t *testing.T) {
Convey("Should compile RETURN [1, 2, 3, 4]", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN [1, 2, 3, 4]
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "[1,2,3,4]")
@@ -156,14 +156,14 @@ func TestReturn(t *testing.T) {
Convey("Should compile RETURN ['foo', 'bar', 'qaz']", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN ['foo', 'bar', 'qaz']
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "[\"foo\",\"bar\",\"qaz\"]")
@@ -172,14 +172,14 @@ func TestReturn(t *testing.T) {
Convey("Should compile RETURN ['foo', 'bar', 1, 2]", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN ['foo', 'bar', 1, 2]
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "[\"foo\",\"bar\",1,2]")
@@ -188,14 +188,14 @@ func TestReturn(t *testing.T) {
Convey("Should compile RETURN {}", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN {}
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "{}")
@@ -204,14 +204,14 @@ func TestReturn(t *testing.T) {
Convey("Should compile RETURN {a: 'foo', b: 'bar'}", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN { a: "foo", b: "bar" }
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "{\"a\":\"foo\",\"b\":\"bar\"}")
@@ -220,14 +220,14 @@ func TestReturn(t *testing.T) {
Convey("Should compile RETURN {['a']: 'foo'}", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
RETURN { ["a"]: "foo" }
`)
So(err, ShouldBeNil)
So(prog, ShouldHaveSameTypeAs, &runtime.Program{})
So(p, ShouldHaveSameTypeAs, &runtime.Program{})
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
So(string(out), ShouldEqual, "{\"a\":\"foo\"}")

View File

@@ -21,3 +21,16 @@ BAR
So(string(out), ShouldEqual, `"\nFOO\nBAR\n"`)
})
}
func BenchmarkStringLiteral(b *testing.B) {
p := compiler.New().MustCompile(`
RETURN "
FOO
BAR
"
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}

View File

@@ -11,14 +11,14 @@ import (
func TestTernaryOperator(t *testing.T) {
Convey("Should compile ternary operator", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
FOR i IN [1, 2, 3, 4, 5, 6]
RETURN i < 3 ? i * 3 : i * 2;
`)
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -27,14 +27,14 @@ func TestTernaryOperator(t *testing.T) {
Convey("Should compile ternary operator with shortcut", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
FOR i IN [1, 2, 3, 4, 5, 6]
RETURN i < 3 ? : i * 2
`)
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -43,14 +43,14 @@ func TestTernaryOperator(t *testing.T) {
Convey("Should compile ternary operator with shortcut with nones", t, func() {
c := compiler.New()
prog, err := c.Compile(`
p, err := c.Compile(`
FOR i IN [NONE, 2, 3, 4, 5, 6]
RETURN i ? : i
`)
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -69,14 +69,14 @@ func TestTernaryOperator(t *testing.T) {
c := compiler.New()
for _, val := range vals {
prog, err := c.Compile(fmt.Sprintf(`
p, err := c.Compile(fmt.Sprintf(`
FOR i IN [%s, 1, 2, 3]
RETURN i ? i * 2 : 'no value'
`, val))
So(err, ShouldBeNil)
out, err := prog.Run(context.Background())
out, err := p.Run(context.Background())
So(err, ShouldBeNil)
@@ -84,3 +84,31 @@ func TestTernaryOperator(t *testing.T) {
}
})
}
func BenchmarkTernaryOperator(b *testing.B) {
p := compiler.New().MustCompile(`
LET a = "a"
LET b = "b"
LET c = FALSE
RETURN c ? a : b;
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkTernaryOperatorDef(b *testing.B) {
p := compiler.New().MustCompile(`
LET a = "a"
LET b = "b"
LET c = FALSE
RETURN c ? : a;
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}

View File

@@ -90,3 +90,35 @@ func TestUnaryOperator(t *testing.T) {
So(string(out1), ShouldEqual, `-1`)
})
}
func BenchmarkUnaryOperatorExcl(b *testing.B) {
p := compiler.New().MustCompile(`
RETURN !TRUE
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkUnaryOperatorQ(b *testing.B) {
p := compiler.New().MustCompile(`
LET foo = TRUE
RETURN !foo ? TRUE : FALSE
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}
func BenchmarkUnaryOperatorN(b *testing.B) {
p := compiler.New().MustCompile(`
LET v = 1
RETURN -v
`)
for n := 0; n < b.N; n++ {
p.Run(context.Background())
}
}