You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Performance] Look into TechEmpower Benchmarks slowness #273
While the performance isn't bad in the latest benchmarks, it should be 10 times better. Our internal testing shows at least 1 million requests per second on a smaller 32core system where the 80 core system that the benchmark uses only had 100k requests per second.
I was running some benchmarks on my machine and could not get amber to deliver better performance than roda until I removed the logging middleware. The logging of every route was slowing it down considerably.
Just FYI.
BTW I would suggest you update your docs to show a sample of how to return json in routes. I figured out by looking at the kemal docs but it wouldn't hurt to throw a paragraph in there.
PS: Please note that i'm running these benchmarks on a quite old Macbook Pro with very decent specs (which is probably also running multiple other services in the background)
Amber::Server.configure do |app|
routes :webdo
get "/plaintext", BenchmarkController, :plaintext
get "/json", BenchmarkController, :json
get "/db", BenchmarkController, :db
get "/queries", BenchmarkController, :queries
get "/updates", BenchmarkController, :updates
get "/fortunes", BenchmarkController, :fortunesendend
I think we can compare crystal-raw vs amber side by side and see what is going on. Maybe the db wrapper or some static controller in some place is between plaintext controller and the browser request.
I believe that crystal-raw got less broken pipe errors.
Isaac Sloan - isaacsloan.com
Artisan Rails Architect
1391 S Slate Canyon Dr.
Provo, Ut 84606
Cell: (435)-669-5005
@elorest right, right. We are seeing the same errors, just less of them in crystal-raw. Amber is (obviously) doing more than raw does in each spawn i.e. allocating a new controller per request, calling before and after filters, rendering templates. My guess is we are hitting some resource limit faster than crystal-raw.
I'm not sure if this error is what is causing the slowness though. It's possible that hitting the error creates some sort of delay, but I'm not convinced this is it yet.
I was thinking that we can do some caching of the controller class this will save the initialization and allocation of the class for subsequent requests. The idea is simple, basically keep controller classes in memory and just reset the context which will set everything else in the controller.
If we can verify any sort of speed increase then I'm in. This is what we
had originally though and it was frustrating because everything was
potentially nil.
Isaac Sloan - isaacsloan.com
Artisan Rails Architect
1391 S Slate Canyon Dr.
Provo, Ut 84606
Cell: (435)-669-5005
I'm thinking in something, Currently the GC is multithreaded, so, every process is launching GC threads even when 100% of 80 cores are bussy.
From gitter/IRC:
its so interesting how much faster it is
22.5 vs 18s
well, on my laptop now switch.cr is 41 seconds, tail-call.cr is 38 seconds
thats like a 20% improvement
and your processor has a 100Mhz advantage on turbo frequency
those small ipc improvements really add up
btw, on my laptop both versions seems to jump from core to core all the time
sure
but that doesn't happen for e.g. C versions
why?
the GC is multithreaded
ah
yeah, i had to use GOMAXPROC=1 or something to get good results from golang versions
oh wow
jsn-, try GC_MARKERS=1
do crystal have something like that, btw?
ah, ok
and try GC_NPROCS=1
you'll probablyt find it slower
i mean try them seperately btw jsn-
itll be interesting to see which is faster
Perhaps, Kemal & Amber are getting in conflict with multithreaded-GC so the results are lower than crystal-raw
Maybe we can set some variables on setup.sh here, here & here
#!/bin/bash
fw_depends postgresql crystal
shards install
export GC_MARKERS=1
export GC_NPROCS=1
crystal build --release --no-debug src/amber.cr
for i in $(seq 1 $(nproc --all)); do
# ./amber &
# ./kemal &
done
wait
My guess is we are hitting some resource limit faster than crystal-raw
Maybe crystal-raw is getting such high RPS on plaintext test because doesn't have problems with GC.
Meanwhile Kemal & Amber are using some layers of abstraction, classes and modules that can be calling the multithreaded-GC earlier, so just imagine 80 processes launching multiples threads across cores competing for CPU time, seems insane.
@robacarp I agree, although I think we should also focus on performance as we write code. Sometimes simply using one method for string manipulation over another can make a huge difference to speed over a few thousand requests. Unfortunately this benchmark ends up being way slower for Amber than our own benchmarks (server to server) even on far weaker boxes. We've tried multiple things and wasted 40+ hours, trying to get the configuration right so that the benchmark will work well with amber. Because of this I think we should temporarily withdraw until we have time to focus solely on that.
Although I don't generally put too much stock in benchmarks, I can't help but notice that on the TechEmpower framework benchmarks, Crystal's performance is very high relative to the competition except when a database query is involved.
For instance, on the JSON Serialization, and Plaintext tests which do not require a database connection, Crystal's highest implementation marks 88.4% and 79.6% respectively relative to the highest performers. However the four tests involving a database, Single Query, Multiple Queries, Fortunes and Data Updates, Crystal marks 40.3%, 36.9%, 45.9% and 17.8% respectively.
In particular, the Data Updates performance seems low, with several Ruby implementations out performing the Crystal implementations. This leads me to believe their must be some low hanging fruit to improve the performance of database connections in general.
Is anyone aware of optimizations other languages/frameworks are using to improve the database access?
Dart in particular seems to be benefitting from fast database access as it does poorly on JSON serialization and Plaintext but is extremely competitive on the Multiple Queries and Data Updates tests.
The Fortunes test requirements state: "Use of an in-memory cache of Fortune objects or rows by the application is not permitted."
Does this preclude low-level database driver optimizations such as server protocol request de-duplication? I.e. If two Fortunes queries arrive at the database driver at approximately the same time, the driver could choose to send a single query to the database and then serve both clients from a single server response....
...Except where noted, all database queries should be delivered to the database servers as-is and not coalesced or deduplicated at the database driver. In all cases where a database query is required, it is expected that the query will reach and execute on the database server—that is, the full round-trip effort of a query will be part of the workload.
Currently the best explanation I have is that the database connection pools are properly sized (well, it is still a work in progress). Otherwise the implementation uses pretty standard techniques:
fully asynchronous database commands
the program never blocks unless absolutely necessary
efficient load balancing among the worker threads with SO_REUSEPORT
this is especially important for thread-local database connection pools (more on that later), so that one never ends up in the situation in which one pool is fully saturated, while another one is idle
prepared statements
created once when a database connection is established
minimizing the number of system calls
libh2o uses writev(), so all fortunes are sent with one system call
avoiding memory allocations and copies as much as possible
extensive usage of the memory pool functionality provided by libh2o
every HTTP request has an associated memory pool, and all allocations belonging to the pool are freed together in one go after the request is completed; note that it is not possible to override the memory management routines used by the database client library, libpq, for example, so not every allocation is a pooled one.
@faustinoaq this is great news and I'm interested to find out what changed in the Benchmarks that finally fixed this. @elorest and I knew that our local testing showed way better results than earlier rounds but we couldn't pinpoint why.
Activity
faustinoaq commentedon Sep 20, 2017
TechEmpower issue here 👉 TechEmpower/FrameworkBenchmarks#2960
timuckun commentedon Oct 3, 2017
I was running some benchmarks on my machine and could not get amber to deliver better performance than roda until I removed the logging middleware. The logging of every route was slowing it down considerably.
Just FYI.
BTW I would suggest you update your docs to show a sample of how to return json in routes. I figured out by looking at the kemal docs but it wouldn't hurt to throw a paragraph in there.
jasdeepsingh commentedon Oct 3, 2017
I can assert this finding by @timuckun Here's a before and an after:
With the logging middleware:
Without logging middleware:
PS: Please note that i'm running these benchmarks on a quite old Macbook Pro with very decent specs (which is probably also running multiple other services in the background)
jasdeepsingh commentedon Oct 3, 2017
Finally a run with logging disabled & release optimizations:
faustinoaq commentedon Oct 6, 2017
@jasdeepsingh and @timuckun We are using a minimal middleware on TFB:
No even pipelines here frameworks/Crystal/amber/config/routes.cr
I still think the big slowdown is on Broken pipe errors shown here
The only difference between crystal-raw logs and amber logs are those errors.
I think we can compare crystal-raw vs amber side by side and see what is going on. Maybe the db wrapper or some static controller in some place is between plaintext controller and the browser request.
faustinoaq commentedon Oct 6, 2017
Also, if we find the solution to this, it would help to @sdogruyol to optimize Kemal too 😄
drujensen commentedon Oct 6, 2017
@faustinoaq I agree but I don't think there is any difference between crystal-raw and amber. We ran both and they both get the same errors.
faustinoaq commentedon Oct 6, 2017
@drujensen Oh, if
crystal-rawis getting the same error as amber, then multiple machines will be the clue, Did you Try to run TFB on 3 machine setup ?drujensen commentedon Oct 6, 2017
@faustinoaq yes, but I don't have access to the same sized servers. At this point I can't duplicate the issue so I have no way of resolving it.
elorest commentedon Oct 6, 2017
drujensen commentedon Oct 6, 2017
@elorest right, right. We are seeing the same errors, just less of them in crystal-raw. Amber is (obviously) doing more than raw does in each spawn i.e. allocating a new controller per request, calling before and after filters, rendering templates. My guess is we are hitting some resource limit faster than crystal-raw.
I'm not sure if this error is what is causing the slowness though. It's possible that hitting the error creates some sort of delay, but I'm not convinced this is it yet.
eliasjpr commentedon Oct 12, 2017
I was thinking that we can do some caching of the controller class this will save the initialization and allocation of the class for subsequent requests. The idea is simple, basically keep controller classes in memory and just reset the context which will set everything else in the controller.
elorest commentedon Oct 12, 2017
eliasjpr commentedon Oct 12, 2017
Yes, maybe you can come up with a proof of concept I know you love the speed 🚤 :)
faustinoaq commentedon Oct 20, 2017
I'm thinking in something, Currently the GC is multithreaded, so, every process is launching GC threads even when 100% of 80 cores are bussy.
From gitter/IRC:
Perhaps, Kemal & Amber are getting in conflict with multithreaded-GC so the results are lower than
crystal-rawMaybe we can set some variables on setup.sh here, here & here
Maybe
crystal-rawis getting such high RPS on plaintext test because doesn't have problems with GC.Meanwhile Kemal & Amber are using some layers of abstraction, classes and modules that can be calling the multithreaded-GC earlier, so just imagine 80 processes launching multiples threads across cores competing for CPU time, seems insane.
WDYT @RX14 @sdogruyol ?
17 remaining items
elorest commentedon Feb 22, 2018
@robacarp I agree, although I think we should also focus on performance as we write code. Sometimes simply using one method for string manipulation over another can make a huge difference to speed over a few thousand requests. Unfortunately this benchmark ends up being way slower for Amber than our own benchmarks (server to server) even on far weaker boxes. We've tried multiple things and wasted 40+ hours, trying to get the configuration right so that the benchmark will work well with amber. Because of this I think we should temporarily withdraw until we have time to focus solely on that.
faustinoaq commentedon Mar 10, 2018
Hi, relevant issue here: crystal-lang/crystal-db#77
faustinoaq commentedon Mar 15, 2018
Interesting comments:
crystal-lang/crystal-db#81 (comment)
faustinoaq commentedon Mar 18, 2018
Hey I found this: crystal-lang/crystal#5610 (Too many heap sections)
eliasjpr commentedon Mar 18, 2018
Not sure what that means in the context of the tests. Can you elaborate please?
faustinoaq commentedon Mar 18, 2018
Well, TFB is a high load environment, Maybe amber isn't using all memory available because Too many heap section are allocated, WDYT?
crystal-lang/crystal#5610 (comment)
TFB has a more RAM than 8.6GB
RX14 commentedon Mar 18, 2018
Did we get the error in the logs? no
I haven't a clue why this is being discussed.
faustinoaq commentedon Mar 24, 2018
^ Hey a small update about DB tests
https://groups.google.com/forum/#!topic/framework-benchmarks/za-Z4MfUUlQ
faustinoaq commentedon May 16, 2018
Oh, yeah! Now looks like Amber is as fast as crystal on TFB 🎉 😄
Ref: Results
Ref: Citrine Build
faustinoaq commentedon May 23, 2018
Amber is fast, although Amber is still slow compared with "things" like h2o or ulib 😅
A very interesting comment here: TechEmpower/FrameworkBenchmarks#3751 (comment)
efficient load balancing among the worker threads with
SO_REUSEPORTprepared statements
minimizing the number of system calls
writev(), so all fortunes are sent with one system callavoiding memory allocations and copies as much as possible
extensive usage of the memory pool functionality provided by libh2o
memcmp()as the string comparison functionRef: TechEmpower/FrameworkBenchmarks#3751
drujensen commentedon May 27, 2018
@faustinoaq this is great news and I'm interested to find out what changed in the Benchmarks that finally fixed this. @elorest and I knew that our local testing showed way better results than earlier rounds but we couldn't pinpoint why.
I think we can finally close this issue.
RX14 commentedon May 27, 2018
I think it's just the change in hardware that's used in benchmarking?