Skip to content

Commit 858b3e1

Browse files
makenowjustAry Borenszweig
authored and
Ary Borenszweig
committedDec 6, 2016
Enhance --stats option
1 parent 2e9c9b5 commit 858b3e1

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed
 

‎src/compiler/crystal/command.cr

+25-13
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class Crystal::Command
5252

5353
def initialize(@options : Array(String))
5454
@color = true
55+
@stats = false
5556
end
5657

5758
def run
@@ -172,7 +173,9 @@ class Crystal::Command
172173

173174
private def hierarchy
174175
config, result = compile_no_codegen "tool hierarchy", hierarchy: true, top_level: true
175-
Crystal.print_hierarchy result.program, config.hierarchy_exp, config.output_format
176+
Crystal.timing("Tool (hierarchy)", @stats, delay: true) do
177+
Crystal.print_hierarchy result.program, config.hierarchy_exp, config.output_format
178+
end
176179
end
177180

178181
private def run_command(single_file = false)
@@ -190,7 +193,9 @@ class Crystal::Command
190193

191194
private def types
192195
config, result = compile_no_codegen "tool types"
193-
Crystal.print_types result.node
196+
Crystal.timing("Tool (types)", @stats, delay: true) do
197+
Crystal.print_types result.node
198+
end
194199
end
195200

196201
private def compile_no_codegen(command, wants_doc = false, hierarchy = false, cursor_command = false, top_level = false)
@@ -202,15 +207,17 @@ class Crystal::Command
202207
end
203208

204209
private def execute(output_filename, run_args)
205-
begin
206-
Process.run(output_filename, args: run_args, input: true, output: true, error: true) do |process|
207-
# Ignore the signal so we don't exit the running process
208-
# (the running process can still handle this signal)
209-
Signal::INT.ignore # do
210+
status = Crystal.timing("Execute", @stats, delay: true) do
211+
begin
212+
Process.run(output_filename, args: run_args, input: true, output: true, error: true) do |process|
213+
# Ignore the signal so we don't exit the running process
214+
# (the running process can still handle this signal)
215+
Signal::INT.ignore # do
216+
end
217+
$?
218+
ensure
219+
File.delete(output_filename) rescue nil
210220
end
211-
status = $?
212-
ensure
213-
File.delete(output_filename) rescue nil
214221
end
215222

216223
if status.normal_exit?
@@ -351,9 +358,14 @@ class Crystal::Command
351358
opts.on("--release", "Compile in release mode") do
352359
compiler.release = true
353360
end
354-
opts.on("-s", "--stats", "Enable statistics output") do
355-
compiler.stats = true
356-
end
361+
end
362+
363+
opts.on("-s", "--stats", "Enable statistics output") do
364+
@stats = true
365+
compiler.stats = true
366+
end
367+
368+
unless no_codegen
357369
opts.on("--single-module", "Generate a single LLVM module") do
358370
compiler.single_module = true
359371
end

‎src/compiler/crystal/command/cursor.cr

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ class Crystal::Command
4545

4646
file = File.expand_path(file)
4747

48-
result = yield Location.new(file, line_number, column_number), config, result
48+
result = Crystal.timing("Tool (#{command.split(' ')[1]})", @stats) do
49+
yield Location.new(file, line_number, column_number), config, result
50+
end
4951

5052
case format
5153
when "json"

‎src/compiler/crystal/util.cr

+7-3
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,19 @@ module Crystal
2323
exit(exit_code) if exit_code
2424
end
2525

26-
def self.timing(label, stats)
26+
def self.timing(label, stats, delay = false)
2727
if stats
28-
print "%-34s" % "#{label}:"
28+
print "%-34s" % "#{label}:" unless delay
2929
time = Time.now
3030
value = yield
3131
elapsed_time = Time.now - time
3232
LibGC.get_heap_usage_safe(out heap_size, out free_bytes, out unmapped_bytes, out bytes_since_gc, out total_bytes)
3333
mb = heap_size / 1024.0 / 1024.0
34-
puts " %s (%7.2fMB)" % {elapsed_time, mb}
34+
if delay
35+
puts "%-34s %s (%7.2fMB)" % {"#{label}:", elapsed_time, mb}
36+
else
37+
puts " %s (%7.2fMB)" % {elapsed_time, mb}
38+
end
3539
value
3640
else
3741
yield

0 commit comments

Comments
 (0)
Please sign in to comment.