Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 29286c7

Browse files
PedroRosalbaguiferpa
authored andcommitted
feat: add manual scanner benchmarks
1 parent b74c838 commit 29286c7

2 files changed

Lines changed: 162 additions & 0 deletions

File tree

lexer/bench_manual_scanner.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
goos: darwin
2+
goarch: arm64
3+
pkg: github.com/guiferpa/aurora/lexer
4+
cpu: Apple M3
5+
BenchmarkScanToken_Keyword-8 78417979 15.25 ns/op 0 B/op 0 allocs/op
6+
BenchmarkScanToken_Identifier-8 73019718 17.32 ns/op 0 B/op 0 allocs/op
7+
BenchmarkScanToken_Number-8 131816068 9.118 ns/op 0 B/op 0 allocs/op
8+
BenchmarkScanToken_HexNumber-8 100000000 10.64 ns/op 0 B/op 0 allocs/op
9+
BenchmarkScanToken_String-8 151201761 7.927 ns/op 0 B/op 0 allocs/op
10+
BenchmarkScanToken_SingleChar-8 192291577 7.268 ns/op 0 B/op 0 allocs/op
11+
BenchmarkScanToken_Comment-8 167061290 7.180 ns/op 0 B/op 0 allocs/op
12+
BenchmarkGetTokens_Simple-8 3057578 444.7 ns/op 1360 B/op 14 allocs/op
13+
BenchmarkGetTokens_Medium-8 1000000 1201 ns/op 3984 B/op 37 allocs/op
14+
BenchmarkGetTokens_Complex-8 349312 4002 ns/op 13488 B/op 102 allocs/op
15+
BenchmarkGetTokens_Hex-8 981447 1231 ns/op 3984 B/op 37 allocs/op
16+
BenchmarkGetTokens_Strings-8 1000000 1116 ns/op 3696 B/op 34 allocs/op
17+
BenchmarkGetTokens_ManyTokens-8 346030 3432 ns/op 13104 B/op 98 allocs/op
18+
BenchmarkGetFilledTokens_Complex-8 223297 5263 ns/op 15648 B/op 109 allocs/op
19+
BenchmarkIsIdentChar-8 145176751 8.246 ns/op 0 B/op 0 allocs/op
20+
BenchmarkKeywordsLookup-8 27211629 50.22 ns/op 0 B/op 0 allocs/op
21+
PASS
22+
ok github.com/guiferpa/aurora/lexer 25.273s

lexer/scanner_bench_test.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package lexer
2+
3+
import (
4+
"testing"
5+
)
6+
7+
var (
8+
// Simple expression
9+
simpleInput = []byte(`ident foo = 123;`)
10+
11+
// Medium complexity - if statement
12+
mediumInput = []byte(`ident result = if 10 bigger 5 { 100; } else { 200; };`)
13+
14+
// Complex - multiple statements with function calls
15+
complexInput = []byte(`
16+
ident fib = {
17+
ident n = arguments 0;
18+
if n smaller 1 or n equals 1 { n; } else { fib(n - 1) + fib(n - 2); };
19+
};
20+
ident result = fib(10);
21+
print result;
22+
`)
23+
24+
// Hexadecimal numbers
25+
hexInput = []byte(`ident hex_ff = 0xFF; ident hex_10 = 0x10; ident sum = hex_ff + hex_10;`)
26+
27+
// String heavy
28+
stringInput = []byte(`ident greeting = "hello"; ident name = "world"; print greeting; print name;`)
29+
30+
// Many tokens
31+
manyTokensInput = []byte(`ident a = 1; ident b = 2; ident c = 3; ident d = 4; ident e = 5; ident f = 6; ident g = 7; ident h = 8; ident i = 9; ident j = 10;`)
32+
)
33+
34+
func BenchmarkScanToken_Keyword(b *testing.B) {
35+
input := []byte(`ident`)
36+
for i := 0; i < b.N; i++ {
37+
ScanToken(input)
38+
}
39+
}
40+
41+
func BenchmarkScanToken_Identifier(b *testing.B) {
42+
input := []byte(`myVariable123`)
43+
for i := 0; i < b.N; i++ {
44+
ScanToken(input)
45+
}
46+
}
47+
48+
func BenchmarkScanToken_Number(b *testing.B) {
49+
input := []byte(`123456`)
50+
for i := 0; i < b.N; i++ {
51+
ScanToken(input)
52+
}
53+
}
54+
55+
func BenchmarkScanToken_HexNumber(b *testing.B) {
56+
input := []byte(`0xABCDEF`)
57+
for i := 0; i < b.N; i++ {
58+
ScanToken(input)
59+
}
60+
}
61+
62+
func BenchmarkScanToken_String(b *testing.B) {
63+
input := []byte(`"hello world"`)
64+
for i := 0; i < b.N; i++ {
65+
ScanToken(input)
66+
}
67+
}
68+
69+
func BenchmarkScanToken_SingleChar(b *testing.B) {
70+
input := []byte(`(`)
71+
for i := 0; i < b.N; i++ {
72+
ScanToken(input)
73+
}
74+
}
75+
76+
func BenchmarkScanToken_Comment(b *testing.B) {
77+
input := []byte(`#- this is a comment`)
78+
for i := 0; i < b.N; i++ {
79+
ScanToken(input)
80+
}
81+
}
82+
83+
func BenchmarkGetTokens_Simple(b *testing.B) {
84+
for i := 0; i < b.N; i++ {
85+
GetTokens(simpleInput)
86+
}
87+
}
88+
89+
func BenchmarkGetTokens_Medium(b *testing.B) {
90+
for i := 0; i < b.N; i++ {
91+
GetTokens(mediumInput)
92+
}
93+
}
94+
95+
func BenchmarkGetTokens_Complex(b *testing.B) {
96+
for i := 0; i < b.N; i++ {
97+
GetTokens(complexInput)
98+
}
99+
}
100+
101+
func BenchmarkGetTokens_Hex(b *testing.B) {
102+
for i := 0; i < b.N; i++ {
103+
GetTokens(hexInput)
104+
}
105+
}
106+
107+
func BenchmarkGetTokens_Strings(b *testing.B) {
108+
for i := 0; i < b.N; i++ {
109+
GetTokens(stringInput)
110+
}
111+
}
112+
113+
func BenchmarkGetTokens_ManyTokens(b *testing.B) {
114+
for i := 0; i < b.N; i++ {
115+
GetTokens(manyTokensInput)
116+
}
117+
}
118+
119+
func BenchmarkGetFilledTokens_Complex(b *testing.B) {
120+
for i := 0; i < b.N; i++ {
121+
GetFilledTokens(complexInput)
122+
}
123+
}
124+
func BenchmarkIsIdentChar(b *testing.B) {
125+
chars := []byte("abcABC123_")
126+
for i := 0; i < b.N; i++ {
127+
for _, c := range chars {
128+
isIdentChar(c)
129+
}
130+
}
131+
}
132+
133+
func BenchmarkKeywordsLookup(b *testing.B) {
134+
words := []string{"if", "else", "ident", "print", "foo", "bar", "myVar"}
135+
for i := 0; i < b.N; i++ {
136+
for _, w := range words {
137+
_ = Keywords[w]
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)