diff options
author | Robert Speicher <robert@gitlab.com> | 2017-06-12 22:23:49 +0300 |
---|---|---|
committer | Robert Speicher <robert@gitlab.com> | 2017-06-12 22:23:49 +0300 |
commit | 5a66e6d968fb9b1bd1159b9156a88bd06208e363 (patch) | |
tree | 99bd4cdca441db24dd538c6811f9d76c12d2de58 /lib/gitlab | |
parent | 8d7951d879d13894dcc1fd5d845c8d844bee321a (diff) | |
parent | be0a949a004a34015977bdbcfe24e3173cd3aba9 (diff) |
Merge branch '29010-perf-bar' into 'master'
Add an optional performance bar to view performance metrics for the current page
Closes #29010
See merge request !11439
Diffstat (limited to 'lib/gitlab')
-rw-r--r-- | lib/gitlab/performance_bar.rb | 7 | ||||
-rw-r--r-- | lib/gitlab/performance_bar/peek_performance_bar_with_rack_body.rb | 22 | ||||
-rw-r--r-- | lib/gitlab/performance_bar/peek_query_tracker.rb | 39 |
3 files changed, 68 insertions, 0 deletions
diff --git a/lib/gitlab/performance_bar.rb b/lib/gitlab/performance_bar.rb new file mode 100644 index 00000000000..163a40ad306 --- /dev/null +++ b/lib/gitlab/performance_bar.rb @@ -0,0 +1,7 @@ +module Gitlab + module PerformanceBar + def self.enabled? + Feature.enabled?('gitlab_performance_bar') + end + end +end diff --git a/lib/gitlab/performance_bar/peek_performance_bar_with_rack_body.rb b/lib/gitlab/performance_bar/peek_performance_bar_with_rack_body.rb new file mode 100644 index 00000000000..d939a6ea18d --- /dev/null +++ b/lib/gitlab/performance_bar/peek_performance_bar_with_rack_body.rb @@ -0,0 +1,22 @@ +# This solves a bug with a X-Senfile header that wouldn't be set properly, see +# https://github.com/peek/peek-performance_bar/pull/27 +module Gitlab + module PerformanceBar + module PeekPerformanceBarWithRackBody + def call(env) + @env = env + reset_stats + + @total_requests += 1 + first_request if @total_requests == 1 + + env['process.request_start'] = @start.to_f + env['process.total_requests'] = total_requests + + status, headers, body = @app.call(env) + body = Rack::BodyProxy.new(body) { record_request } + [status, headers, body] + end + end + end +end diff --git a/lib/gitlab/performance_bar/peek_query_tracker.rb b/lib/gitlab/performance_bar/peek_query_tracker.rb new file mode 100644 index 00000000000..7ab80f5ee0f --- /dev/null +++ b/lib/gitlab/performance_bar/peek_query_tracker.rb @@ -0,0 +1,39 @@ +# Inspired by https://github.com/peek/peek-pg/blob/master/lib/peek/views/pg.rb +module Gitlab + module PerformanceBar + module PeekQueryTracker + def sorted_queries + PEEK_DB_CLIENT.query_details. + sort { |a, b| b[:duration] <=> a[:duration] } + end + + def results + super.merge(queries: sorted_queries) + end + + private + + def setup_subscribers + super + + # Reset each counter when a new request starts + before_request do + PEEK_DB_CLIENT.query_details = [] + end + + subscribe('sql.active_record') do |_, start, finish, _, data| + if RequestStore.active? && RequestStore.store[:peek_enabled] + track_query(data[:sql].strip, data[:binds], start, finish) + end + end + end + + def track_query(raw_query, bindings, start, finish) + query = Gitlab::Sherlock::Query.new(raw_query, start, finish) + query_info = { duration: '%.3f' % query.duration, sql: query.formatted_query } + + PEEK_DB_CLIENT.query_details << query_info + end + end + end +end |