Skip to content

Commit 9b80841

Browse files
committed
testing
1 parent e2b66d8 commit 9b80841

File tree

5 files changed

+71
-9
lines changed

5 files changed

+71
-9
lines changed

app/controllers/railsdb/admin/stats_controller.rb

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module Admin
33
class StatsController < ApplicationController
44
def index
55
@stats = Railsdb::Admin::Stats.fetch_avg_durations
6-
pp @stats
76
end
87

98
private

app/views/railsdb/admin/stats/index.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<td class='is-vcentered'><%= stat[:count] %></td>
1818
<td class='is-vcentered'><%= stat[:avg_duration] %></td>
1919
<td class='is-vcentered'><%= stat[:sql] %></td>
20-
<td class='is-vcentered'><%= stat[:gc_time] %></td>
20+
<td class='is-vcentered'><%= stat[:min_duration] %></td>
2121
<td class='is-vcentered'><%= stat[:max_duration] %></td>
2222
</tr>
2323
<% end %>

lib/railsdb/admin/metric_subscriber.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
require 'prettyprint'
22
module Railsdb
33
module Admin
4-
class MetricSubscriber
4+
module MetricSubscriber
55
def self.add_instrumentation
66
ActiveSupport::Notifications.subscribe "sql.active_record" do |*args|
77
event = ActiveSupport::Notifications::Event.new(*args)
8+
# pp args
89

910
event.payload[:alt_name] = event.payload[:name] || name_from_sql(event.payload[:sql])
1011
unless ["SCHEMA", "TRANSACTION", "ActiveRecord::SchemaMigration Load"].include?(event.payload[:name])

lib/railsdb/admin/stats.rb

+4-6
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,16 @@ def self.fetch_avg_durations
1111
avg_duration = durations.sum / durations.size
1212
first_event = events.first
1313
payload = first_event.payload
14-
gc_time = events.map(&:gc_time).sum
1514

1615
{
1716
sql: payload[:sql].split(" /*").first,
18-
avg_duration: avg_duration,
17+
avg_duration: avg_duration.round(2),
1918
name: payload[:name] || payload[:alt_name],
2019
count: events.size,
21-
max_duration: durations.max,
22-
min_duration: durations.min,
23-
gc_time: gc_time
20+
max_duration: durations.max.round(2),
21+
min_duration: durations.min.round(2)
2422
}
25-
end
23+
end.sort_by { |event| event[:avg_duration] }
2624
end
2725

2826
private

test/railsdb/admin/stats_test.rb

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
require "test_helper"
2+
3+
class Railsdb::Admin::StatsTest < ActiveSupport::TestCase
4+
setup do
5+
Railsdb::Admin::EventStore.instance.clear
6+
@stats_klass = Railsdb::Admin::Stats
7+
end
8+
9+
test "returns an array of hashes" do
10+
Product.all.to_a
11+
catalog = Catalog.create!(title: "Catalog", available: true)
12+
Product.create!(name: "Product", code: 100, catalog: catalog)
13+
14+
stats = @stats_klass.fetch_avg_durations
15+
assert_equal(3, stats.size)
16+
end
17+
18+
test "returns one event for product creation" do
19+
catalog = Catalog.create!(title: "Catalog", available: true)
20+
Product.create!(name: "Product", code: 100, catalog: catalog)
21+
Product.create!(name: "Product good", code: 101, catalog: catalog)
22+
23+
stats = @stats_klass.fetch_avg_durations
24+
assert_equal(2, stats.size)
25+
end
26+
27+
test "returns sql query" do
28+
Product.all.to_a
29+
30+
stats = @stats_klass.fetch_avg_durations
31+
32+
first_event = "SELECT \"products\".* FROM \"products\""
33+
assert_equal(first_event, stats.first[:sql])
34+
end
35+
36+
def create_events
37+
event = ActiveSupport::Notifications::Event.new("sql.active_record",
38+
10.seconds.ago,
39+
3.seconds.ago,
40+
1,
41+
{ sql: "SELECT * FROM products" })
42+
event_2 = ActiveSupport::Notifications::Event.new("sql.active_record",
43+
10.seconds.ago,
44+
5.seconds.ago,
45+
6,
46+
{ sql: "SELECT * FROM products" })
47+
Railsdb::Admin::EventStore.instance.add_event(event)
48+
Railsdb::Admin::EventStore.instance.add_event(event_2)
49+
end
50+
51+
test "calculates avg duration" do
52+
create_events
53+
stats = @stats_klass.fetch_avg_durations
54+
55+
assert_equal(6000, stats.first[:avg_duration].to_i)
56+
end
57+
58+
test "counts the amount of query calls" do
59+
create_events
60+
stats = @stats_klass.fetch_avg_durations
61+
62+
assert_equal(2, stats.first[:count])
63+
end
64+
end

0 commit comments

Comments
 (0)