null+****@clear*****
null+****@clear*****
2011年 6月 3日 (金) 16:00:23 JST
Kouhei Sutou 2011-06-03 07:00:23 +0000 (Fri, 03 Jun 2011) New Revision: d21a1fdc104c4e1a0a5bcd99864fd5e8c44bf08c Log: [query-log][analyzer] support streaming report. Modified files: tools/groonga-query-log-analyzer.rb Modified: tools/groonga-query-log-analyzer.rb (+96 -15) =================================================================== --- tools/groonga-query-log-analyzer.rb 2011-06-03 05:09:12 +0000 (b49c754) +++ tools/groonga-query-log-analyzer.rb 2011-06-03 07:00:23 +0000 (c223d34) @@ -13,9 +13,14 @@ class GroongaQueryLogAnaylzer def run(argv=nil) log_paths = @option_parser.parse!(argv || ARGV) + stream = @options[:stream] dynamic_sort = @options[:dynamic_sort] statistics = SizedStatistics.new(@options[:n_entries], @options[:order]) - if dynamic_sort + if stream + streamer = Streamer.new(create_reporter(statistics)) + streamer.start + parser = QueryLogParser.new(streamer) + elsif dynamic_sort parser = QueryLogParser.new(statistics) else full_statistics = [] @@ -26,6 +31,10 @@ class GroongaQueryLogAnaylzer parser.parse(log) end end + if stream + streamer.finish + return + end statistics.replace(full_statistics) unless dynamic_sort reporter = create_reporter(statistics) @@ -43,6 +52,7 @@ class GroongaQueryLogAnaylzer @options[:slow_threshold] = 0.05 @options[:reporter] = "console" @options[:dynamic_sort] = true + @options[:stream] = false @option_parser = OptionParser.new do |parser| parser.banner += " LOG1 ..." @@ -112,6 +122,13 @@ class GroongaQueryLogAnaylzer "(#{@options[:dynamic_sort]})") do |sort| @options[:dynamic_sort] = sort end + + parser.on("--[no-]stream", + "Outputs analyzed query on the fly.", + "NOTE: --n-entries and --order are ignored.", + "(#{@options[:stream]})") do |stream| + @options[:stream] = stream + end end def create_reporter(statistics) @@ -123,6 +140,16 @@ class GroongaQueryLogAnaylzer ConsoleQueryLogReporter.new(statistics) end end + + def create_stream_reporter + case @options[:reporter] + when "json" + require 'json' + StreamJSONQueryLogReporter.new + else + StreamConsoleQueryLogReporter.new + end + end end class Command @@ -403,6 +430,24 @@ class GroongaQueryLogAnaylzer end end + class Streamer + def initialize(reporter) + @reporter = reporter + end + + def start + @reporter.start + end + + def <<(statistic) + @reporter.report_statistic(statistic) + end + + def finish + @reporter.finish + end + end + class QueryLogReporter include Enumerable @@ -436,13 +481,17 @@ class GroongaQueryLogAnaylzer end def setup_output + original_output = @output if****@outpu*****_a?(String) File.open(@output, "w") do |output| - yield(output) + @output = output + yield(@output) end else yield(@output) end + ensure + @output = original_output end end @@ -556,19 +605,37 @@ class GroongaQueryLogAnaylzer @color = options[:color] || @color end + def start + @index = 0 + if****@stati*****? + @digit = 1 + else + @digit = Math.log10(@statistics.size).truncate + 1 + end + end + def report setup_output do |output| setup_color(output) do - digit = Math.log10(@statistics.size).truncate + 1 - each_with_index do |statistic, i| - output.puts "%*d) %s" % [digit, i + 1, format_heading(statistic)] - report_parameters(output, statistic) - report_steps(output, statistic) + start + each do |statistic| + report_statistic(statistic) end + finish end end end + def finish + end + + def report_statistic(statistic) + @index += 1 + @output.puts "%*d) %s" % [@digit, @index, format_heading(statistic)] + report_parameters(@output, statistic) + report_steps(@output, statistic) + end + private def report_parameters(output, statistic) command = statistic.command @@ -654,19 +721,33 @@ class GroongaQueryLogAnaylzer end class JSONQueryLogReporter < QueryLogReporter + def start + @index = 0 + @output.print("[") + end + def report - setup_output do |output| - output.print("[") - each_with_index do |statistic, i| - output.print(",") if i > 0 - output.print("\n") - output.print(format_statistic(statistic)) + setup_output do + start + each do |statistic| + report_statistic(statistic) end - output.puts - output.puts("]") + finish end end + def finish + @output.puts + @output.puts("]") + end + + def report_statistic(statistic) + @output.print(",") if @index > 0 + @output.print("\n") + @output.print(format_statistic(statistic)) + @index += 1 + end + private def format_statistic(statistic) data = {