Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic JSON parsing/serialization benchmark #1510

Merged
merged 1 commit into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion Jint.Benchmark/Jint.Benchmark.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AssemblyName>Jint.Benchmark</AssemblyName>
<OutputType>Exe</OutputType>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
Expand Down
70 changes: 70 additions & 0 deletions Jint.Benchmark/JsonBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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]);
}
}
15 changes: 4 additions & 11 deletions Jint.Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
using System.Reflection;
using BenchmarkDotNet.Running;
using Jint.Benchmark;

namespace Jint.Benchmark;

public static class Program
{
public static void Main(string[] args)
{
BenchmarkSwitcher
.FromAssembly(typeof(Program).GetTypeInfo().Assembly)
.Run(args);
}
}
BenchmarkSwitcher
.FromAssembly(typeof(ArrayBenchmark).GetTypeInfo().Assembly)
.Run(args);