grpc / grpc-dart

The Dart language implementation of gRPC.
https://pub.dev/packages/grpc
Apache License 2.0
861 stars 271 forks source link

Latency in the hello_world sample #463

Closed kakyoism closed 3 years ago

kakyoism commented 3 years ago
The dart version of the official hello_world example shows a slower response than the Python version. ```yaml # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: archive: dependency: transitive description: name: archive url: "https://pub.flutter-io.cn" source: hosted version: "3.1.2" async: dependency: "direct main" description: name: async url: "https://pub.flutter-io.cn" source: hosted version: "2.5.0" charcode: dependency: transitive description: name: charcode url: "https://pub.flutter-io.cn" source: hosted version: "1.2.0" collection: dependency: transitive description: name: collection url: "https://pub.flutter-io.cn" source: hosted version: "1.15.0" crypto: dependency: transitive description: name: crypto url: "https://pub.flutter-io.cn" source: hosted version: "3.0.0" fixnum: dependency: transitive description: name: fixnum url: "https://pub.flutter-io.cn" source: hosted version: "1.0.0" googleapis_auth: dependency: transitive description: name: googleapis_auth url: "https://pub.flutter-io.cn" source: hosted version: "1.1.0" grpc: dependency: "direct main" description: path: "../.." relative: true source: path version: "3.0.1-dev" http: dependency: transitive description: name: http url: "https://pub.flutter-io.cn" source: hosted version: "0.13.1" http2: dependency: transitive description: name: http2 url: "https://pub.flutter-io.cn" source: hosted version: "2.0.0" http_parser: dependency: transitive description: name: http_parser url: "https://pub.flutter-io.cn" source: hosted version: "4.0.0" meta: dependency: transitive description: name: meta url: "https://pub.flutter-io.cn" source: hosted version: "1.3.0" path: dependency: transitive description: name: path url: "https://pub.flutter-io.cn" source: hosted version: "1.8.0" pedantic: dependency: transitive description: name: pedantic url: "https://pub.flutter-io.cn" source: hosted version: "1.11.0" protobuf: dependency: "direct main" description: name: protobuf url: "https://pub.flutter-io.cn" source: hosted version: "2.0.0" source_span: dependency: transitive description: name: source_span url: "https://pub.flutter-io.cn" source: hosted version: "1.8.1" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.flutter-io.cn" source: hosted version: "1.1.0" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.flutter-io.cn" source: hosted version: "1.2.0" typed_data: dependency: transitive description: name: typed_data url: "https://pub.flutter-io.cn" source: hosted version: "1.3.0" sdks: dart: ">=2.12.0 <3.0.0" ``` ## Repro steps 1. Run the official dart example of hello_world according to[ the instruction](https://grpc.io/docs/languages/dart/quickstart/) 1. Observe how soon the response shows up in Terminal 1. Switch the server to the official Python server code, keep using Dart client code, and try again the above 2. Use Python hello_world server/client pair, observe the same repro, and compare 3. Use Dart server and Python client, observe the same repro, and compare Expected result: The response in the case of a Dart client should be about the same speed as the Python version of the hello_world client/server pair. Actual result: Dart version takes >1 second to show the response while the Python client did it almost instantly in all the cases. ## Details - macOS 10.15.6 - Python 3.9.0 (v3.9.0:9cf6752276, Oct 5 2020, 11:29:23) - Dart SDK version: 2.12.2 (stable) (Wed Mar 17 10:30:20 2021 +0100) on "macos_x64"
mraleph commented 3 years ago

What you observe is Dart startup cost (which unfortunately has been on a slower side since Dart 2.0 made significant changes both to the type-system and how we run Dart code). You can do the following, to confirm:

$ time dart bin/client.dart
Greeter client received: Hello, world!
        1.94 real         2.05 user         0.25 sys
$ dart --snapshot=bin/client.dart.snapshot bin/client.dart
Info: Compiling without sound null safety
$ time dart bin/client.dart.snapshot
Greeter client received: Hello, world!
        0.26 real         0.26 user         0.04 sys
mraleph commented 3 years ago

@kakyoism yes to both of your questions from the deleted comment.

What you are observing is a one time program compilation cost. Snapshotting can address that if startup matters.

You can also observe some warmup latency, you can further mitigate that by compiling your server with AOT compiler (dart2native) instead of running it in JIT mode.

kakyoism commented 3 years ago

@kakyoism yes to both of your questions from the deleted comment.

What you are observing is a one time program compilation cost. Snapshotting can address that if startup matters.

You can also observe some warmup latency, you can further mitigate that by compiling your server with AOT compiler (dart2native) instead of running it in JIT mode.

Thank you so much. Compiling to aot does the job for me.

dartaotruntime client.aot

runs blazingly fast!