-
Notifications
You must be signed in to change notification settings - Fork 0
/
performance_test.go
118 lines (104 loc) · 2.57 KB
/
performance_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"bytes"
"fmt"
"log"
"os"
"strings"
"testing"
"whitehouse.id.au/microlisp/read"
"whitehouse.id.au/microlisp/run"
"whitehouse.id.au/microlisp/scan"
"whitehouse.id.au/microlisp/value"
)
var src = readFile("testdata/eval.lisp")
func BenchmarkParse(b *testing.B) {
b.SetBytes(int64(len(src)))
for i := 0; i < b.N; i++ {
scanner := scan.New(bytes.NewReader(src))
reader := read.New(scanner)
v := reader.Read()
if err, ok := v.(value.Error); ok {
b.Fatalf("benchmark failed due to parse error: %s", err)
}
}
}
func BenchmarkFprint(b *testing.B) {
scanner := scan.New(bytes.NewReader(src))
reader := read.New(scanner)
v := reader.Read()
if err, ok := v.(value.Error); ok {
b.Fatalf("benchmark failed due to parse error: %s", err)
}
// Initial print to allocate underlying buffer.
var buf bytes.Buffer
buf.WriteString(v.String())
b.SetBytes(int64(buf.Len()))
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf.Reset()
buf.WriteString(v.String())
}
}
func BenchmarkEval(b *testing.B) {
scanner := scan.New(bytes.NewReader(src))
reader := read.New(scanner)
v := reader.Read()
if err, ok := v.(value.Error); ok {
b.Fatalf("benchmark failed due to parse error: %s", err)
}
for i := 0; i < b.N; i++ {
run.Eval(v)
}
}
func BenchmarkInvoke(b *testing.B) {
testCases := []struct {
name string
args string
}{
{"list", "1 2 3"},
{"car", "(quote (x y z))"},
{"cdr", "(quote (a b c))"},
{"caar", "(quote ((a . 1) (b . 2) (c . 3)))"},
{"cadr", "(quote (a b c))"},
{"cddr", "(quote (a b c))"},
{"caddr", "(quote (a b c))"},
{"cadar", "(quote ((a a') b c))"},
{"caddar", "(quote ((a a' a'') b c))"},
{"cons", "1 2"},
{"equal", "(quote (a (b c) d (e (f) g))) (quote (a (b c) d (e (f) g)))"},
{"apply", "list (list d e f g)"},
{"apply", "list a b c (list d e f g)"},
{"lambda", "(a) a"},
}
for _, tc := range testCases {
b.Run(tc.name, func(b *testing.B) {
src := fmt.Sprintf("(%s %s)", tc.name, tc.args)
scanner := scan.New(strings.NewReader(src))
reader := read.New(scanner)
v := reader.Read()
if err, ok := v.(value.Error); ok {
b.Fatalf("benchmark failed due to parse error: %s", err)
}
for i := 0; i < b.N; i++ {
if _, err := unwrapEval(v); err != nil {
b.Fatalf("benchmark failed due to eval error: %s", err)
}
}
})
}
}
func readFile(name string) []byte {
b, err := os.ReadFile(name)
if err != nil {
log.Fatal(err)
}
return b
}
func unwrapEval(expr value.Value) (value.Value, error) {
v := run.Eval(expr)
if err, ok := v.(value.Error); ok {
return nil, err
}
return v, nil
}