Skip to content
Prev Previous commit
Next Next commit
benchmark(ServerRendering) add benchmarks for different renderers
  • Loading branch information
rmosolgo committed Jun 8, 2015
commit e843b97606523fcd06919016cc300ec5e6c26160
51 changes: 51 additions & 0 deletions benchmarks/server_rendering_benchmark.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# - `gem install duktape`
# - Remove `JavaScriptCore` from RUNTIMES if you're not on mac
# - `ruby -I lib benchmarks/server_rendering_benchmark.rb`

require 'react-rails'
require 'duktape'
require 'benchmark'

SLOW_COMPONENT = "
var SlowComponent = React.createClass({
render: function() {
var rand = 0
for (var i = 0; i < 10000; i++) {
rand = rand + (Math.random() * i)
}
return React.createElement('h1', rand + ' :)')
}
})
"

REACT_JS_PATH = File.expand_path("../../vendor/react/react.js", __FILE__)
JS_CODE = File.read(REACT_JS_PATH) + SLOW_COMPONENT

React::ServerRendering.renderer = React::ServerRendering::ExecJSRenderer
React::ServerRendering.renderer_options = {code: JS_CODE}
React::ServerRendering.pool_timeout = 10

def test_runtime(runtime, renders:, pool_size:)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to have these explicit

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you mean? I think keyword args are good for expressing meaning

ExecJS.runtime = runtime
React::ServerRendering.pool_size = pool_size
React::ServerRendering.reset_pool

renders.times do
React::ServerRendering.render("SlowComponent", {}, {})
end
end

RUNTIMES = [
ExecJS::Runtimes::RubyRacer,
ExecJS::Runtimes::Duktape,
ExecJS::Runtimes::JavaScriptCore,
ExecJS::Runtimes::Node,
]

Benchmark.bm(25) do |x|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better comparison would be BM.ips

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh thanks, let me take a look at that...

[1, 10, 25].each do |pool_size|
RUNTIMES.each do |runtime|
x.report("#{runtime.name}, #{pool_size}x") { test_runtime(runtime, renders: 50, pool_size: pool_size)}
end
end
end