Created
June 4, 2024 00:03
-
-
Save codekeyz/166eac14a05e2a66caf855a12762e143 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'dart:convert'; | |
import 'dart:io'; | |
Future<void> main() async { | |
final payloadSize = 10 * 1024 * 1024; // 10 MB | |
final payload = 'A' * payloadSize; | |
// Set up an HTTP server to send a large payload | |
final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 8080); | |
server.listen((HttpRequest request) async { | |
if (request.uri.path == '/test1') { | |
final body = await utf8.decoder.bind(request).join(); | |
request.response | |
..statusCode = HttpStatus.ok | |
..write('Received ${body.length} bytes') | |
..close(); | |
} else if (request.uri.path == '/test2') { | |
final buffer = StringBuffer(); | |
await for (final chunk in utf8.decoder.bind(request)) { | |
buffer.write(chunk); | |
} | |
final body = buffer.toString(); | |
request.response | |
..statusCode = HttpStatus.ok | |
..write('Received ${body.length} bytes') | |
..close(); | |
} | |
}); | |
// Function to perform a benchmark | |
Future<void> benchmark(String url, String testName) async { | |
final stopwatch = Stopwatch()..start(); | |
final client = HttpClient(); | |
final request = await client.postUrl(Uri.parse(url)); | |
request.write(payload); | |
final response = await request.close(); | |
await response.drain(); | |
client.close(); | |
stopwatch.stop(); | |
print('$testName took ${stopwatch.elapsedMilliseconds} ms'); | |
} | |
// Run the benchmarks | |
print('Starting benchmarks...'); | |
await benchmark('http://localhost:8080/test1', 'utf8.decoder.bind.join()'); | |
await benchmark('http://localhost:8080/test2', 'req.transform(utf8.decoder)'); | |
// Close the server | |
await server.close(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment