-
-
Notifications
You must be signed in to change notification settings - Fork 560
/
JsonBenchmark.cs
70 lines (58 loc) · 2.06 KB
/
JsonBenchmark.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using BenchmarkDotNet.Attributes;
using Jint.Native;
using Jint.Native.Json;
namespace Jint.Benchmark;
[MemoryDiagnoser]
public class JsonBenchmark
{
private Engine _engine;
private readonly Dictionary<string, string> _sources = new()
{
{ "twitter.json", "https://raw.githubusercontent.com/miloyip/nativejson-benchmark/master/data/twitter.json" },
{ "bestbuy_dataset.json", "https://github.com/algolia/examples/raw/master/instant-search/instantsearch.js/dataset_import/bestbuy_dataset.json" },
};
private readonly Dictionary<string, JsValue> _parsedInstance = new();
private readonly Dictionary<string, string> _json = new();
[GlobalSetup]
public async Task GlobalSetup()
{
_engine = new Engine();
foreach (var source in _sources)
{
var filePath = Path.Combine(Path.GetTempPath(), source.Key);
if (!File.Exists(filePath))
{
using var client = new HttpClient();
using var response = await client.GetAsync(source.Value);
await using var streamToReadFrom = await response.Content.ReadAsStreamAsync();
await using var streamToWriteTo = File.OpenWrite(filePath);
await streamToReadFrom.CopyToAsync(streamToWriteTo);
}
var json = await File.ReadAllTextAsync(filePath);
_json[source.Key] = json;
var parser = new JsonParser(_engine);
_parsedInstance[source.Key] = parser.Parse(json);
}
}
public IEnumerable<string> FileNames()
{
foreach (var entry in _sources)
{
yield return entry.Key;
}
}
[ParamsSource(nameof(FileNames))]
public string FileName { get; set; }
[Benchmark]
public JsValue Parse()
{
var parser = new JsonParser(_engine);
return parser.Parse(_json[FileName]);
}
[Benchmark]
public JsValue Stringify()
{
var serializer = new JsonSerializer(_engine);
return serializer.Serialize(_parsedInstance[FileName]);
}
}