Optimize `AsyncMessageGrouper`. (#54)

Add a benchmark for a large (~600k) request.

Before this change, grouping the message takes around 14ms. After this change it takes around 1ms.

Deserializing is not affected, but is benchmarked too as it is always needed and so is useful to compare. It takes around 2ms.
4 files changed
tree: 20bf3bc2dfffab24c6e5c50ada9930297b3ee866
  1. .github/
  2. benchmark/
  3. e2e_test/
  4. lib/
  5. test/
  6. tool/
  7. .gitignore
  8. analysis_options.yaml
  9. AUTHORS
  10. CHANGELOG.md
  11. CONTRIBUTING.md
  12. LICENSE
  13. pubspec.yaml
  14. README.md
README.md

Tools for creating a persistent worker loop for bazel.

Usage

There are two abstract classes provided by this package, AsyncWorkerLoop and SyncWorkerLoop. These each have a performRequest method which you must implement.

Lets look at a simple example of a SyncWorkerLoop implementation:

import 'dart:io';
import 'package:bazel_worker/bazel_worker.dart';

void main() {
  // Blocks until it gets an EOF from stdin.
  new SyncSimpleWorker().run();
}

class SyncSimpleWorker extends SyncWorkerLoop {
  /// Must synchronously return a [WorkResponse], since this is a
  /// [SyncWorkerLoop].
  WorkResponse performRequest(WorkRequest request) {
    new File('hello.txt').writeAsStringSync('hello world!');
    return new WorkResponse()..exitCode = EXIT_CODE_OK;
  }
}

And now the same thing, implemented as an AsyncWorkerLoop:

import 'dart:io';
import 'package:bazel_worker/bazel_worker.dart';

void main() {
  // Doesn't block, runs tasks async as they are received on stdin.
  new AsyncSimpleWorker().run();
}

class AsyncSimpleWorker extends AsyncWorkerLoop {
  /// Must return a [Future<WorkResponse>], since this is an
  /// [AsyncWorkerLoop].
  Future<WorkResponse> performRequest(WorkRequest request) async {
    await new File('hello.txt').writeAsString('hello world!');
    return new WorkResponse()..exitCode = EXIT_CODE_OK;
  }
}

As you can see, these are nearly identical, it mostly comes down to the constraints on your package and personal preference which one you choose to implement.

Testing

A package:bazel_worker/testing.dart file is also provided, which can greatly assist with writing unit tests for your worker. See the test/worker_loop_test.dart test included in this package for an example of how the helpers can be used.

Features and bugs

Please file feature requests and bugs at the issue tracker.