Skip to content

Instantly share code, notes, and snippets.

@rajveermalviya
Created September 30, 2024 17:02
Show Gist options
  • Save rajveermalviya/7b4d92f84c68f0976ed07f6d797ac164 to your computer and use it in GitHub Desktop.
Save rajveermalviya/7b4d92f84c68f0976ed07f6d797ac164 to your computer and use it in GitHub Desktop.
import 'dart:convert';
import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:http/http.dart' as http;
final jsonUtf8Decoder = const Utf8Decoder().fuse(const JsonDecoder());
class StreamingJsonBenchmark extends AsyncBenchmarkBase {
StreamingJsonBenchmark(this.uri)
: super('${uri.path} StreamingJsonBenchmark');
final Uri uri;
static Future<void> main(Uri uri) async {
await StreamingJsonBenchmark(uri).report();
}
late final http.Client _client;
@override
Future<void> setup() async {
_client = http.Client();
}
@override
Future<void> teardown() async {
_client.close();
}
@override
Future<void> run() async {
final streamedResponse = await _client.send(http.Request('GET', uri));
final jsonStream = jsonUtf8Decoder.bind(streamedResponse.stream);
final json = await jsonStream.single as Map<String, dynamic>?;
if (json!.isEmpty) {
print('fail');
}
}
}
class NonStreamingJsonBenchmark extends AsyncBenchmarkBase {
NonStreamingJsonBenchmark(this.uri)
: super('${uri.path} NonStreamingJsonBenchmark');
final Uri uri;
static Future<void> main(Uri uri) async {
await NonStreamingJsonBenchmark(uri).report();
}
late final http.Client _client;
@override
Future<void> setup() async {
_client = http.Client();
}
@override
Future<void> teardown() async {
_client.close();
}
@override
Future<void> run() async {
final streamedResponse = await _client.send(http.Request('GET', uri));
final bytes = await streamedResponse.stream.toBytes();
final json = jsonUtf8Decoder.convert(bytes) as Map<String, dynamic>?;
if (json!.isEmpty) {
print('fail');
}
}
}
Future<void> main() async {
// curl -XPOST 'https://chat.zulip.org/api/v1/register' \
// --header 'content-type: application/x-www-form-urlencoded; charset=utf-8' \
// --data-raw 'apply_markdown=true&slim_presence=true&client_gravatar=false&client_capabilities=%7B%22notification_settings_null%22%3Atrue%2C%22bulk_message_deletion%22%3Atrue%2C%22user_avatar_url_field_optional%22%3Afalse%2C%22stream_typing_notifications%22%3Atrue%2C%22user_settings_object%22%3Atrue%7D' \
// --netrc-file .netrc \
// -o register.json
final Uri registerUri = Uri.parse('http://localhost:8080/register.json');
await StreamingJsonBenchmark.main(registerUri);
await NonStreamingJsonBenchmark.main(registerUri);
// curl https://chat.zulip.org/static/generated/emoji/emoji_api.ed3d6cb1ae06.json -o emoji_api.json
final Uri emojiUri = Uri.parse('http://localhost:8080/emoji_api.json');
await StreamingJsonBenchmark.main(emojiUri);
await NonStreamingJsonBenchmark.main(emojiUri);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment