Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Tienisto committed Nov 2, 2024
1 parent 22a2ab1 commit 6845456
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 13 deletions.
2 changes: 1 addition & 1 deletion benchmark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Start server:

```shell
cd nodejs
node main.js
node download.js
```

Start benchmark:
Expand Down
43 changes: 36 additions & 7 deletions benchmark/benchmark/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class _MyHomePageState extends State<MyHomePage> {
children: [
ElevatedButton(
onPressed: () async {
final time = await benchmarkHelper(
final time = await benchmarkDownload(
debugLabel: 'http',
count: _smallResponseIterations,
url: Uri.parse('https://localhost:3000'),
Expand All @@ -63,7 +63,7 @@ class _MyHomePageState extends State<MyHomePage> {
),
ElevatedButton(
onPressed: () async {
final time = await benchmarkHelper(
final time = await benchmarkDownload(
debugLabel: 'http',
count: _largeResponseIterations,
url: Uri.parse('https://localhost:3000/large'),
Expand All @@ -83,7 +83,7 @@ class _MyHomePageState extends State<MyHomePage> {
),
ElevatedButton(
onPressed: () async {
final time = await benchmarkHelper(
final time = await benchmarkDownload(
debugLabel: 'dio',
count: _smallResponseIterations,
url: Uri.parse('https://localhost:3000'),
Expand Down Expand Up @@ -113,7 +113,7 @@ class _MyHomePageState extends State<MyHomePage> {
),
ElevatedButton(
onPressed: () async {
final time = await benchmarkHelper(
final time = await benchmarkDownload(
debugLabel: 'dio',
count: _largeResponseIterations,
url: Uri.parse('https://localhost:3000/large'),
Expand Down Expand Up @@ -143,7 +143,7 @@ class _MyHomePageState extends State<MyHomePage> {
),
ElevatedButton(
onPressed: () async {
final time = await benchmarkHelper(
final time = await benchmarkDownload(
debugLabel: 'rhttp',
count: _smallResponseIterations,
url: 'https://localhost:3000',
Expand All @@ -165,7 +165,7 @@ class _MyHomePageState extends State<MyHomePage> {
),
ElevatedButton(
onPressed: () async {
final time = await benchmarkHelper(
final time = await benchmarkDownload(
debugLabel: 'rhttp',
count: _largeResponseIterations,
url: 'https://localhost:3000/large',
Expand All @@ -192,7 +192,7 @@ class _MyHomePageState extends State<MyHomePage> {
}
}

Future<int> benchmarkHelper<T, U>({
Future<int> benchmarkDownload<T, U>({
required String debugLabel,
required int count,
required U url,
Expand All @@ -212,3 +212,32 @@ Future<int> benchmarkHelper<T, U>({

return stopwatch.elapsedMilliseconds;
}

final _oneKb = Uint8List(1024);

Future<int> benchmarkUpload<T, U>({
required String debugLabel,
required int count,
required U url,
required Future<T> Function() createClient,
required Future<void> Function(T client, U url, Stream<List<int>>) 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<List<int>> _generateStream(int length) async* {
for (var i = 0; i < length; i += _oneKb.length) {
yield _oneKb;
}
}
10 changes: 5 additions & 5 deletions benchmark/benchmark/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
File renamed without changes.
38 changes: 38 additions & 0 deletions benchmark/nodejs/upload.js
Original file line number Diff line number Diff line change
@@ -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}`);
});
1 change: 1 addition & 0 deletions rhttp/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ environment:
flutter: '>=3.22.0'

dependencies:
async: ^2.11.0
flutter:
sdk: flutter
flutter_rust_bridge: 2.5.1
Expand Down

0 comments on commit 6845456

Please sign in to comment.