Skip to content

Instantly share code, notes, and snippets.

@shinayser
Last active July 29, 2019 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinayser/1305309b516a5d6583b6e38796a93446 to your computer and use it in GitHub Desktop.
Save shinayser/1305309b516a5d6583b6e38796a93446 to your computer and use it in GitHub Desktop.
Just playing with Dart's server capabilities
import 'dart:async';
import 'dart:io';
import 'dart:isolate';
import 'package:http/http.dart' as http;
// ignore_for_file: unawaited_futures
void main([List<String> args]) {
// prefix0.main(["15900152"]);
_buildServers();
// await _buildRequesters(args?.elementAt(0));
}
////////////////////////////////////////////////////////////////////////////////
void _buildServers() {
for (int i = 0; i < Platform.numberOfProcessors - 1; i++) {
Isolate.spawn(_startServer, null);
}
_startServer(null);
}
const String _GREET = 'Hello World';
_startServer(arg) async {
var server = await HttpServer.bind(
InternetAddress.loopbackIPv4,
666,
shared: true,
);
print("Iniciado servidor");
await for (HttpRequest request in server) {
print("Nova requisição");
request.response
..write(_GREET)
// ignore: unawaited_futures
..close();
}
}
////////////////////////////////////////////////////////////////////////////////
void _buildRequesters(String address) async {
int requester = 0;
for (int i = 0; i < Platform.numberOfProcessors - 1; i++) {
Isolate.spawn(_performRequest, address);
}
await _performRequest(address);
await Future.delayed(Duration(milliseconds: 2000));
}
_performRequest(String address) async {
final startTime = DateTime.now();
int count = 0;
var client = http.Client();
while (DateTime.now().difference(startTime) < Duration(seconds: 10)) {
await client.get(address);
count++;
}
client.close();
print("Requisições por segundo: ${count / 10}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment