Debian Codegraph @

data/munin/groonga_query_performance_

raw ◀ ▶
1 Debian patch touch this file
•1 #!/usr/bin/ruby
2
3 #%# family=auto
4 #%# capabilities=autoconf suggest
5
6 require 'English'
7 require 'strscan'
8
9 label = ENV["label"]
10
11 command = ARGV.shift
12
13 def autoconf
14 query_log_path_variable_names = [
15 "query_log_path",
16 "log_path", # For backward compatibility. Remove me when 5.0.0.
17 "http_query_log_path",
18 "httpd_query_log_path",
19 "gqtp_query_log_path",
20 ]
21 query_log_paths = query_log_path_variable_names.collect do |variable_name|
22 ENV[variable_name]
23 end
24 query_log_paths = query_log_paths.compact
25 if query_log_paths.empty?
26 variable_names = query_log_path_variable_names.collect do |name|
27 "env.#{name}"
28 end
29 variable_names_label = variable_names[0..-2].join(", ")
30 variable_names_label << " and/or #{variable_names.last}"
31 puts("no (No query log path is specified. Specify #{variable_names_label}.)")
32 exit(false)
33 end
34 query_log_paths.each do |query_log_path|
35 next unless File.exist?(query_log_path)
36 puts("yes")
37 exit(true)
38 end
39 query_log_paths_label = query_log_paths.join(", ")
40 puts("no (All query log paths don't exist: #{query_log_paths_label})")
41 exit(false)
42 end
43
44 def suggest
45 env_path_exist_p = lambda do |variable_name|
46 path = ENV[variable_name]
47 path and File.exist?(path)
48 end
49 exist_p = lambda do |query_log_path_name, pid_file_path_name|
50 env_path_exist_p.call(query_log_path_name) and
51 env_path_exist_p.call(pid_file_path_name)
52 end
53 if exist_p.call("http_query_log_path", "http_pid_path")
54 puts("http")
55 end
56 if exist_p.call("httpd_query_log_path", "httpd_pid_path")
57 puts("httpd")
58 end
59 if exist_p.call("gqtp_query_log_path", "gqtp_pid_path")
60 puts("gqtp")
61 end
62 exit(true)
63 end
64
65 def target_query_log_path
66 if /_([^_]+)\z/ =~ $0
67 service_type = $1
68 query_log_path_variable_name = "#{service_type}_query_log_path"
69 query_log_path = ENV[query_log_path_variable_name]
70 else
71 query_log_path_variable_name = "query_log_path"
72 query_log_path = ENV[query_log_path_variable_name]
73 # For backward compatibility. Remove me when 5.0.0.
74 query_log_path ||= ENV["log_file"]
75 end
76
77 if query_log_path.nil?
78 key = "env.#{query_log_path_variable_name}"
79 $stderr.puts("Query log path isn't specified by #{key}")
80 exit(false)
81 end
82
83 unless File.exist?(query_log_path)
84 $stderr.puts("Query log path doesn't exist: #{query_log_path}")
85 exit(false)
86 end
87
88 query_log_path
89 end
90
91 class ReverseLineReader
92 def initialize(io)
93 @io = io
94 @io.seek(0, IO::SEEK_END)
95 @buffer = ""
96 @data = ""
97 end
98
99 def each
100 separator = $/
101 separator_length = separator.length
102 while read_to_buffer
103 loop do
104 index = @buffer.rindex(separator, @buffer.length - 1 - separator_length)
105 break if index.nil? or index.zero?
106 last_line = @buffer.slice!((index + separator_length)..-1)
107 yield(last_line)
108 end
109 end
110 yield(@buffer) unless @buffer.empty?
111 end
112
113 private
114 BYTES_PER_READ = 4096
115 def read
116 position = @io.pos
117 if position < BYTES_PER_READ
118 bytes_per_read = position
119 else
120 bytes_per_read = BYTES_PER_READ
121 end
122
123 if bytes_per_read.zero?
124 @data.replace("")
125 else
126 @io.seek(-bytes_per_read, IO::SEEK_CUR)
127 @io.read(bytes_per_read, @data)
128 @io.seek(-bytes_per_read, IO::SEEK_CUR)
129 end
130
131 @data
132 end
133
134 def read_to_buffer
135 data = read
136 if data.empty?
137 false
138 else
139 @buffer.insert(0, data)
140 true
141 end
142 end
143 end
144
145 case command
146 when "autoconf", "detect"
147 autoconf
148 when "suggest"
149 suggest
150 when "config"
151 if label.nil?
152 title = "groonga: query performance"
153 else
154 title = "groonga: #{label}: query performance"
155 end
156 puts(<<EOF)
157 graph_title #{title}
158 graph_vlabel seconds
159 graph_category groonga
160 graph_info groonga query performance
161
162 longest.label Longest
163 average.label Average
164 median.label Median
165 EOF
166 exit(true)
167 end
168
169 span = 60 * 5 # 5min
170 mega = 1_000_000.0
171 now = Time.now
172 elapsed_times = []
173 File.open(target_query_log_path) do |log_file|
174 ReverseLineReader.new(log_file).each do |line|
175 case line
176 when /\A(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)\.(\d+)\|((?:0x)?[\da-f]+)\|<(\d+) rc=0$/
177 _, year, month, day, hour, minutes, seconds, milliseconds,
178 context, elapsed = $LAST_MATCH_INFO.to_a
179 time_stamp = Time.local(year, month, day,
180 hour, minutes, seconds, milliseconds)
181 difference = now - time_stamp
182 break if difference > span
183 elapsed_in_micro_seconds = elapsed.to_i / mega
184 elapsed_times << elapsed_in_micro_seconds
185 end
186 end
187 end
188
189 sorted_elapsed_times = elapsed_times.sort
190 if sorted_elapsed_times.empty?
191 longest = 0
192 average = 0
193 median = 0
194 else
195 longest = sorted_elapsed_times.last
196 average = sorted_elapsed_times.inject(&:+) / sorted_elapsed_times.size.to_f
197 median = sorted_elapsed_times[sorted_elapsed_times.size / 2]
198 end
199
200 puts("longest.value #{longest}")
201 puts("average.value #{average}")
202 puts("median.value #{median}")