/
sample.rb
57 lines (51 loc) · 1.91 KB
/
sample.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: true
require 'rack-mini-profiler'
require 'sinatra'
require 'sinatra/base'
class Sample < Sinatra::Base
use Rack::MiniProfiler
@@data = JSON.parse(File.read(File.expand_path("data.json", __dir__)), symbolize_names: true)
@@page_struct = Rack::MiniProfiler::TimerStruct::Page.allocate
@@page_struct.instance_variable_set(:@attributes, @@data)
@@patched = false
get '/' do
storage_instance = Rack::MiniProfiler.config.storage_instance
storage_instance.instance_variable_set(:@timer_struct_cache, {})
storage_instance.instance_variable_set(:@user_view_cache, {})
storage_instance.save(@@page_struct)
patch_mini_profiler(@@page_struct) unless @@patched
help = <<~TEXT
To the left of this page there should be 2 speed badges.
The first one (with small load time) represents metrics for this particular page.
The other one (with ~2.5 seconds load time) is taken from a real application (Discourse),
modified so that it has as many UI elements as possible and always included with this page
to make easier for you to test your JavaScript and CSS changes. Refreshing this page should
be enough to see any changes you make to files in the <code>lib/html</code> directory.
TEXT
body = <<~HTML
<html>
<head>
</head>
<body>
<div style="margin: auto; width: 660px; font-family: Arial">
<h2>Rack Mini Profiler</h2>
<p>#{help.split("\n").join(' ')}</p>
</div>
</body>
</html>
HTML
body.dup
end
def patch_mini_profiler(page)
Rack::MiniProfiler.send(:define_method, :cache_control_value) do |*args|
0
end
Rack::MiniProfiler.send(:alias_method, :ids_original, :ids)
Rack::MiniProfiler.send(:define_method, :ids) do |*args, &blk|
s = self.send(:ids_original, *args, &blk)
s << page[:id]
s
end
@@patched = true
end
end