forked from codetriage/CodeTriage
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartial_cache.rb
51 lines (36 loc) · 1.34 KB
/
partial_cache.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
require"benchmark/ips"
require"action_view"
require"action_pack"
require"action_controller"
require"active_record"
require"stackprof"
ActiveRecord::Base.establish_connection(adapter: "sqlite3",database: ":memory:")
ActiveRecord::Schema.definedo
create_table:customers,force: truedo |t|
t.string:name
end
end
classCustomer < ActiveRecord::Base;end
classTestController < ActionController::Base
end
ActionView::PartialRenderer.collection_cache=ActiveSupport::Cache::MemoryStore.new
TestController.view_paths=[File.expand_path("perf_scripts/partial_cache")]
putsTestController.view_paths
# Create a bunch of data
letters=("a".."z").to_a * 5
1000.times{Customer.create!(name: letters.sample(10).join)}
customers=Customer.all.to_a
controller_view=TestController.new.view_context
# Heat compilation cache and collection cache
controller_view.render("render_collection",customers: customers)
controller_view.render("render_collection_with_cache",customers: customers)
pActionView::PartialRenderer.collection_cache# 1000 entries
Benchmark.ipsdo |x|
x.report("collection render: no cache")do
controller_view.render("render_collection",customers: customers)
end
x.report("collection render: with cache")do
controller_view.render("render_collection_with_cache",customers: customers)
end
x.compare!
end