Forráskód Böngészése

Add basic JSON parsing/serialization benchmark (#1510)

Marko Lahma 2 éve
szülő
commit
d4ba479562

+ 0 - 1
Jint.Benchmark/Jint.Benchmark.csproj

@@ -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>

+ 70 - 0
Jint.Benchmark/JsonBenchmark.cs

@@ -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]);
+    }
+}

+ 4 - 11
Jint.Benchmark/Program.cs

@@ -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);