Last active
July 29, 2019 22:08
-
-
Save shinayser/1305309b516a5d6583b6e38796a93446 to your computer and use it in GitHub Desktop.
Just playing with Dart's server capabilities
This file contains 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: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