Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4375 from lest/benchmark-helper
add benchmark helper that works in erb
- Loading branch information
Showing
3 changed files
with
39 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'active_support/benchmarkable' | ||
|
||
module ActionView | ||
module Helpers | ||
module BenchmarkHelper | ||
include ActiveSupport::Benchmarkable | ||
|
||
def benchmark(*) | ||
capture { super } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'abstract_unit' | ||
require 'stringio' | ||
|
||
class BenchmarkHelperTest < ActionView::TestCase | ||
include RenderERBUtils | ||
tests ActionView::Helpers::BenchmarkHelper | ||
|
||
def test_output_in_erb | ||
output = render_erb("Hello <%= benchmark do %>world<% end %>") | ||
expected = 'Hello world' | ||
assert_equal expected, output | ||
end | ||
|
||
def test_returns_value_from_block | ||
assert_equal 'test', benchmark { 'test' } | ||
end | ||
|
||
def test_default_message | ||
log = StringIO.new | ||
self.stubs(:logger).returns(Logger.new(log)) | ||
benchmark {} | ||
assert_match(log.rewind && log.read, /Benchmarking \(\d+.\d+ms\)/) | ||
end | ||
end |