Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/codspeed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: CodSpeed

on:
push:
branches:
- "main"
pull_request:
# `workflow_dispatch` allows CodSpeed to trigger backtest
# performance analysis in order to generate initial data.
workflow_dispatch:

permissions:
contents: read
id-token: write

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
cancel-in-progress: ${{ github.ref_name != 'main' }}

jobs:
benchmarks:
name: Run benchmarks
runs-on: codspeed-macro
steps:
- name: Checkout code
uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
with:
submodules: true
fetch-depth: 1

- name: Setup Go
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
with:
go-version: "1.26.0"
cache: false

- name: Run benchmarks
uses: CodSpeedHQ/action@db35df748deb45fdef0960669f57d627c1956c30 # v4.13.1
with:
mode: walltime
run: go test -bench=. -benchtime=5s ./internal/linter/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<a href="https://npmjs.com/package/@rslint/core?activeTab=readme"><img src="https://img.shields.io/npm/v/@rslint/core?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" /></a>
<a href="https://npmcharts.com/compare/@rslint/core?minimal=true"><img src="https://img.shields.io/npm/dm/@rslint/core.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="downloads" /></a>
<a href="https://github.com/web-infra-dev/rslint/blob/main/LICENSE"><img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="license" /></a>
<a href="https://codspeed.io/web-infra-dev/rslint?utm_source=badge"><img src="https://img.shields.io/endpoint?url=https://codspeed.io/badge.json" alt="CodSpeed"/></a>
</p>

> [!NOTE]
Expand Down
332 changes: 332 additions & 0 deletions internal/linter/linter_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,332 @@
package linter

import (
"os"
"path/filepath"
"sync"
"testing"

"github.com/microsoft/typescript-go/shim/ast"
"github.com/microsoft/typescript-go/shim/bundled"
"github.com/microsoft/typescript-go/shim/compiler"
"github.com/microsoft/typescript-go/shim/tspath"
"github.com/microsoft/typescript-go/shim/vfs/cachedvfs"
"github.com/microsoft/typescript-go/shim/vfs/osvfs"
"github.com/web-infra-dev/rslint/internal/rule"
"github.com/web-infra-dev/rslint/internal/utils"
)

// sampleTypeScriptCode is a realistic TypeScript file with various constructs
// that exercise different lint rules.
const sampleTypeScriptCode = `
interface User {
name: string;
age: number;
email?: string;
}

type Status = 'active' | 'inactive' | 'pending';

function processUser(user: User): string {
var result = '';
var temp = user.name;

if (user.age == 18) {
console.log('Adult user:', user.name);
result = temp + ' is an adult';
}

for (var i = 0; i < 10; i++) {
result += String(i);
}

const items: string[] = [];
for (var item of ['a', 'b', 'c']) {
items.push(item);
}

if (user.email != null) {
console.warn('Email exists');
}

switch (user.age) {
case 18:
result = 'young';
break;
case 30:
result = 'mid';
break;
default:
result = 'other';
}

return result;
}

class UserService {
private users: User[] = [];

addUser(user: User): void {
var existing = this.users.find(u => u.name === user.name);
if (existing == undefined) {
this.users.push(user);
}
}

getUsers(): User[] {
return this.users;
}

processAll(): string[] {
var results: string[] = [];
for (var u of this.users) {
results.push(processUser(u));
}
return results;
}
}

export { UserService, processUser };
export type { User, Status };
`

// benchNoVarRule returns a configured rule that reports all var declarations.
func benchNoVarRule() ConfiguredRule {
return ConfiguredRule{
Name: "no-var",
Severity: rule.SeverityWarning,
Run: func(ctx rule.RuleContext) rule.RuleListeners {
return rule.RuleListeners{
ast.KindVariableDeclarationList: func(node *ast.Node) {
if node.Flags&ast.NodeFlagsBlockScoped != 0 {
return
}
ctx.ReportNode(node.Parent, rule.RuleMessage{
Id: "unexpectedVar",
Description: "Unexpected var, use let or const instead.",
})
},
}
},
}
}

// benchEqeqeqRule returns a configured rule that reports loose equality comparisons.
func benchEqeqeqRule() ConfiguredRule {
return ConfiguredRule{
Name: "eqeqeq",
Severity: rule.SeverityWarning,
Run: func(ctx rule.RuleContext) rule.RuleListeners {
return rule.RuleListeners{
ast.KindBinaryExpression: func(node *ast.Node) {
bin := node.AsBinaryExpression()
if bin == nil {
return
}
op := bin.OperatorToken.Kind
if op == ast.KindEqualsEqualsToken || op == ast.KindExclamationEqualsToken {
ctx.ReportNode(node, rule.RuleMessage{
Id: "unexpected",
Description: "Expected '===' and instead saw '=='.",
})
}
},
}
},
}
}

// benchNoConsoleRule returns a configured rule that reports console usage.
func benchNoConsoleRule() ConfiguredRule {
return ConfiguredRule{
Name: "no-console",
Severity: rule.SeverityWarning,
Run: func(ctx rule.RuleContext) rule.RuleListeners {
return rule.RuleListeners{
ast.KindPropertyAccessExpression: func(node *ast.Node) {
propAccess := node.AsPropertyAccessExpression()
if propAccess == nil {
return
}
if propAccess.Expression.Kind != ast.KindIdentifier {
return
}
if propAccess.Expression.AsIdentifier().Text != "console" {
return
}
ctx.ReportNode(node, rule.RuleMessage{
Id: "unexpected",
Description: "Unexpected console statement.",
})
},
}
},
}
}

