Skip to content

Commit b6b5660

Browse files
committed
Add a simple benchmark script to bin/prism
1 parent 88cfd26 commit b6b5660

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

bin/prism

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Prism
88
class CLI
99
def run(argv)
1010
case argv.shift
11+
when "bench" then bench(argv)
1112
when "benchmark" then benchmark(argv)
1213
when "bundle" then bundle(argv)
1314
when "console" then console
@@ -25,6 +26,7 @@ module Prism
2526
else
2627
puts <<~TXT
2728
Usage:
29+
bin/prism bench [file ...]
2830
bin/prism benchmark [source_file]
2931
bin/prism bundle [...]
3032
bin/prism console
@@ -49,6 +51,37 @@ module Prism
4951
# Commands
5052
############################################################################
5153

54+
# bin/prism bench [file ...]
55+
# Measures raw parse throughput using Prism.profile (no Ruby AST creation).
56+
def bench(argv)
57+
require "benchmark/ips"
58+
59+
files =
60+
if argv.any?
61+
argv.map { |path| [path, File.read(path)] }
62+
else
63+
Dir[File.join(__dir__, "../test/prism/fixtures/**/*.txt")].sort.map { |path| [path, File.read(path)] } +
64+
Dir[File.join(__dir__, "../lib/**/*.rb")].sort.map { |path| [path, File.read(path)] }
65+
end
66+
67+
total_bytes = files.sum { |_, source| source.bytesize }
68+
puts "Benchmarking #{files.size} files (#{total_bytes} bytes total)"
69+
puts
70+
71+
Benchmark.ips do |x|
72+
x.time = 10
73+
x.warmup = 3
74+
75+
x.report("parse (all files)") do
76+
files.each { |_, source| Prism.profile(source) }
77+
end
78+
79+
x.report("parse (fixtures only)") do
80+
files.each { |path, source| Prism.profile(source) if path.end_with?(".txt") }
81+
end
82+
end
83+
end
84+
5285
# bin/prism benchmark [source_file]
5386
def benchmark(argv)
5487
require "benchmark/ips"

0 commit comments

Comments
 (0)