johnthethird
6/9/2015 - 4:25 AM

React-Rails Benchmark Script

React-Rails Benchmark Script

require 'react-rails'
require 'benchmark'

SLOW_COMPONENT = "
var SlowComponent = React.createClass({
  render: function() {
    var items = [];
    for (var i = 0; i < 250; i++) {
      items.push(React.createElement('li', {}, 'item:'+i));
    }
    return React.createElement('ul', {}, items)
  }
})
"

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 = 1000

def test_runtime(runtime, pool_size, thread_size, renders)
  ExecJS.runtime = runtime
  React::ServerRendering.pool_size = pool_size
  React::ServerRendering.reset_pool

  threads = thread_size.times.map do
    Thread.new do
      (renders/thread_size).to_i.times do
        React::ServerRendering.render("SlowComponent", {}, {})
      end
    end
  end
  threads.map(&:join)
end

if RUBY_ENGINE == "ruby"
  RENDERS = 2000
  RUNTIMES = [
    #ExecJS::Runtimes::RubyRacer
    #ExecJS::Runtimes::Duktape,
    ExecJS::Runtimes::JavaScriptCore,
    ExecJS::Runtimes::Node
  ]
else
  RENDERS = 2000
  RUNTIMES = [
    ExecJS::Runtimes::RubyRhino
  ]
  puts "JRUBY -- Warmup the JVM..."
  test_runtime(ExecJS::Runtimes::RubyRhino, 1, 1, RENDERS)
end

printf "%-20s %-10s %-10s %-10s", "Runtime", "Threads", "Pool", "Renders"
Benchmark.bm(45) do |x|
  RUNTIMES.each do |runtime|
    [1, 10].each do |pool_size|
      [1, 10].each do |thread_size|
        logline = printf "%-20s %-10s %-10s %-10s", runtime.name, thread_size, pool_size, RENDERS
        x.report(logline) do
          test_runtime(runtime, pool_size, thread_size, RENDERS)
        end
      end
    end
  end
end
Runtime              Threads    Pool       Renders       user     system      total        real
JavaScriptCore       1          1          100       0.060000   0.140000   3.590000 (  3.621248)
JavaScriptCore       10         1          100       0.060000   0.180000   3.760000 (  3.913241)
JavaScriptCore       1          10         100       0.060000   0.160000   4.100000 (  4.511085)
JavaScriptCore       10         10         100       0.070000   0.230000   6.000000 (  1.366264)
Node.js (V8)         1          1          100       0.050000   0.140000  10.390000 ( 10.602247)
Node.js (V8)         10         1          100       0.070000   0.190000  10.510000 ( 10.632389)
Node.js (V8)         1          10         100       0.050000   0.150000  10.150000 ( 10.297540)
Node.js (V8)         10         10         100       0.060000   0.210000  17.010000 (  3.465977)
therubyrhino (Rhino) 1          1          100       0.980000   0.030000   1.010000 (  0.651000)
therubyrhino (Rhino) 10         1          100       0.860000   0.030000   0.890000 (  0.549000)
therubyrhino (Rhino) 1          10         100       0.710000   0.010000   0.720000 (  0.516000)
therubyrhino (Rhino) 10         10         100       1.920000   0.040000   1.960000 (  0.572000)

# Now bump the renders so we can actually see the effects
therubyrhino (Rhino) 1          1          1000      5.770000   0.040000   5.810000 (  5.122000)
therubyrhino (Rhino) 10         1          1000      6.640000   0.240000   6.880000 (  5.313000)
therubyrhino (Rhino) 1          10         1000      5.190000   0.030000   5.220000 (  5.078000)
therubyrhino (Rhino) 10         10         1000     10.160000   0.080000  10.240000 (  1.717000)