Skip to content

Commit f5eee20

Browse files
committed
Adding page loading time metrics
This change creates a new table where we simply log how much time a page (url, controller, action, was it a turbo call?) took to render, so that we can provide some statistics on the admin panel.
1 parent 4c1cb8c commit f5eee20

5 files changed

Lines changed: 188 additions & 3 deletions

File tree

app/controllers/application_controller.rb

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
class ApplicationController < ActionController::Base
22
allow_browser versions: :modern
3-
3+
4+
around_action :track_page_load_time
45
rescue_from ActiveRecord::RecordNotFound, with: :render_404
56
helper_method :current_user, :user_signed_in?, :current_admin?
67
helper_method :activity_unread_count
78
before_action :authorize_mini_profiler
8-
9+
910
private
1011

1112
def render_404
@@ -43,4 +44,26 @@ def activity_unread_count
4344
return 0 unless current_user
4445
@activity_unread_count ||= Activity.where(user: current_user, hidden: false, read_at: nil).count
4546
end
47+
48+
def track_page_load_time
49+
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
50+
yield
51+
ensure
52+
begin
53+
duration_ms = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) * 1000.0
54+
55+
unless request.path == "/up" || !(request.format.html? || request.format.turbo_stream?)
56+
PageLoadStat.insert({
57+
url: request.path,
58+
controller: controller_name,
59+
action: action_name,
60+
render_time: duration_ms.round(1),
61+
is_turbo: turbo_frame_request? || request.format.turbo_stream?,
62+
created_at: Time.current
63+
})
64+
end
65+
rescue StandardError
66+
# Never let logging break a request
67+
end
68+
end
4669
end

app/models/page_load_stat.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class PageLoadStat < ApplicationRecord
2+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class CreatePageLoadStats < ActiveRecord::Migration[8.0]
2+
def change
3+
create_table :page_load_stats do |t|
4+
t.string :url, null: false
5+
t.string :controller, null: false
6+
t.string :action, null: false
7+
t.float :render_time, null: false
8+
t.boolean :is_turbo, default: false, null: false
9+
t.datetime :created_at, null: false
10+
end
11+
12+
add_index :page_load_stats, :created_at
13+
add_index :page_load_stats, [:controller, :action]
14+
end
15+
end

db/schema.rb

Lines changed: 111 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/models/page_load_stat_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe PageLoadStat, type: :model do
4+
it 'persists a record with all attributes' do
5+
PageLoadStat.insert({
6+
url: '/topics/1',
7+
controller: 'topics',
8+
action: 'show',
9+
render_time: 42.5,
10+
is_turbo: false,
11+
created_at: Time.current
12+
})
13+
14+
stat = PageLoadStat.last
15+
expect(stat.url).to eq('/topics/1')
16+
expect(stat.controller).to eq('topics')
17+
expect(stat.action).to eq('show')
18+
expect(stat.render_time).to be_within(0.01).of(42.5)
19+
expect(stat.is_turbo).to eq(false)
20+
expect(stat.created_at).to be_present
21+
end
22+
23+
it 'stores turbo requests' do
24+
PageLoadStat.insert({
25+
url: '/topics/1',
26+
controller: 'topics',
27+
action: 'show',
28+
render_time: 15.0,
29+
is_turbo: true,
30+
created_at: Time.current
31+
})
32+
33+
expect(PageLoadStat.last.is_turbo).to eq(true)
34+
end
35+
end

0 commit comments

Comments
 (0)