Skip to content

Commit e4e332f

Browse files
authored
Print timing for page requests and re-parsing in server mode (#1648)
## Summary - Silence the stats progress bar in server mode — it uses `\r` on a tty which collides with the server's own logging - Print response time for page requests (not assets or status polls): `200 /RDoc.html (12.3ms)` - Print re-parse duration with relative paths: `Re-parsed lib/rdoc.rb (17.5ms)` ``` Serving documentation at: http://localhost:4000 Press Ctrl+C to stop. 200 /RDoc.html (14.9ms) Re-parsed lib/rdoc.rb (15.7ms) 200 /RDoc.html (8.5ms) ```
1 parent 78325e1 commit e4e332f

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

lib/rdoc/server.rb

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ def initialize(rdoc, port)
6767
@store = rdoc.store
6868
@port = port
6969

70+
# Silence stats output — the server prints its own timing.
71+
@rdoc.stats.verbosity = 0
7072
@generator = create_generator
7173
@template_dir = File.expand_path(@generator.template_dir)
7274
@page_cache = {}
@@ -102,6 +104,12 @@ def start
102104

103105
private
104106

107+
def measure
108+
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
109+
yield
110+
((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000).round(1)
111+
end
112+
105113
def create_generator
106114
gen = RDoc::Generator::Aliki.new(@store, @options)
107115
gen.file_output = false
@@ -138,7 +146,14 @@ def handle_client(client)
138146
return write_response(client, 405, 'text/plain', 'Method Not Allowed')
139147
end
140148

141-
status, content_type, body = route(path)
149+
if path.start_with?('/__') || %r{\A/(?:css|js)/}.match?(path)
150+
status, content_type, body = route(path)
151+
else
152+
duration_ms = measure do
153+
status, content_type, body = route(path)
154+
end
155+
$stderr.puts "#{status} #{path} (#{duration_ms}ms)"
156+
end
142157
write_response(client, status, content_type, body)
143158
rescue => e
144159
write_response(client, 500, 'text/html', <<~HTML)
@@ -185,6 +200,8 @@ def write_response(client, status, content_type, body)
185200
client.write(header)
186201
client.write(body_bytes)
187202
client.flush
203+
rescue Errno::EPIPE
204+
# Client disconnected before we finished writing — harmless.
188205
end
189206

190207
##
@@ -347,19 +364,23 @@ def reparse_and_refresh(changed_files, removed_files)
347364
end
348365

349366
unless changed_files.empty?
350-
$stderr.puts "Re-parsing: #{changed_files.join(', ')}"
351-
changed_files.each do |f|
352-
begin
367+
changed_file_names = []
368+
duration_ms = measure do
369+
changed_files.each do |f|
353370
relative = @rdoc.relative_path_for(f)
354-
@store.clear_file_contributions(relative, keep_position: true)
355-
@rdoc.parse_file(f)
356-
@file_mtimes[f] = File.mtime(f) rescue nil
357-
rescue => e
358-
$stderr.puts "Error parsing #{f}: #{e.message}"
371+
changed_file_names << relative
372+
begin
373+
@store.clear_file_contributions(relative, keep_position: true)
374+
@rdoc.parse_file(f)
375+
@file_mtimes[f] = File.mtime(f) rescue nil
376+
rescue => e
377+
$stderr.puts "Error parsing #{f}: #{e.message}"
378+
end
359379
end
360-
end
361380

362-
@store.cleanup_stale_contributions
381+
@store.cleanup_stale_contributions
382+
end
383+
$stderr.puts "Re-parsed #{changed_file_names.join(', ')} (#{duration_ms}ms)"
363384
end
364385

365386
@store.complete(@options.visibility)

lib/rdoc/stats.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,17 @@ def initialize(store, num_files, verbosity = 1)
3939
@start = Time.now
4040
@undoc_params = 0
4141

42+
self.verbosity = verbosity
43+
end
44+
45+
##
46+
# Sets the verbosity level, rebuilding the display outputter.
47+
48+
def verbosity=(verbosity)
4249
@display = case verbosity
43-
when 0 then Quiet.new num_files
44-
when 1 then Normal.new num_files
45-
else Verbose.new num_files
50+
when 0 then Quiet.new @num_files
51+
when 1 then Normal.new @num_files
52+
else Verbose.new @num_files
4653
end
4754
end
4855

0 commit comments

Comments
 (0)