Skip to content

Commit 7414a8c

Browse files
authored
Merge pull request #3965 from ruby/bench
Add a simple benchmark script to bin/prism
2 parents 819a796 + 17c404f commit 7414a8c

1 file changed

Lines changed: 21 additions & 33 deletions

File tree

bin/prism

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Prism
88
class CLI
99
def run(argv)
1010
case argv.shift
11-
when "benchmark" then benchmark(argv)
11+
when "bench" then bench(argv)
1212
when "bundle" then bundle(argv)
1313
when "console" then console
1414
when "dot" then dot(argv)
@@ -25,7 +25,7 @@ module Prism
2525
else
2626
puts <<~TXT
2727
Usage:
28-
bin/prism benchmark [source_file]
28+
bin/prism bench [file ...]
2929
bin/prism bundle [...]
3030
bin/prism console
3131
bin/prism dot [source]
@@ -49,46 +49,34 @@ module Prism
4949
# Commands
5050
############################################################################
5151

52-
# bin/prism benchmark [source_file]
53-
def benchmark(argv)
52+
# bin/prism bench [file ...]
53+
# Measures raw parse throughput using Prism.profile (no Ruby AST creation).
54+
def bench(argv)
5455
require "benchmark/ips"
55-
require "parser/current"
56-
require "ripper"
57-
require "ruby_parser"
58-
59-
filepath = argv.fetch(0) { File.expand_path("../lib/prism/node.rb", __dir__) }
60-
source = File.read(filepath)
61-
62-
Benchmark.ips do |x|
63-
x.report("Prism") do
64-
Prism.parse(source, filepath: filepath)
65-
end
6656

67-
x.report("Ripper::SexpBuilder") do
68-
Ripper.sexp_raw(source, filepath)
69-
end
70-
71-
x.report("Prism::Translation::Ripper::SexpBuilder") do
72-
Prism::Translation::Ripper.sexp_raw(source, filepath)
57+
files =
58+
if argv.any?
59+
argv.map { |path| [path, File.read(path)] }
60+
else
61+
Dir[File.join(__dir__, "../test/prism/fixtures/**/*.txt")].sort.map { |path| [path, File.read(path)] } +
62+
Dir[File.join(__dir__, "../lib/**/*.rb")].sort.map { |path| [path, File.read(path)] }
7363
end
7464

75-
x.report("Parser::CurrentRuby") do
76-
Parser::CurrentRuby.parse(source, filepath)
77-
end
65+
total_bytes = files.sum { |_, source| source.bytesize }
66+
puts "Benchmarking #{files.size} files (#{total_bytes} bytes total)"
67+
puts
7868

79-
x.report("Prism::Translation::Parser") do
80-
Prism::Translation::Parser.parse(source, filepath)
81-
end
69+
Benchmark.ips do |x|
70+
x.time = 10
71+
x.warmup = 3
8272

83-
x.report("RubyParser") do
84-
RubyParser.new.parse(source, filepath)
73+
x.report("parse (all files)") do
74+
files.each { |_, source| Prism.profile(source) }
8575
end
8676

87-
x.report("Prism::Translation::RubyParser") do
88-
Prism::Translation::RubyParser.new.parse(source, filepath)
77+
x.report("parse (fixtures only)") do
78+
files.each { |path, source| Prism.profile(source) if path.end_with?(".txt") }
8979
end
90-
91-
x.compare!
9280
end
9381
end
9482

0 commit comments

Comments
 (0)