Skip to content

Instantly share code, notes, and snippets.

@shuhei
Last active April 17, 2016 14:44
Show Gist options
  • Save shuhei/d3e022e31ec97b21411e910cfb060253 to your computer and use it in GitHub Desktop.
Save shuhei/d3e022e31ec97b21411e910cfb060253 to your computer and use it in GitHub Desktop.
Faster partial rendering with Jbuilder

Before:

Running 10s test @ http://localhost:3000/tests/to_json.json
  1 threads and 1 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    73.59ms    9.26ms 125.82ms   79.41%
    Req/Sec    13.19      4.94    20.00     60.42%
  136 requests in 10.06s, 29.14MB read
Requests/sec:     13.52
Transfer/sec:      2.90MB

Running 10s test @ http://localhost:3000/tests/as_json.json
  1 threads and 1 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    95.73ms    9.90ms 135.35ms   81.90%
    Req/Sec    10.11      3.19    20.00     88.42%
  105 requests in 10.08s, 22.49MB read
Requests/sec:     10.42
Transfer/sec:      2.23MB

Running 10s test @ http://localhost:3000/tests/inline.json
  1 threads and 1 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    82.25ms   10.29ms 159.58ms   92.56%
    Req/Sec    11.71      4.35    20.00     74.23%
  121 requests in 10.05s, 25.92MB read
Requests/sec:     12.05
Transfer/sec:      2.58MB

Running 10s test @ http://localhost:3000/tests/nested.json
  1 threads and 1 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   454.90ms   12.84ms 471.92ms   66.67%
    Req/Sec     1.57      0.51     2.00     57.14%
  21 requests in 10.00s, 5.22MB read
Requests/sec:      2.10
Transfer/sec:    534.48KB

After:

Running 10s test @ http://localhost:3000/tests/nested.json
  1 threads and 1 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   111.49ms   13.19ms 200.09ms   87.78%
    Req/Sec     8.86      1.59    10.00     91.11%
  90 requests in 10.06s, 19.28MB read
Requests/sec:      8.95
Transfer/sec:      1.92MB
class JbuilderTemplate
private
def _render_partial(options)
options[:locals].merge! json: self
@template_cache ||= {}
path = options[:partial]
template = @template_cache[path]
if template.nil?
prefixes = path.include?(?/) ? [] : @context.lookup_context.prefixes
opts = options.reject { |k, v| [:partial, :locals].include?(k) }
args = [options[:partial], prefixes, true, options[:locals].keys, opts]
template = @context.lookup_context.find_template(*args)
@template_cache[path] = template
template.render(@context, options[:locals])
else
@context.send(template.send(:method_name), options[:locals], nil)
end
end
end if defined?(JbuilderTemplate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment