Skip to content
This repository was archived by the owner on Feb 11, 2024. It is now read-only.

Commit 80df427

Browse files
move to package:args for cli arg parsing (#15)
1 parent 0f2a3fd commit 80df427

File tree

10 files changed

+163
-61
lines changed

10 files changed

+163
-61
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1+
## 0.0.3
2+
3+
* Using `package:args` for handling CLI arguments.
4+
* Dependency versions increased.
5+
* Corrected wrong cli command suggestions in case of un-locateable dylibs.
6+
* Fixed throughput benchmark's `RangeError` in case of 0 result.
7+
18
## 0.0.2
9+
210
* Added support for MacOS.
311

412
## 0.0.1+1

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ dart run benchmark/throughput.dart # For parallel requests benchmark.
8787
dart run benchmark/run_all.dart # To run all the benchmarks and get reports.
8888
```
8989

90-
All the benchmarking scripts take test server url as a cli argument. `throughput.dart` and `run_all.dart` also take `N` where `2^N` is the maximum possible parallel requests and the max duration for each run to complete in seconds.
90+
Use `-h` to see available cli arguments and usage informations.
9191

9292
To know how to setup local test servers, read [benchmarking guide](benchmark/benchmarking.md).
9393

benchmark/latency.dart

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:io' as io;
66

7+
import 'package:args/args.dart';
78
import 'package:cronet/cronet.dart';
89

910
abstract class LatencyBenchmark {
@@ -119,11 +120,24 @@ class CronetLatencyBenchmark extends LatencyBenchmark {
119120
}
120121

121122
void main(List<String> args) async {
122-
// Accepts test url as optional cli parameter.
123-
var url = 'https://example.com';
124-
if (args.isNotEmpty) {
125-
url = args[0];
123+
final parser = ArgParser();
124+
parser
125+
..addOption('url',
126+
abbr: 'u',
127+
help: 'The server to ping for running this benchmark.',
128+
defaultsTo: 'https://example.com')
129+
..addFlag('help',
130+
abbr: 'h', negatable: false, help: 'Print this usage information.');
131+
final arguments = parser.parse(args);
132+
if (arguments.wasParsed('help')) {
133+
print(parser.usage);
134+
return;
126135
}
136+
if (arguments.rest.isNotEmpty) {
137+
print(parser.usage);
138+
throw ArgumentError();
139+
}
140+
final url = arguments['url'] as String;
127141
// TODO: https://github.com/google/cronet.dart/issues/11
128142
await CronetLatencyBenchmark.main(url);
129143
// Used as an delemeter while parsing output in run_all script.

benchmark/run_all.dart

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import 'dart:convert';
66
import 'dart:io';
77
import 'dart:math';
88

9+
import 'package:args/args.dart';
10+
911
import 'latency.dart';
1012
import 'throughput.dart';
1113

@@ -24,27 +26,46 @@ List<int> throughputParserHelper(String aotThroughputStdout) {
2426
}
2527

2628
void main(List<String> args) async {
27-
var url = 'https://example.com';
28-
var throughputPrallelLimit = 10;
29-
var duration = const Duration(seconds: 1);
30-
if (args.isNotEmpty) {
31-
url = args[0];
32-
}
33-
if (args.length > 1) {
34-
throughputPrallelLimit = int.parse(args[1]).toInt();
29+
final parser = ArgParser();
30+
parser
31+
..addOption('url',
32+
abbr: 'u',
33+
help: 'The server to ping for running this benchmark.',
34+
defaultsTo: 'https://example.com')
35+
..addOption('limit',
36+
abbr: 'l',
37+
help: 'Limits the maximum number of parallel requests to 2^N where N'
38+
' is provided through this option.',
39+
defaultsTo: '10')
40+
..addOption('time',
41+
abbr: 't',
42+
help: 'Maximum second(s) the benchmark should wait for each request.',
43+
defaultsTo: '1')
44+
..addFlag('help',
45+
abbr: 'h', negatable: false, help: 'Print this usage information.');
46+
final arguments = parser.parse(args);
47+
if (arguments.wasParsed('help')) {
48+
print(parser.usage);
49+
return;
3550
}
36-
if (args.length > 2) {
37-
duration = Duration(seconds: int.parse(args[2]));
51+
if (arguments.rest.isNotEmpty) {
52+
print(parser.usage);
53+
throw ArgumentError();
3854
}
55+
final url = arguments['url'] as String;
56+
final throughputPrallelLimit = int.parse(arguments['limit'] as String);
57+
final duration = Duration(seconds: int.parse(arguments['time'] as String));
58+
3959
print('Latency Test against: $url');
4060
print('JIT');
4161
final jitCronetLatency = await CronetLatencyBenchmark.main(url);
4262
final jitDartIOLatency = await DartIOLatencyBenchmark.main(url);
4363

4464
print('AOT');
4565
print('Compiling...');
46-
Process.runSync('dart', ['compile', 'exe', 'benchmarks/latency.dart']);
47-
final aotLantencyProc = await Process.start('benchmarks/latency.exe', [url]);
66+
Process.runSync('dart', ['compile', 'exe', 'benchmark/latency.dart']);
67+
final aotLantencyProc =
68+
await Process.start('benchmark/latency.exe', ['-u', url]);
4869
stderr.addStream(aotLantencyProc.stderr);
4970
var latencyStdout = '';
5071
await for (final chunk in aotLantencyProc.stdout.transform(utf8.decoder)) {
@@ -67,9 +88,15 @@ void main(List<String> args) async {
6788

6889
print('AOT');
6990
print('Compiling...');
70-
Process.runSync('dart', ['compile', 'exe', 'benchmarks/throughput.dart']);
71-
final aotThroughputProc = await Process.start('benchmarks/throughput.exe',
72-
[url, throughputPrallelLimit.toString(), duration.inSeconds.toString()]);
91+
Process.runSync('dart', ['compile', 'exe', 'benchmark/throughput.dart']);
92+
final aotThroughputProc = await Process.start('benchmark/throughput.exe', [
93+
'-u',
94+
url,
95+
'-l',
96+
throughputPrallelLimit.toString(),
97+
'-t',
98+
duration.inSeconds.toString()
99+
]);
73100
stderr.addStream(aotThroughputProc.stderr);
74101
var throughputStdout = '';
75102
await for (final chunk in aotThroughputProc.stdout.transform(utf8.decoder)) {
@@ -90,7 +117,7 @@ void main(List<String> args) async {
90117
' ${jitDartIOLatency.toStringAsFixed(3)} ms |');
91118
print('| AOT | ${aotCronetLatency.toStringAsFixed(3)} ms |'
92119
' ${aotDartIOLatency.toStringAsFixed(3)} ms |');
93-
print('\n\nThroughput Test Results (Duration ${duration.inSeconds}');
120+
print('\nThroughput Test Results (Duration: ${duration.inSeconds}s)');
94121
print('| Mode | package:cronet | dart:io |');
95122
print('| :-----------: |:--------------: | :-----------: |');
96123
print('| JIT | ${jitCronetThroughput[1]} out of'

benchmark/throughput.dart

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:async';
66
import 'dart:io' as io;
77
import 'dart:math';
88

9+
import 'package:args/args.dart';
910
import 'package:cronet/cronet.dart';
1011

1112
abstract class ThroughputBenchmark {
@@ -56,7 +57,7 @@ abstract class ThroughputBenchmark {
5657

5758
Future<List<int>> report() async {
5859
var maxReturn = 0;
59-
var throughput = List<int>.empty();
60+
var throughput = [0, 0];
6061
// Run the benchmark for 1, 2, 4...spawnThreshold.
6162
for (int currentThreshold = 1;
6263
currentThreshold <= spawnThreshold;
@@ -151,19 +152,36 @@ class CronetThroughputBenchmark extends ThroughputBenchmark {
151152
}
152153

153154
void main(List<String> args) async {
154-
// Accepts test url & parallel request threshold as optional cli parameter.
155-
var url = 'https://example.com';
156-
var spawnThreshold = 1024;
157-
var duration = const Duration(seconds: 1);
158-
if (args.isNotEmpty) {
159-
url = args[0];
160-
}
161-
if (args.length > 1) {
162-
spawnThreshold = pow(2, int.parse(args[1])).toInt();
163-
}
164-
if (args.length > 2) {
165-
duration = Duration(seconds: int.parse(args[2]));
166-
}
155+
final parser = ArgParser();
156+
parser
157+
..addOption('url',
158+
abbr: 'u',
159+
help: 'The server to ping for running this benchmark.',
160+
defaultsTo: 'https://example.com')
161+
..addOption('limit',
162+
abbr: 'l',
163+
help: 'Limits the maximum number of parallel requests to 2^N where N'
164+
' is provided through this option.',
165+
defaultsTo: '10')
166+
..addOption('time',
167+
abbr: 't',
168+
help: 'Maximum second(s) the benchmark should wait for each request.',
169+
defaultsTo: '1')
170+
..addFlag('help',
171+
abbr: 'h', negatable: false, help: 'Print this usage information.');
172+
final arguments = parser.parse(args);
173+
if (arguments.wasParsed('help')) {
174+
print(parser.usage);
175+
return;
176+
}
177+
if (arguments.rest.isNotEmpty) {
178+
print(parser.usage);
179+
throw ArgumentError();
180+
}
181+
final url = arguments['url'] as String;
182+
final spawnThreshold =
183+
pow(2, int.parse(arguments['limit'] as String)).toInt();
184+
final duration = Duration(seconds: int.parse(arguments['time'] as String));
167185
// TODO: https://github.com/google/cronet.dart/issues/11
168186
await CronetThroughputBenchmark.main(url, spawnThreshold, duration);
169187
// Used as an delemeter while parsing output in run_all script.

bin/setup.dart

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:io';
66

77
import 'package:archive/archive.dart';
8+
import 'package:args/command_runner.dart';
89
import 'package:cli_util/cli_logging.dart' show Ansi, Logger;
910
import 'package:cronet/src/constants.dart';
1011
import 'package:cronet/src/third_party/ffigen/find_resource.dart';
@@ -183,32 +184,61 @@ void verifyCronetBinary() {
183184
sample.deleteSync();
184185
}
185186

186-
Future<void> main(List<String> args) async {
187-
const docStr = """
188-
dart run cronet:setup [option]
189-
Downloads the cronet binaries.\n
190-
clean\tClean downloaded or built binaries.
191-
build\tBuilds the wrapper. Requires cmake.
192-
verify\tVerifies the cronet binary.
193-
""";
194-
final logger = Logger.standard();
195-
if (args.length > 1) {
196-
logger.stderr('Expected 1 argument only.');
197-
logger.stdout(docStr);
198-
} else if (args.contains('-h')) {
199-
logger.stdout(docStr);
200-
} else if (args.contains('clean')) {
201-
logger.stdout('cleaning...');
202-
Directory(binaryStorageDir).deleteSync(recursive: true);
203-
logger.stdout('Done!');
204-
} else if (args.contains('build')) {
187+
// Available Commands.
188+
189+
class BuildCommand extends Command<void> {
190+
@override
191+
String get description => 'Builds the wrapper binaries. Requires cmake.';
192+
193+
@override
194+
String get name => 'build';
195+
196+
@override
197+
void run() {
205198
buildWrapper();
206-
} else if (args.contains('verify')) {
199+
}
200+
}
201+
202+
class CleanCommand extends Command<void> {
203+
@override
204+
String get description => 'Cleans downloaded or built binaries.';
205+
206+
@override
207+
String get name => 'clean';
208+
209+
@override
210+
void run() {
211+
print('cleaning...');
212+
Directory(binaryStorageDir).deleteSync(recursive: true);
213+
}
214+
}
215+
216+
class VerifyCommand extends Command<void> {
217+
@override
218+
String get description => 'Verifies the cronet binary.';
219+
220+
@override
221+
String get name => 'verify';
222+
223+
@override
224+
void run() {
207225
verifyCronetBinary();
208-
} else {
226+
}
227+
}
228+
229+
Future<void> main(List<String> args) async {
230+
final runner =
231+
CommandRunner<void>('setup', 'Downloads/Builds the cronet binaries.');
232+
runner
233+
..addCommand(BuildCommand())
234+
..addCommand(CleanCommand())
235+
..addCommand(VerifyCommand());
236+
if (args.isEmpty) {
209237
// Targeting only 64bit OS. (At least for the time being.)
210238
if (validPlatforms.contains('${Platform.operatingSystem}64')) {
211239
await downloadCronetBinaries('${Platform.operatingSystem}64');
212240
}
241+
} else {
242+
await runner.run(args);
213243
}
214244
}

example/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Examples
2+
3+
- [Bare Minimum](https://github.com/google/cronet.dart#example)
4+
- [Simple (Dart CLI)](https://github.com/google/cronet.dart/tree/main/example/)

lib/src/dylib_handler.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ DynamicLibrary loadDylib(String name) {
133133
logger.stdout(
134134
'To download the binaries, please run the following from the root of'
135135
' your project:');
136-
logger.stdout('${ansi.yellow}dart run cronet <platform>${ansi.none}');
136+
logger.stdout('${ansi.yellow}dart run cronet:setup${ansi.none}');
137137
logger.stdout('${ansi.green}Valid platforms are:');
138138
for (final platform in validPlatforms) {
139139
logger.stdout(platform);

pubspec.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@
33
# BSD-style license that can be found in the LICENSE file.
44

55
name: cronet
6-
version: 0.0.2
6+
version: 0.0.3
77
homepage: https://github.com/google/cronet.dart
88
description: Experimental Cronet dart bindings.
99

1010
environment:
1111
sdk: '>=2.12.0 <3.0.0'
1212

1313
dependencies:
14-
ffi: ^1.0.0
14+
ffi: ^1.1.2
1515
path: ^1.8.0
16-
cli_util: ^0.3.0
16+
args: ^2.1.1
17+
cli_util: ^0.3.3
1718
archive: ^3.1.2
1819

1920
dev_dependencies:

tool/update_bindings.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import 'dart:io';
66

7-
void main(List<String> args) async {
7+
void main() async {
88
final root =
99
Directory.fromUri(Platform.script).parent.parent.uri.toFilePath();
1010

0 commit comments

Comments
 (0)