Skip to content

Commit 2028853

Browse files
authored
Add binder benchmark, update parse benchmark, restructure fixtures (#5)
1 parent 41b3d71 commit 2028853

File tree

5 files changed

+134
-12
lines changed

5 files changed

+134
-12
lines changed

Herebyfile.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export const generate = task({
5656
export const test = task({
5757
name: "test",
5858
run: async () => {
59-
assertTypeScriptCloned();
6059
await $`go test ${options.race ? ["-race"] : []} ./...`;
6160
// Run the benchmarks once to ensure they compile and run without errors.
6261
await $`go test ${options.race ? ["-race"] : []} -run=- -bench=. -benchtime=1x ./...`;

internal/compiler/binder_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package compiler
2+
3+
import (
4+
"runtime"
5+
"testing"
6+
)
7+
8+
func BenchmarkBind(b *testing.B) {
9+
for _, f := range benchFixtures {
10+
b.Run(f.Name(), func(b *testing.B) {
11+
f.SkipIfNotExist(b)
12+
13+
fileName := f.Path()
14+
sourceText := f.ReadFile(b)
15+
16+
sourceFiles := make([]*SourceFile, b.N)
17+
for i := 0; i < b.N; i++ {
18+
sourceFiles[i] = ParseSourceFile(fileName, sourceText, ScriptTargetESNext)
19+
}
20+
21+
compilerOptions := &CompilerOptions{Target: ScriptTargetESNext, ModuleKind: ModuleKindNodeNext}
22+
23+
// The above parses do a lot of work; ensure GC is settled before we start collecting pefrormance data.
24+
runtime.GC()
25+
runtime.GC()
26+
27+
b.ResetTimer()
28+
for i := 0; i < b.N; i++ {
29+
bindSourceFile(sourceFiles[i], compilerOptions)
30+
}
31+
})
32+
}
33+
}

internal/compiler/fixtures_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package compiler
2+
3+
import (
4+
"path/filepath"
5+
6+
"github.com/microsoft/typescript-go/internal/repo"
7+
"github.com/microsoft/typescript-go/internal/testutil/filefixture"
8+
)
9+
10+
var benchFixtures = []filefixture.Fixture{
11+
filefixture.FromString("empty.ts", "empty.ts", ""),
12+
filefixture.FromFile("checker.ts", filepath.Join(repo.TypeScriptSubmodulePath, "src/compiler/checker.ts")),
13+
filefixture.FromFile("dom.generated.d.ts", filepath.Join(repo.TypeScriptSubmodulePath, "src/lib/dom.generated.d.ts")),
14+
filefixture.FromFile("Herebyfile.mjs", filepath.Join(repo.TypeScriptSubmodulePath, "Herebyfile.mjs")),
15+
filefixture.FromFile("jsxComplexSignatureHasApplicabilityError.tsx", filepath.Join(repo.TypeScriptSubmodulePath, "tests/cases/compiler/jsxComplexSignatureHasApplicabilityError.tsx")),
16+
}

internal/compiler/parser_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package compiler
22

33
import (
4-
"fmt"
5-
"os"
64
"testing"
75
)
86

97
func BenchmarkParse(b *testing.B) {
10-
fileName := "../../_submodules/TypeScript/src/compiler/checker.ts"
11-
bytes, err := os.ReadFile(fileName)
12-
if err != nil {
13-
fmt.Println(err)
14-
}
15-
sourceText := string(bytes)
16-
b.ResetTimer()
17-
for i := 0; i < b.N; i++ {
18-
ParseSourceFile(fileName, sourceText, ScriptTargetESNext)
8+
for _, f := range benchFixtures {
9+
b.Run(f.Name(), func(b *testing.B) {
10+
f.SkipIfNotExist(b)
11+
12+
fileName := f.Path()
13+
sourceText := f.ReadFile(b)
14+
15+
for i := 0; i < b.N; i++ {
16+
ParseSourceFile(fileName, sourceText, ScriptTargetESNext)
17+
}
18+
})
1919
}
2020
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package filefixture
2+
3+
import (
4+
"os"
5+
"sync"
6+
"testing"
7+
)
8+
9+
type Fixture interface {
10+
Name() string
11+
Path() string
12+
SkipIfNotExist(t testing.TB)
13+
ReadFile(t testing.TB) string
14+
}
15+
16+
type fromFile struct {
17+
name string
18+
path string
19+
contents func() (string, error)
20+
}
21+
22+
func FromFile(name string, path string) Fixture {
23+
return &fromFile{
24+
name: name,
25+
path: path,
26+
// Cache the file contents and errors.
27+
contents: sync.OnceValues(func() (string, error) {
28+
b, err := os.ReadFile(path)
29+
return string(b), err
30+
}),
31+
}
32+
}
33+
34+
func (f *fromFile) Name() string { return f.name }
35+
func (f *fromFile) Path() string { return f.path }
36+
37+
func (f *fromFile) SkipIfNotExist(t testing.TB) {
38+
t.Helper()
39+
40+
if _, err := os.Stat(f.path); err != nil {
41+
t.Skipf("Test fixture %q does not exist", f.path)
42+
}
43+
}
44+
45+
func (f *fromFile) ReadFile(t testing.TB) string {
46+
t.Helper()
47+
48+
contents, err := f.contents()
49+
if err != nil {
50+
t.Fatalf("Failed to read test fixture %q: %v", f.path, err)
51+
}
52+
return contents
53+
}
54+
55+
type fromString struct {
56+
name string
57+
path string
58+
contents string
59+
}
60+
61+
func FromString(name string, path string, contents string) Fixture {
62+
return &fromString{
63+
name: name,
64+
path: path,
65+
contents: contents,
66+
}
67+
}
68+
69+
func (f *fromString) Name() string { return f.name }
70+
func (f *fromString) Path() string { return f.path }
71+
72+
func (f *fromString) SkipIfNotExist(t testing.TB) {}
73+
74+
func (f *fromString) ReadFile(t testing.TB) string { return f.contents }

0 commit comments

Comments
 (0)