From c0aab820ed0bdc356f528bf1515773d0d3371a19 Mon Sep 17 00:00:00 2001 From: Justin Li Date: Wed, 12 Nov 2014 00:05:01 -0500 Subject: [PATCH 1/4] Lazily load profiler hooks --- lib/liquid.rb | 5 ++++- lib/liquid/profiler.rb | 4 ++++ lib/liquid/profiler/hooks.rb | 6 ++++++ lib/liquid/template.rb | 2 ++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/liquid.rb b/lib/liquid.rb index af38cd4..cacb0d8 100644 --- a/lib/liquid.rb +++ b/lib/liquid.rb @@ -41,6 +41,10 @@ module Liquid singleton_class.send(:attr_accessor, :cache_classes) self.cache_classes = true + + def self.load_profiler_hooks + require 'liquid/profiler/hooks' + end end require "liquid/version" @@ -75,4 +79,3 @@ require 'liquid/token' Dir[File.dirname(__FILE__) + '/liquid/tags/*.rb'].each { |f| require f } require 'liquid/profiler' -require 'liquid/profiler/hooks' diff --git a/lib/liquid/profiler.rb b/lib/liquid/profiler.rb index 912804d..a90c303 100644 --- a/lib/liquid/profiler.rb +++ b/lib/liquid/profiler.rb @@ -70,6 +70,10 @@ module Liquid end end + def self.hooks_loaded + false + end + def self.profile_token_render(token) if Profiler.current_profile && token.respond_to?(:render) Profiler.current_profile.start_token(token) diff --git a/lib/liquid/profiler/hooks.rb b/lib/liquid/profiler/hooks.rb index 8a6e808..fc2736a 100644 --- a/lib/liquid/profiler/hooks.rb +++ b/lib/liquid/profiler/hooks.rb @@ -1,4 +1,10 @@ module Liquid + class Profiler + def self.hooks_loaded + true + end + end + class BlockBody def render_token_with_profiling(token, context) Profiler.profile_token_render(token) do diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index 8862564..d143dfd 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -250,6 +250,8 @@ module Liquid def with_profiling if @profiling && !@options[:included] + Liquid.load_profiler_hooks unless Profiler.hooks_loaded + @profiler = Profiler.new @profiler.start From 8c70682d6b757952b26f558ae8f6d54a2097a081 Mon Sep 17 00:00:00 2001 From: Justin Li Date: Thu, 4 Dec 2014 17:39:41 -0500 Subject: [PATCH 2/4] Don't automatically load hooks --- lib/liquid.rb | 4 ---- lib/liquid/profiler.rb | 8 +++++++- lib/liquid/profiler/hooks.rb | 6 ------ lib/liquid/template.rb | 2 +- test/test_helper.rb | 1 + 5 files changed, 9 insertions(+), 12 deletions(-) diff --git a/lib/liquid.rb b/lib/liquid.rb index cacb0d8..70d26de 100644 --- a/lib/liquid.rb +++ b/lib/liquid.rb @@ -41,10 +41,6 @@ module Liquid singleton_class.send(:attr_accessor, :cache_classes) self.cache_classes = true - - def self.load_profiler_hooks - require 'liquid/profiler/hooks' - end end require "liquid/version" diff --git a/lib/liquid/profiler.rb b/lib/liquid/profiler.rb index a90c303..f253e2d 100644 --- a/lib/liquid/profiler.rb +++ b/lib/liquid/profiler.rb @@ -71,7 +71,13 @@ module Liquid end def self.hooks_loaded - false + @hooks_loaded + end + + def self.load_hooks + return if @hooks_loaded + require 'liquid/profiler/hooks' + @hooks_loaded = true end def self.profile_token_render(token) diff --git a/lib/liquid/profiler/hooks.rb b/lib/liquid/profiler/hooks.rb index fc2736a..8a6e808 100644 --- a/lib/liquid/profiler/hooks.rb +++ b/lib/liquid/profiler/hooks.rb @@ -1,10 +1,4 @@ module Liquid - class Profiler - def self.hooks_loaded - true - end - end - class BlockBody def render_token_with_profiling(token, context) Profiler.profile_token_render(token) do diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index d143dfd..e10d429 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -250,7 +250,7 @@ module Liquid def with_profiling if @profiling && !@options[:included] - Liquid.load_profiler_hooks unless Profiler.hooks_loaded + raise "Profiler hooks not loaded, call Liquid::Profiler.load_hooks first" unless Profiler.hooks_loaded @profiler = Profiler.new @profiler.start diff --git a/test/test_helper.rb b/test/test_helper.rb index 75d5491..e56efee 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -12,6 +12,7 @@ if env_mode = ENV['LIQUID_PARSER_MODE'] mode = env_mode.to_sym end Liquid::Template.error_mode = mode +Liquid::Profiler.load_hooks if Minitest.const_defined?('Test') # We're on Minitest 5+. Nothing to do here. From dd455a63613e577ba0fa8cf5fa3b54f833c6a710 Mon Sep 17 00:00:00 2001 From: Justin Li Date: Thu, 4 Dec 2014 17:48:26 -0500 Subject: [PATCH 3/4] Force user to require the profiler themselves --- lib/liquid.rb | 2 -- lib/liquid/profiler.rb | 17 +++++------------ lib/liquid/template.rb | 2 +- test/test_helper.rb | 2 +- 4 files changed, 7 insertions(+), 16 deletions(-) diff --git a/lib/liquid.rb b/lib/liquid.rb index 70d26de..d6334ec 100644 --- a/lib/liquid.rb +++ b/lib/liquid.rb @@ -73,5 +73,3 @@ require 'liquid/token' # Load all the tags of the standard library # Dir[File.dirname(__FILE__) + '/liquid/tags/*.rb'].each { |f| require f } - -require 'liquid/profiler' diff --git a/lib/liquid/profiler.rb b/lib/liquid/profiler.rb index f253e2d..67ad6fa 100644 --- a/lib/liquid/profiler.rb +++ b/lib/liquid/profiler.rb @@ -1,9 +1,12 @@ +require 'liquid/profiler/hooks' + module Liquid # Profiler enables support for profiling template rendering to help track down performance issues. # - # To enable profiling, pass the profile: true option to Liquid::Template.parse. Then, after - # Liquid::Template#render is called, the template object makes available an instance of this + # To enable profiling, first require 'liquid/profiler'. + # Then, to profile a parse/render cycle, pass the profile: true option to Liquid::Template.parse. + # After Liquid::Template#render is called, the template object makes available an instance of this # class via the Liquid::Template#profiler method. # # template = Liquid::Template.parse(template_content, profile: true) @@ -70,16 +73,6 @@ module Liquid end end - def self.hooks_loaded - @hooks_loaded - end - - def self.load_hooks - return if @hooks_loaded - require 'liquid/profiler/hooks' - @hooks_loaded = true - end - def self.profile_token_render(token) if Profiler.current_profile && token.respond_to?(:render) Profiler.current_profile.start_token(token) diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index e10d429..a134962 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -250,7 +250,7 @@ module Liquid def with_profiling if @profiling && !@options[:included] - raise "Profiler hooks not loaded, call Liquid::Profiler.load_hooks first" unless Profiler.hooks_loaded + raise "Profiler not loaded, require 'liquid/profiler' first" unless defined?(Profiler) @profiler = Profiler.new @profiler.start diff --git a/test/test_helper.rb b/test/test_helper.rb index e56efee..438f075 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -5,6 +5,7 @@ require 'spy/integration' $:.unshift(File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib')) require 'liquid.rb' +require 'liquid/profiler' mode = :strict if env_mode = ENV['LIQUID_PARSER_MODE'] @@ -12,7 +13,6 @@ if env_mode = ENV['LIQUID_PARSER_MODE'] mode = env_mode.to_sym end Liquid::Template.error_mode = mode -Liquid::Profiler.load_hooks if Minitest.const_defined?('Test') # We're on Minitest 5+. Nothing to do here. From b628477af1226670eb2ef652106a9fe8df64dedb Mon Sep 17 00:00:00 2001 From: Justin Li Date: Thu, 4 Dec 2014 17:51:54 -0500 Subject: [PATCH 4/4] Disambiguate checking if Liquid::Profiler is defined --- lib/liquid/template.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index a134962..e86dfb5 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -250,7 +250,7 @@ module Liquid def with_profiling if @profiling && !@options[:included] - raise "Profiler not loaded, require 'liquid/profiler' first" unless defined?(Profiler) + raise "Profiler not loaded, require 'liquid/profiler' first" unless defined?(Liquid::Profiler) @profiler = Profiler.new @profiler.start