|
| 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