Hi Zach, et al
Well, I amended the code as below (based on your instructions):
#!/usr/bin/dart
import 'dart:convert';
import 'dart:io';
import 'dart:isolate';
const int _PORT = 9292;
const String _HOST = '0.0.0.0';
const String _GREET = 'Hello World';
final ContentType _CONTENT_TYPE = new ContentType('text', 'plain');
_startServer(arg) {
final List<int> response = UTF8.encode(_GREET);
HttpServer.bind(_HOST, _PORT, shared: true).then((server) {
server.listen((HttpRequest request) {
request.response
..headers.contentType = _CONTENT_TYPE
..add(response)
..close();
});
});
}
void main() {
final int _CORES = Platform.numberOfProcessors ~/ 2;
print("Cores: $_CORES");
for (int i = 0; i < _CORES - 1; ++i) {
Isolate.spawn(_startServer, null);
}
_startServer(null);
}
and ran an ab test on it, executing:
*ab -n 786432 -c 128 -k http://localhost:9292/ <http://localhost:9292/>*
Results obtained (3 runs):
- Requests per second: 33469.39 [#/sec] (mean)
- Requests per second: 35338.14 [#/sec] (mean)
- Requests per second: 34385.53 [#/sec] (mean)
Then I built this C++ equivalent (using pistache: http://pistache.io/),
code:
#include "pistache/endpoint.h"
using namespace Pistache;
class HelloHandler : public Http::Handler {
public:
HTTP_PROTOTYPE(HelloHandler)
void onRequest(const Http::Request& request, Http::ResponseWriter response)
{
response.send(Http::Code::Ok, "Hello, World");
}
};
int main() {
Address addr(Ipv4::any(), Port(9080));
auto opts = Http::Endpoint::options().threads(4);
Http::Endpoint server(addr);
server.init(opts);
server.setHandler(std::make_shared<HelloHandler>());
server.serve();
}
Compiled the code with:
*g++ -pthread -s -o x -Ofast hw.cpp -lpistache *
and ran ab test on it, executing:
*ab -n 786432 -c 128 -k http://localhost:9080/ <http://localhost:9080/>*
*Results obtained (3 runs):*
- Requests per second: 253391.44 [#/sec] (mean)
- Requests per second: 252958.08 [#/sec] (mean)
- Requests per second: 251081.52 [#/sec] (mean)
I ran these tests on an Ubuntu Linux 17.04 64-bit machine with 32GB RAM
having Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz (4 cores with
hyper-threading).
As you can see, the best I could get with JIT-ed Dart was about 35K
requests per second; using C++, I was getting an average of about 251K
requests per second. So basically, C++ is about 7 times faster than JIT-ed
Dart! This is miles away from the 'near the level of C++' mentioned. What
could I be doing wrong? Why can't I get anywhere close to the level of C++?
- Adrian.
On Mon, Mar 12, 2018 at 5:34 PM, 'Zach Anderson' via Dart Misc <
Post by 'Zach Anderson' via Dart MiscHttpResponse.write() does a conversion to utf8 and one or more copies
and/or allocations, which are probably not too great for benchmarking.
The benchmark here: https://github.com/costajob/app-servers/blob/
master/servers/dart_server.dart seems more apples-to-apples to what
you're doing with C++, but you'd probably also want to pre-allocate the
ContentType object in that benchmark instead of allocating a new one for
every request.
Cheers,
Zach
Post by Adrian MerciecaHi Matan,
Thanks for your answer (and sorry for my delay in answering).
Well, I have experienced Java improved performance of server apps over
time (after considerable warm-up), so I can correlate with your view.
But on the other hand, I have seen C++ REST frameworks delivering 320K
requests per second (doing the thing above - nothing complicated, just
returning 'Hello World' just as my Dart code quoted was doing). The best I
could manage with Dart (JIT-ed) on my new (same spec) machine was about 55K
requests per second. Why would Dart be about 4 times slower than C++ even
after considerable warm-up running in JIT mode?
Regards.
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to a topic in the
Google Groups "Dart Misc" group.
To unsubscribe from this topic, visit https://groups.google.com/a/
dartlang.org/d/topic/misc/BCfmQ6WTfNU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.