Skip to content
Snippets Groups Projects
Commit 4c7f421e authored by doodlezucc's avatar doodlezucc
Browse files

move fetch statistics to separate class

parent 68df2013
No related branches found
No related tags found
No related merge requests found
...@@ -11,7 +11,7 @@ import 'element_type.dart'; ...@@ -11,7 +11,7 @@ import 'element_type.dart';
import 'lecture.dart'; import 'lecture.dart';
typedef RequestOptions = (String path, String method, Json params); typedef RequestOptions = (String path, String method, Json params);
typedef JsonWithCookies = (Json json, List<Cookie> cookies); typedef RequestResult = (Json json, List<Cookie> cookies, FetchStats stats);
class UntisFetch { class UntisFetch {
static const String apiBase = '/WebUntis/api/public'; static const String apiBase = '/WebUntis/api/public';
...@@ -64,10 +64,10 @@ class UntisFetch { ...@@ -64,10 +64,10 @@ class UntisFetch {
return response; return response;
} }
Future<JsonWithCookies> _requestJson(RequestOptions options) async { Future<RequestResult> _requestJson(RequestOptions options) async {
final (_, String method, _) = options; final (_, String method, _) = options;
final before = DateTime.now(); final onStart = DateTime.now();
final response = await _request(method, _makeUri(options)); final response = await _request(method, _makeUri(options));
final cookies = response.cookies; final cookies = response.cookies;
...@@ -79,30 +79,19 @@ class UntisFetch { ...@@ -79,30 +79,19 @@ class UntisFetch {
} }
} }
final afterDownloadStart = DateTime.now(); final onDownloadStart = DateTime.now();
final responseTime = afterDownloadStart.difference(before);
final responseString = await utf8.decodeStream(response); final responseString = await utf8.decodeStream(response);
final onDownloadEnd = DateTime.now();
final afterDownloadEnd = DateTime.now();
final downloadTime = afterDownloadEnd.difference(afterDownloadStart);
final bodyJson = jsonDecode(responseString); final bodyJson = jsonDecode(responseString);
final afterDecode = DateTime.now(); final onDecodeEnd = DateTime.now();
final decodeTime = afterDecode.difference(afterDownloadEnd);
final timeStats = { final stats =
'Ping': responseTime, FetchStats(onStart, onDownloadStart, onDownloadEnd, onDecodeEnd);
'Download': downloadTime,
'JSON': decodeTime,
};
print(timeStats.entries return (bodyJson as Json, resultCookies, stats);
.map((e) => '${e.key}: ${e.value.inMilliseconds}ms')
.join(', '));
return (bodyJson as Json, resultCookies);
} }
Future<Json> requestOptions(RequestOptions options) async { Future<Json> requestOptions(RequestOptions options) async {
...@@ -112,9 +101,10 @@ class UntisFetch { ...@@ -112,9 +101,10 @@ class UntisFetch {
final methodPadded = method.padRight(8); final methodPadded = method.padRight(8);
print('$methodPadded $uri'); print('$methodPadded $uri');
final (result, cookies) = await compute(_requestJson, options); final (result, cookies, stats) = await compute(_requestJson, options);
_sessionCookies.addAll(cookies); _sessionCookies.addAll(cookies);
print('$stats');
return result; return result;
} }
...@@ -127,3 +117,34 @@ class UntisFetch { ...@@ -127,3 +117,34 @@ class UntisFetch {
return await requestOptions(options); return await requestOptions(options);
} }
} }
class FetchStats {
final DateTime onStart;
final DateTime onDownloadStart;
final DateTime onDownloadEnd;
final DateTime onDecodeEnd;
FetchStats(
this.onStart,
this.onDownloadStart,
this.onDownloadEnd,
this.onDecodeEnd,
);
@override
String toString() {
final responseTime = onDownloadStart.difference(onStart);
final downloadTime = onDownloadEnd.difference(onDownloadStart);
final decodeTime = onDecodeEnd.difference(onDownloadEnd);
final timeStats = {
'Ping': responseTime,
'Download': downloadTime,
'JSON': decodeTime,
};
return timeStats.entries
.map((e) => '${e.key}: ${e.value.inMilliseconds}ms')
.join(', ');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment