dart-sdk/benchmarks/EventLoopLatencyJson/dart/json_benchmark.dart
Martin Kustermann 2935fecccb [vm/concurrency] Add EventLoopLatencyJson benchmarks
Issue https://github.com/dart-lang/sdk/issues/36097

Change-Id: Ic364e7dccc15bc14832ce61e45ba6c0e241dbbc3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/162509
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Alexander Aprelev <aam@google.com>
2020-09-11 16:46:22 +00:00

48 lines
1.2 KiB
Dart

// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:math';
import 'dart:convert';
class JsonRoundTripBenchmark {
void run() {
final res = json.decode(jsonData);
final out = json.encode(res);
if (out[0] != jsonData[0]) {
throw 'json conversion error';
}
}
}
// Builds around 4.5 MB of json data - big enough so the decoded object graph
// does not fit into new space.
final String jsonData = () {
final rnd = Random(42);
dynamic buildTree(int depth) {
final int coin = rnd.nextInt(1000);
if (depth == 0) {
if (coin % 2 == 0) return coin;
return 'foo-$coin';
}
if (coin % 2 == 0) {
final map = <String, dynamic>{};
final int length = rnd.nextInt(18);
for (int i = 0; i < length; ++i) {
map['bar-$i'] = buildTree(depth - 1);
}
return map;
} else {
final list = <dynamic>[];
final int length = rnd.nextInt(18);
for (int i = 0; i < length; ++i) {
list.add(buildTree(depth - 1));
}
return list;
}
}
return json.encode({'data': buildTree(6)});
}();