jknack / handlebars.java

Logic-less and semantic Mustache templates with Java
http://jknack.github.io/handlebars.java
Other
1.48k stars 383 forks source link

Including partials is slow? #612

Open mach-kernel opened 6 years ago

mach-kernel commented 6 years ago

I'm using this library as it is wrapped in vertx's templating adapters, source repo here. I have a very simple template (shown below) that just includes another partial (~27k in size) in order to render a header. I did a small benchmark using ab -n1000 -c25 http://localhost:9090/ with one HTTP server instance using this library to render. Having to use the {{ > ... }} is much slower than inlining it (~40 rps vs ~1k rps). I realize that caching mostly solves this issue but the discrepancy seems very large to me.

I'm happy to explore solutions and go digging for how to optimize this. Thanks!

Template

<html>
<head>
</head>

{{> header }}

</html>
Include Directive
This is ApacheBench, Version 2.3 <$Revision: 1807734 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests

Server Software:
Server Hostname:        localhost
Server Port:            9090

Document Path:          /
Document Length:        27355 bytes

Concurrency Level:      25
Time taken for tests:   26.094 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      27422000 bytes
HTML transferred:       27355000 bytes
Requests per second:    38.32 [#/sec] (mean)
Time per request:       652.346 [ms] (mean)
Time per request:       26.094 [ms] (mean, across all concurrent requests)
Transfer rate:          1026.27 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.4      0       4
Processing:    42  644  57.7    644     785
Waiting:       42  644  57.8    644     785
Total:         46  644  57.5    645     786

Percentage of the requests served within a certain time (ms)
  50%    645
  66%    649
  75%    653
  80%    657
  90%    678
  95%    689
  98%    761
  99%    770
 100%    786 (longest request)
No include directive
This is ApacheBench, Version 2.3 <$Revision: 1807734 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests

Server Software:
Server Hostname:        localhost
Server Port:            9090

Document Path:          /
Document Length:        48927 bytes

Concurrency Level:      25
Time taken for tests:   1.088 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      48994000 bytes
HTML transferred:       48927000 bytes
Requests per second:    918.78 [#/sec] (mean)
Time per request:       27.210 [ms] (mean)
Time per request:       1.088 [ms] (mean, across all concurrent requests)
Transfer rate:          43959.71 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.5      0       4
Processing:     6   27  74.7     13     502
Waiting:        6   27  74.7     13     502
Total:          7   27  75.1     13     503

Percentage of the requests served within a certain time (ms)
  50%     13
  66%     16
  75%     18
  80%     19
  90%     23
  95%     32
  98%    487
  99%    499
 100%    503 (longest request)

JProfiler

image

bechte commented 6 years ago

In: https://github.com/jknack/handlebars.java/blob/1f6c48e606dc1303d1e92a0a0eaa94120eba64fd/handlebars/src/main/java/com/github/jknack/handlebars/internal/Text.java#L93

I think it's worth considering to not always create a new char array that is just of the length of the required size, but to expand the capacity to a multiple of 2 (see implementation of StringBuilder for details) and, therefore, save a lot of calls to System.arraycopy. Especially, as append gets invoked very often with shorter Strings. Further more I would suggest using a StringBuilder internally that handles all the low level details... 😃

jknack commented 6 years ago

please a pull request and will look.

thanks

mach-kernel commented 6 years ago

I'm working on it in this branch, will make a PR when everything is ready + I want to write a test for the new char vector.

jcputney commented 3 years ago

@mach-kernel did anything ever get pulled in? We're seeing slow speeds on partials as well, just wondering if there was a fix.

bechte commented 3 years ago

Hi, I have made an easy version using StringBuilder here: https://github.com/jknack/handlebars.java/pull/831 Needs a final test, but might help to move forward here.