From 68454568a102087b46a13fa4bff6771a30adc4b6 Mon Sep 17 00:00:00 2001 From: Tien Do Nam Date: Sat, 2 Nov 2024 03:24:59 +0100 Subject: [PATCH] wip --- benchmark/README.md | 2 +- benchmark/benchmark/lib/main.dart | 43 +++++++++++++++++++---- benchmark/benchmark/pubspec.lock | 10 +++--- benchmark/nodejs/{main.js => download.js} | 0 benchmark/nodejs/upload.js | 38 ++++++++++++++++++++ rhttp/pubspec.yaml | 1 + 6 files changed, 81 insertions(+), 13 deletions(-) rename benchmark/nodejs/{main.js => download.js} (100%) create mode 100644 benchmark/nodejs/upload.js diff --git a/benchmark/README.md b/benchmark/README.md index 2bfb472..cd96533 100644 --- a/benchmark/README.md +++ b/benchmark/README.md @@ -4,7 +4,7 @@ Start server: ```shell cd nodejs -node main.js +node download.js ``` Start benchmark: diff --git a/benchmark/benchmark/lib/main.dart b/benchmark/benchmark/lib/main.dart index 6437a71..b8322b8 100644 --- a/benchmark/benchmark/lib/main.dart +++ b/benchmark/benchmark/lib/main.dart @@ -43,7 +43,7 @@ class _MyHomePageState extends State { children: [ ElevatedButton( onPressed: () async { - final time = await benchmarkHelper( + final time = await benchmarkDownload( debugLabel: 'http', count: _smallResponseIterations, url: Uri.parse('https://localhost:3000'), @@ -63,7 +63,7 @@ class _MyHomePageState extends State { ), ElevatedButton( onPressed: () async { - final time = await benchmarkHelper( + final time = await benchmarkDownload( debugLabel: 'http', count: _largeResponseIterations, url: Uri.parse('https://localhost:3000/large'), @@ -83,7 +83,7 @@ class _MyHomePageState extends State { ), ElevatedButton( onPressed: () async { - final time = await benchmarkHelper( + final time = await benchmarkDownload( debugLabel: 'dio', count: _smallResponseIterations, url: Uri.parse('https://localhost:3000'), @@ -113,7 +113,7 @@ class _MyHomePageState extends State { ), ElevatedButton( onPressed: () async { - final time = await benchmarkHelper( + final time = await benchmarkDownload( debugLabel: 'dio', count: _largeResponseIterations, url: Uri.parse('https://localhost:3000/large'), @@ -143,7 +143,7 @@ class _MyHomePageState extends State { ), ElevatedButton( onPressed: () async { - final time = await benchmarkHelper( + final time = await benchmarkDownload( debugLabel: 'rhttp', count: _smallResponseIterations, url: 'https://localhost:3000', @@ -165,7 +165,7 @@ class _MyHomePageState extends State { ), ElevatedButton( onPressed: () async { - final time = await benchmarkHelper( + final time = await benchmarkDownload( debugLabel: 'rhttp', count: _largeResponseIterations, url: 'https://localhost:3000/large', @@ -192,7 +192,7 @@ class _MyHomePageState extends State { } } -Future benchmarkHelper({ +Future benchmarkDownload({ required String debugLabel, required int count, required U url, @@ -212,3 +212,32 @@ Future benchmarkHelper({ return stopwatch.elapsedMilliseconds; } + +final _oneKb = Uint8List(1024); + +Future benchmarkUpload({ + required String debugLabel, + required int count, + required U url, + required Future Function() createClient, + required Future Function(T client, U url, Stream>) upload, +}) async { + print('Downloading benchmark using $debugLabel package...'); + + final client = await createClient(); + final padLeft = count.toString().length; + + final stopwatch = Stopwatch()..start(); + for (var i = 0; i < count; i++) { + await upload(client, url, _generateStream(1024)); + print('[${'${i + 1}'.padLeft(padLeft)}]'); + } + + return stopwatch.elapsedMilliseconds; +} + +Stream> _generateStream(int length) async* { + for (var i = 0; i < length; i += _oneKb.length) { + yield _oneKb; + } +} diff --git a/benchmark/benchmark/pubspec.lock b/benchmark/benchmark/pubspec.lock index 9c7c174..691036a 100644 --- a/benchmark/benchmark/pubspec.lock +++ b/benchmark/benchmark/pubspec.lock @@ -106,18 +106,18 @@ packages: dependency: transitive description: name: material_color_utilities - sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a" + sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec url: "https://pub.dev" source: hosted - version: "0.8.0" + version: "0.11.1" meta: dependency: transitive description: name: meta - sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136" + sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7 url: "https://pub.dev" source: hosted - version: "1.12.0" + version: "1.15.0" path: dependency: transitive description: @@ -132,7 +132,7 @@ packages: path: "../../rhttp" relative: true source: path - version: "0.7.2" + version: "0.8.0" sky_engine: dependency: transitive description: flutter diff --git a/benchmark/nodejs/main.js b/benchmark/nodejs/download.js similarity index 100% rename from benchmark/nodejs/main.js rename to benchmark/nodejs/download.js diff --git a/benchmark/nodejs/upload.js b/benchmark/nodejs/upload.js new file mode 100644 index 0000000..fb7e997 --- /dev/null +++ b/benchmark/nodejs/upload.js @@ -0,0 +1,38 @@ +const https = require('https'); +const fs = require('fs'); + +const PORT = 3000; +const HOST = '0.0.0.0'; + +// Read SSL certificate and private key +const privateKey = fs.readFileSync('../cert/privateKey.pem', 'utf8'); +const certificate = fs.readFileSync('../cert/cert.pem', 'utf8'); +const credentials = { key: privateKey, cert: certificate }; + +const requestHandler = (request, response) => { + let receivedBytes = 0; + + request.on('data', (chunk) => { + receivedBytes += chunk.length; + // Discard the data + }); + + request.on('end', () => { + console.log(`Received ${receivedBytes} bytes`); + response.statusCode = 200; + response.end('Upload complete'); + }); + + request.on('error', (err) => { + console.error('Error receiving data:', err); + response.statusCode = 500; + response.end('Error receiving data'); + }); +}; + +// Create HTTPS server with the provided SSL credentials and request handler +const server = https.createServer(credentials, requestHandler); + +server.listen(PORT, HOST, () => { + console.log(`Server listening on https://${HOST}:${PORT}`); +}); diff --git a/rhttp/pubspec.yaml b/rhttp/pubspec.yaml index 46e0e04..5b699c6 100644 --- a/rhttp/pubspec.yaml +++ b/rhttp/pubspec.yaml @@ -16,6 +16,7 @@ environment: flutter: '>=3.22.0' dependencies: + async: ^2.11.0 flutter: sdk: flutter flutter_rust_bridge: 2.5.1