// setupBenchProgram creates a compiler.Program with the given source files for benchmarking.
// This follows the same approach as createTestProgramWithFiles in linter_test.go.
func setupBenchProgram(b *testing.B, sourceFiles map[string]string) (*compiler.Program, map[string]string) {
b.Helper()

tmpDir := b.TempDir()

includes := make([]string, 0, len(sourceFiles))
normalizedPaths := make(map[string]string, len(sourceFiles))
for name, content := range sourceFiles {
filePath := filepath.Join(tmpDir, name)
dir := filepath.Dir(filePath)
if err := os.MkdirAll(dir, 0755); err != nil {
b.Fatalf("Failed to create directory %s: %v", dir, err)
}
if err := os.WriteFile(filePath, []byte(content), 0644); err != nil {
b.Fatalf("Failed to write %s: %v", name, err)
}
includes = append(includes, "./"+name)
normalizedPaths[name] = tspath.NormalizePath(filePath)
}

includeJSON := `"` + joinStrings(includes, `","`) + `"`
tsconfig := `{"compilerOptions":{"strict":true,"target":"esnext","module":"commonjs"},"include":[` + includeJSON + `]}`
if err := os.WriteFile(filepath.Join(tmpDir, "tsconfig.json"), []byte(tsconfig), 0644); err != nil {
b.Fatalf("Failed to write tsconfig: %v", err)
}

fs := bundled.WrapFS(cachedvfs.From(osvfs.FS()))
host := utils.CreateCompilerHost(tmpDir, fs)
program, err := utils.CreateProgram(true, fs, tmpDir, "tsconfig.json", host)
if err != nil {
b.Fatalf("Failed to create program: %v", err)
}

return program, normalizedPaths
}

func joinStrings(strs []string, sep string) string {
if len(strs) == 0 {
return ""
}
result := strs[0]
for _, s := range strs[1:] {
result += sep + s

Check failure on line 211 in internal/linter/linter_bench_test.go

View workflow job for this annotation

GitHub Actions / Lint&Check

concat-loop: string concatenation in a loop (perfsprint)
}
return result
}

// runLintBenchmark is the common linting benchmark harness.
func runLintBenchmark(b *testing.B, code string, rules []ConfiguredRule) {
b.Helper()

program, normalizedPaths := setupBenchProgram(b, map[string]string{"file.ts": code})
filePath := normalizedPaths["file.ts"]
allowedFiles := []string{filePath}

// Verify the program was created properly
sourceFile := program.GetSourceFile(filePath)
if sourceFile == nil {
b.Fatalf("Source file not found at path: %s", filePath)
}

b.ResetTimer()
for b.Loop() {
var mu sync.Mutex
diagnostics := make([]rule.RuleDiagnostic, 0, 16)

RunLinterInProgram(
program,
allowedFiles,
nil,
[]string{},
func(sourceFile *ast.SourceFile) []ConfiguredRule {
return rules
},
false,
func(diagnostic rule.RuleDiagnostic) {
mu.Lock()
diagnostics = append(diagnostics, diagnostic)
mu.Unlock()
},
nil,
nil,
)
}
}

func BenchmarkLintSingleRule_NoVar(b *testing.B) {
runLintBenchmark(b, sampleTypeScriptCode, []ConfiguredRule{benchNoVarRule()})
}

func BenchmarkLintSingleRule_Eqeqeq(b *testing.B) {
runLintBenchmark(b, sampleTypeScriptCode, []ConfiguredRule{benchEqeqeqRule()})
}

func BenchmarkLintSingleRule_NoConsole(b *testing.B) {
runLintBenchmark(b, sampleTypeScriptCode, []ConfiguredRule{benchNoConsoleRule()})
}

func BenchmarkLintMultipleRules(b *testing.B) {
rules := []ConfiguredRule{
benchNoVarRule(),
benchEqeqeqRule(),
benchNoConsoleRule(),
}
runLintBenchmark(b, sampleTypeScriptCode, rules)
}

// benchLargeTypeScriptCode generates a larger TypeScript file to benchmark scaling behavior.
func benchLargeTypeScriptCode() string {
base := "function process%d(input: string): string {\n" +
" var result = input;\n" +
" if (result == '') {\n" +
" console.log('empty');\n" +
" result = 'default';\n" +
" }\n" +
" for (var i = 0; i < 10; i++) {\n" +
" result += String(i);\n" +
" }\n" +
" return result;\n" +
"}\n\n"

var code string
for i := 0; i < 50; i++ {

Check failure on line 291 in internal/linter/linter_bench_test.go

View workflow job for this annotation

GitHub Actions / Lint&Check

for loop can be changed to use an integer range (Go 1.22+) (intrange)
code += benchReplacePercD(base, i)

Check failure on line 292 in internal/linter/linter_bench_test.go

View workflow job for this annotation

GitHub Actions / Lint&Check

concat-loop: string concatenation in a loop (perfsprint)
}
code += "\nexport {};\n"
return code
}

func benchItoa(i int) string {
if i == 0 {
return "0"
}
s := ""
n := i
for n > 0 {
s = string(rune('0'+n%10)) + s
n /= 10
}
return s
}

func benchReplacePercD(s string, i int) string {
result := ""
for j := 0; j < len(s); j++ {
if j+1 < len(s) && s[j] == '%' && s[j+1] == 'd' {
result += benchItoa(i)

Check failure on line 315 in internal/linter/linter_bench_test.go

View workflow job for this annotation

GitHub Actions / Lint&Check

concat-loop: string concatenation in a loop (perfsprint)
j++
} else {
result += string(s[j])
}
}
return result
}

func BenchmarkLintLargeFile(b *testing.B) {
code := benchLargeTypeScriptCode()
rules := []ConfiguredRule{
benchNoVarRule(),
benchEqeqeqRule(),
benchNoConsoleRule(),
}
runLintBenchmark(b, code, rules)
}
Loading