This repository has been archived by the owner on Dec 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e3fdf09
commit 37395a2
Showing
7 changed files
with
138 additions
and
13 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,36 @@ | ||
#---------------------------------------------------------- | ||
# This file demonstrates Rails performance with and without | ||
# the Timber library installed. Timber was designed with an | ||
# obsessive focus on performance and resource usage. The | ||
# below benchmarking test reveals this. A few notes: | ||
# 1. Both loggers are set to log to the same device. | ||
# 2. The log level is set to debug to maximize logging output. | ||
# 3. Timber is run last so that we can insert the probes after. | ||
# This ensures Timber does not affect the environment at all. | ||
# --------------------------------------------------------- | ||
|
||
$:.unshift File.dirname(__FILE__) | ||
require "support/rails" | ||
require "benchmark" | ||
require "logger" | ||
|
||
ITERATIONS = 5_000 | ||
PRECISION = 8 | ||
test = Proc.new { ITERATIONS.times { Support::Rails.dispatch_rails_request("/") } } | ||
|
||
puts "\nTesting Rails request performance. #{ITERATIONS} requests per test.\n\n" | ||
puts " Real Per request" | ||
|
||
without_timber = Benchmark.measure("without Timber", &test) | ||
without_timber_per = without_timber.real / ITERATIONS | ||
puts "#{without_timber.label} #{without_timber.real.round(PRECISION)} #{without_timber_per.round(PRECISION)}" | ||
|
||
# Install Timber | ||
Timber::Config.enabled = true | ||
Timber::Bootstrap.bootstrap!(RailsApp.config.app_middleware, ::Rails::Rack::Logger) | ||
|
||
with_timber = Benchmark.measure("with Timber", &test) | ||
with_timber_per = with_timber.real / ITERATIONS | ||
puts "#{with_timber.label} #{with_timber.real.round(PRECISION)} #{with_timber_per.round(PRECISION)}" | ||
|
||
puts "Difference #{(without_timber.real - with_timber.real).round(PRECISION)} #{(without_timber_per - with_timber_per).round(PRECISION)}" |
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,71 @@ | ||
require "rails" | ||
require "action_controller" | ||
require "timber" | ||
require "stringio" | ||
|
||
# Helper methods | ||
module Support | ||
module Rails | ||
def self.dispatch_rails_request(path) | ||
application = ::Rails.application | ||
env = application.respond_to?(:env_config) ? application.env_config.clone : application.env_defaults.clone | ||
env["rack.request.cookie_hash"] = {}.with_indifferent_access | ||
::Rack::MockRequest.new(application).get(path, env) | ||
end | ||
|
||
def self.set_logger(log_dev) | ||
logger = defined?(::ActiveSupport::Logger) ? ::ActiveSupport::Logger.new(log_dev) : ::Logger.new(log_dev) | ||
::Rails.logger = defined?(::ActiveSupport::TaggedLogging) ? ::ActiveSupport::TaggedLogging.new(logger) : logger | ||
::Rails.logger.level = ::Logger::DEBUG # log everything | ||
end | ||
|
||
def self.set_timber_logger | ||
set_logger(Timber::LogDevices::IO.new(StringIO.new)) | ||
end | ||
end | ||
end | ||
|
||
# Disable by default | ||
Timber::Config.enabled = false | ||
|
||
# Setup default rails logger with StringIO. | ||
# This ensures that the log data isn't output, but the level is sufficient | ||
# to be logged. | ||
Support::Rails.set_logger(StringIO.new) | ||
|
||
# Base rails app | ||
class RailsApp < Rails::Application | ||
if ::Rails.version =~ /^3\./ | ||
config.secret_token = '1e05af2b349457936a41427e63450937' | ||
else | ||
config.secret_key_base = '1e05af2b349457936a41427e63450937' | ||
end | ||
config.active_support.deprecation = :stderr | ||
config.eager_load = false | ||
end | ||
|
||
# Start the app to get initialization out of the way | ||
RailsApp.initialize! | ||
|
||
# Setup a controller | ||
class PagesController < ActionController::Base | ||
layout nil | ||
|
||
def index | ||
# Similuate above average logging for a single action | ||
25.times { Rails.logger.info("this is a test log message") } | ||
render json: {} | ||
end | ||
|
||
def method_for_action(action_name) | ||
action_name | ||
end | ||
end | ||
|
||
# Some routes | ||
::RailsApp.routes.draw do | ||
get '/' => 'pages#index' | ||
end | ||
|
||
# Dispatch a request to get the initial caching / loading out of the way | ||
Support::Rails.dispatch_rails_request("/") |
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
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Timber | ||
VERSION = "0.0.1-beta1".freeze | ||
VERSION = "1.0.0.beta1" | ||
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