Skip to content

Commit 208de2c

Browse files
authored
Add Laravel corpus wall-clock benchmark + CI (#2)
1 parent e18e2e3 commit 208de2c

5 files changed

Lines changed: 165 additions & 0 deletions

File tree

‎.github/workflows/benchmark.yaml‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: Benchmark
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
push:
8+
branches:
9+
- main
10+
schedule:
11+
# every 12 hours
12+
- cron: '0 */12 * * *'
13+
14+
jobs:
15+
corpus:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v4
19+
20+
- uses: actions/setup-go@v5
21+
with:
22+
go-version: "1.26.6"
23+
24+
- name: Clone + install Laravel corpus
25+
run: |
26+
git clone --depth 1 https://github.com/laravel/framework laravel
27+
composer install --no-interaction --no-progress --ignore-platform-reqs --working-dir=laravel
28+
# drop intentionally-broken PHP fixtures
29+
rm -rf laravel/tests
30+
find laravel/vendor -depth -type d -name tests -exec rm -rf {} +
31+
32+
- run: go build -o benchmark-corpus ./benchmark
33+
34+
- name: Run benchmark (timed + memory)
35+
run: |
36+
# average wall-clock over 5 runs
37+
bench() {
38+
total=0
39+
for i in $(seq 1 5); do
40+
start=$(date +%s%3N)
41+
"$@" > /dev/null
42+
end=$(date +%s%3N)
43+
total=$((total + end - start))
44+
done
45+
echo $((total / 5))
46+
}
47+
# peak resident set size (MB) of a single run
48+
mem() {
49+
/usr/bin/time -v "$@" 2>mem.log > /dev/null
50+
awk '/Maximum resident set size/{printf "%d", $NF/1024}' mem.log
51+
}
52+
ms=$(bench ./benchmark-corpus laravel)
53+
mb=$(mem ./benchmark-corpus laravel)
54+
files=$(./benchmark-corpus laravel | awk '{print $2}')
55+
{
56+
echo "## Corpus benchmark"
57+
echo ""
58+
echo "Parsing the full Laravel framework (\`src/\` + Composer \`vendor/\`), average wall-clock over 5 runs."
59+
echo ""
60+
echo "| Files | Avg (5 runs) | Peak mem |"
61+
echo "|------:|-------------:|---------:|"
62+
echo "| $files | $ms ms | $mb MB |"
63+
} | tee -a "$GITHUB_STEP_SUMMARY"

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
.idea
33
**/*.test
44

5+
/laravel/
6+
/benchmark-corpus
7+
58
cpu.pprof
69
mem.pprof
710
trace.out

‎Makefile‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ bench:
1717
go test -benchmem -bench=. ./internal/php5
1818
go test -benchmem -bench=. ./internal/php7
1919

20+
# wall-clock parse of a corpus, e.g. `make bench-corpus DIR=./laravel`
21+
bench-corpus:
22+
go run ./benchmark $(DIR)
23+
2024
compile: ./internal/php5/php5.go ./internal/php7/php7.go ./internal/php8/php8.go ./internal/php8/scanner.go ./internal/scanner/scanner.go
2125
sed -i '' -e 's/yyErrorVerbose = false/yyErrorVerbose = true/g' ./internal/php5/php5.go
2226
sed -i '' -e 's/yyErrorVerbose = false/yyErrorVerbose = true/g' ./internal/php7/php7.go

‎README.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ func main() {
4444
- `pkg/visitor` — traverser, printer, dumper, namespace and class resolvers, formatter.
4545
- `pkg/token`, `pkg/position`, `pkg/version`, `pkg/errors`, `pkg/conf`.
4646

47+
## Benchmark
48+
49+
`benchmark/` parses every `.php` file under a directory and reports the wall-clock time. Point it at any corpus:
50+
51+
```bash
52+
make bench-corpus DIR=./laravel
53+
# parsed 2966 files in 1215 ms
54+
```
55+
56+
CI runs it against a full Laravel framework checkout (`src/` + Composer `vendor/`) on every push and every 12 hours, reporting the average over 5 runs and peak memory in the run's **Summary**.
57+
4758
## Generated code
4859

4960
`internal/*/php*.go` and `internal/*/scanner.go` are generated. Edit the grammar source instead:

‎benchmark/main.go‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Corpus benchmark: parse every .php file under the given path and report timing.
2+
// Parsing runs across GOMAXPROCS workers with the GC disabled for the short-lived run.
3+
package main
4+
5+
import (
6+
"fmt"
7+
"io/fs"
8+
"os"
9+
"path/filepath"
10+
"runtime"
11+
"runtime/debug"
12+
"sync"
13+
"sync/atomic"
14+
"time"
15+
16+
"github.com/rectorphp/php-parser-in-go/pkg/conf"
17+
"github.com/rectorphp/php-parser-in-go/pkg/parser"
18+
"github.com/rectorphp/php-parser-in-go/pkg/version"
19+
)
20+
21+
func main() {
22+
root := "."
23+
if len(os.Args) > 1 {
24+
root = os.Args[1]
25+
}
26+
27+
phpVersion, err := version.New("8.3")
28+
if err != nil {
29+
fmt.Fprintln(os.Stderr, err)
30+
os.Exit(1)
31+
}
32+
config := conf.Config{Version: phpVersion}
33+
34+
// short-lived process: parse throughput matters, not steady-state memory
35+
debug.SetGCPercent(-1)
36+
37+
files := collectPHPFiles(root)
38+
39+
start := time.Now()
40+
parsed := parseAll(files, config)
41+
elapsed := time.Since(start)
42+
43+
fmt.Printf("parsed %d files in %d ms\n", parsed, elapsed.Milliseconds())
44+
}
45+
46+
// parseAll parses every file across GOMAXPROCS workers and returns the count parsed.
47+
func parseAll(files []string, config conf.Config) int64 {
48+
jobs := make(chan string, runtime.GOMAXPROCS(0))
49+
var parsed int64
50+
51+
var wg sync.WaitGroup
52+
for range runtime.GOMAXPROCS(0) {
53+
wg.Go(func() {
54+
for path := range jobs {
55+
content, err := os.ReadFile(path)
56+
if err != nil {
57+
continue
58+
}
59+
// broken fixtures return an error; the parse work is what we time
60+
parser.Parse(content, config)
61+
atomic.AddInt64(&parsed, 1)
62+
}
63+
})
64+
}
65+
66+
for _, path := range files {
67+
jobs <- path
68+
}
69+
close(jobs)
70+
wg.Wait()
71+
72+
return parsed
73+
}
74+
75+
func collectPHPFiles(root string) []string {
76+
var files []string
77+
filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
78+
if err == nil && !d.IsDir() && filepath.Ext(path) == ".php" {
79+
files = append(files, path)
80+
}
81+
return nil
82+
})
83+
return files
84+
}

0 commit comments

Comments
 (0)