|
| 1 | +/// Benchmark: System.Text.Json-backed parser vs current FSharp.Data hand-written parser |
| 2 | +/// Prototypes "Option 2" from https://github.com/fsprojects/FSharp.Data/issues/1671 — |
| 3 | +/// keep the JsonValue public API while using Utf8JsonReader / JsonDocument as the parsing kernel. |
| 4 | +/// |
| 5 | +/// Run with: dotnet run -c Release -- stj |
| 6 | +namespace FSharp.Data.Benchmarks |
| 7 | + |
| 8 | +open System.IO |
| 9 | +open System.Text.Json |
| 10 | +open BenchmarkDotNet.Attributes |
| 11 | +open FSharp.Data |
| 12 | + |
| 13 | +/// Converts a System.Text.Json JsonElement into a FSharp.Data JsonValue. |
| 14 | +/// This is the prototype STJ backend that could replace the hand-written parser. |
| 15 | +module private StjConverter = |
| 16 | + |
| 17 | + let rec ofJsonElement (el: JsonElement) : JsonValue = |
| 18 | + match el.ValueKind with |
| 19 | + | JsonValueKind.Null -> JsonValue.Null |
| 20 | + | JsonValueKind.True -> JsonValue.Boolean true |
| 21 | + | JsonValueKind.False -> JsonValue.Boolean false |
| 22 | + | JsonValueKind.String -> JsonValue.String(el.GetString()) |
| 23 | + | JsonValueKind.Number -> |
| 24 | + let mutable d = 0m |
| 25 | + |
| 26 | + if el.TryGetDecimal(&d) then |
| 27 | + JsonValue.Number(d) |
| 28 | + else |
| 29 | + JsonValue.Float(el.GetDouble()) |
| 30 | + | JsonValueKind.Array -> |
| 31 | + el.EnumerateArray() |
| 32 | + |> Seq.map ofJsonElement |
| 33 | + |> Seq.toArray |
| 34 | + |> JsonValue.Array |
| 35 | + | JsonValueKind.Object -> |
| 36 | + el.EnumerateObject() |
| 37 | + |> Seq.map (fun p -> p.Name, ofJsonElement p.Value) |
| 38 | + |> Seq.toArray |
| 39 | + |> JsonValue.Record |
| 40 | + | _ -> JsonValue.Null |
| 41 | + |
| 42 | + /// Parse a JSON string to JsonValue using System.Text.Json as the parsing backend. |
| 43 | + let parse (text: string) : JsonValue = |
| 44 | + use doc = JsonDocument.Parse(text) |
| 45 | + ofJsonElement doc.RootElement |
| 46 | + |
| 47 | +/// Compares the current FSharp.Data hand-written parser with a System.Text.Json-backed |
| 48 | +/// prototype on three representative real-world JSON files. See issue #1671 for context. |
| 49 | +[<MemoryDiagnoser>] |
| 50 | +[<SimpleJob>] |
| 51 | +type JsonStjBenchmarks() = |
| 52 | + |
| 53 | + let mutable githubJsonText = "" |
| 54 | + let mutable twitterJsonText = "" |
| 55 | + let mutable worldBankJsonText = "" |
| 56 | + |
| 57 | + [<GlobalSetup>] |
| 58 | + member _.Setup() = |
| 59 | + let dataPath = Path.Combine(__SOURCE_DIRECTORY__, "../FSharp.Data.Tests/Data") |
| 60 | + githubJsonText <- File.ReadAllText(Path.Combine(dataPath, "GitHub.json")) |
| 61 | + twitterJsonText <- File.ReadAllText(Path.Combine(dataPath, "TwitterSample.json")) |
| 62 | + worldBankJsonText <- File.ReadAllText(Path.Combine(dataPath, "WorldBank.json")) |
| 63 | + |
| 64 | + // ── Current hand-written parser (baseline) ────────────────────────────────────── |
| 65 | + |
| 66 | + [<Benchmark(Baseline = true)>] |
| 67 | + member _.ParseGitHub_Current() = JsonValue.Parse(githubJsonText) |
| 68 | + |
| 69 | + [<Benchmark>] |
| 70 | + member _.ParseTwitter_Current() = JsonValue.Parse(twitterJsonText) |
| 71 | + |
| 72 | + [<Benchmark>] |
| 73 | + member _.ParseWorldBank_Current() = JsonValue.Parse(worldBankJsonText) |
| 74 | + |
| 75 | + // ── STJ-backed prototype (Option 2 from #1671) ────────────────────────────────── |
| 76 | + |
| 77 | + [<Benchmark>] |
| 78 | + member _.ParseGitHub_Stj() = StjConverter.parse githubJsonText |
| 79 | + |
| 80 | + [<Benchmark>] |
| 81 | + member _.ParseTwitter_Stj() = StjConverter.parse twitterJsonText |
| 82 | + |
| 83 | + [<Benchmark>] |
| 84 | + member _.ParseWorldBank_Stj() = StjConverter.parse worldBankJsonText |
0 commit comments