Skip to content

Commit 8abb9c2

Browse files
committed
initial release
0 parents  commit 8abb9c2

16 files changed

+216
-0
lines changed

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/.bundle/
2+
/.yardoc
3+
/Gemfile.lock
4+
/_yardoc/
5+
/coverage/
6+
/doc/
7+
/pkg/
8+
/spec/reports/
9+
/tmp/

.rspec

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--format documentation
2+
--color

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: ruby
2+
rvm:
3+
- 2.1.2
4+
before_install: gem install bundler -v 1.10.4

Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in react-rails-benchmark_renderer.gemspec
4+
gemspec

README.md

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# React::Rails::BenchmarkRenderer
2+
3+
This idea started with a PR I sent to actual react-rails before it hit 1.0 release. I didn't have time to finish it then,
4+
and the structure of the react-rails codebase changed considerably, so it is now quite easy to write a renderer as a plugin.
5+
6+
This gem is a plugin to ract-rails providing two things:
7+
8+
1. `React::ServerRendering::Concern::Instrumentation`
9+
- benchmarking instrumentation of the sort found in rails view rendering
10+
- can be included into any Renderer to add Benchmarking to it
11+
2. `React::ServerRendering::BenchmarkRenderer`
12+
- a reference implementation of `React::ServerRendering::Concern::Instrumentation`
13+
14+
## Installation
15+
16+
Add this line to your application's Gemfile:
17+
18+
```ruby
19+
gem 'react-rails-benchmark_renderer'
20+
```
21+
22+
And then execute:
23+
24+
$ bundle
25+
26+
Or install it yourself as:
27+
28+
$ gem install react-rails-benchmark_renderer
29+
30+
## Usage
31+
32+
To simply use the reference implementation, just edit your application.rb with:
33+
34+
```
35+
config.react.server_renderer = React::ServerRendering::BenchmarkRenderer
36+
```
37+
38+
To roll your own renderer:
39+
40+
Use `Concern::Implementation` to enhance any React ServerRendering Renderer with benchmarking.
41+
42+
Usage is just like you can see in the reference implementation:
43+
44+
```ruby
45+
require "react/server_rendering/concerns/instrumentation"
46+
# Extends SprocketsRenderer for benchmarking in the Rails environment
47+
# - benchmarks rendering in the same manner as Rails view rendering is benchmarked by Rails
48+
module React
49+
module ServerRendering
50+
class BenchmarkRenderer < SprocketsRenderer
51+
include Concerns::Instrumentation
52+
end
53+
end
54+
end
55+
```
56+
57+
## Development
58+
59+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
60+
61+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
62+
63+
## Contributing
64+
65+
1. Fork it ( https://github.com/[my-github-username]/react-rails-benchmark_renderer/fork )
66+
2. Create your feature branch (`git checkout -b my-new-feature`)
67+
3. Commit your changes (`git commit -am 'Add some feature'`)
68+
4. Push to the branch (`git push origin my-new-feature`)
69+
5. Create a new Pull Request

Rakefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require "bundler/gem_tasks"
2+
require "rspec/core/rake_task"
3+
4+
RSpec::Core::RakeTask.new(:spec)
5+
6+
task :default => :spec

bin/console

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env ruby
2+
3+
require "bundler/setup"
4+
require "react/rails/benchmark_renderer"
5+
6+
# You can add fixtures and/or initialization code here to make experimenting
7+
# with your gem easier. You can also use a different console, if you like.
8+
9+
# (If you use this, don't forget to add pry to your Gemfile!)
10+
# require "pry"
11+
# Pry.start
12+
13+
require "irb"
14+
IRB.start

bin/setup

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
4+
5+
bundle install
6+
7+
# Do any other automated setup that you need to do here

lib/react-rails-benchmark_renderer.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require "react/rails/benchmark_renderer"

lib/react/rails/benchmark_renderer.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require "react/rails/benchmark_renderer/version"
2+
require "react/server_rendering/concerns/instrumentation"
3+
require "react/server_rendering/benchmark_renderer"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module React
2+
module Rails
3+
module BenchmarkRenderer
4+
VERSION = "0.1.0"
5+
end
6+
end
7+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require "react/server_rendering/concerns/instrumentation"
2+
3+
# Extends SprocketsRenderer for benchmarking in the Rails environment
4+
# - benchmarks rendering in the same manner as Rails view rendering is benchmarked by Rails
5+
module React
6+
module ServerRendering
7+
class BenchmarkRenderer < SprocketsRenderer
8+
include Concerns::Instrumentation
9+
end
10+
end
11+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module React
2+
module ServerRendering
3+
module Concerns
4+
module Instrumentation
5+
extend ActiveSupport::Concern
6+
7+
attr_internal :view_runtime
8+
9+
included do
10+
prepend RenderWithBenchmark
11+
end
12+
13+
module RenderWithBenchmark
14+
# Ripped mostly from actionpack/lib/action_controller/metal/instrumentation.rb
15+
def render(*args)
16+
render_output = nil
17+
self.view_runtime = cleanup_view_runtime do
18+
Benchmark.ms { render_output = super }
19+
end
20+
::Rails.logger.info("#{self.class}#render(#{args.join(", ")} in %.1fms" % view_runtime.to_f)
21+
render_output
22+
end
23+
24+
# Ripped directly from actionpack/lib/action_controller/metal/instrumentation.rb
25+
# A hook which allows you to clean up any time taken into account in
26+
# views wrongly, like database querying time.
27+
#
28+
# def cleanup_view_runtime
29+
# super - time_taken_in_something_expensive
30+
# end
31+
#
32+
# :api: plugin
33+
def cleanup_view_runtime #:nodoc:
34+
yield
35+
end
36+
end
37+
end
38+
end
39+
end
40+
end
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# coding: utf-8
2+
lib = File.expand_path('../lib', __FILE__)
3+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4+
require 'react/rails/benchmark_renderer/version'
5+
6+
Gem::Specification.new do |spec|
7+
spec.name = "react-rails-benchmark_renderer"
8+
spec.version = React::Rails::BenchmarkRenderer::VERSION
9+
spec.authors = ["Peter Boling"]
10+
spec.email = ["[email protected]"]
11+
12+
spec.summary = %q{Concern::Instrumentation Plugin for React::Rails Render Benchmarking with a reference implementation}
13+
spec.description = %q{Concern::Instrumentation Plugin for React::Rails Render Benchmarking with a reference implementation}
14+
spec.homepage = "TODO: Put your gem's website or public repo URL here."
15+
16+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17+
spec.bindir = "exe"
18+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19+
spec.require_paths = ["lib"]
20+
21+
spec.add_dependency "activesupport", ">= 3.2"
22+
spec.add_dependency "react-rails", ">= 1.0"
23+
spec.add_development_dependency "bundler", "~> 1.10"
24+
spec.add_development_dependency "rake", "~> 10.0"
25+
spec.add_development_dependency "rspec"
26+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'spec_helper'
2+
3+
describe React::Rails::BenchmarkRenderer do
4+
it 'has a version number' do
5+
expect(React::Rails::BenchmarkRenderer::VERSION).not_to be nil
6+
end
7+
8+
it 'does something useful' do
9+
expect(false).to eq(true)
10+
end
11+
end

spec/spec_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2+
require 'react/rails/benchmark_renderer'

0 commit comments

Comments
 (0)