Skip to content

Commit 92bf264

Browse files
committed
Add a JSON static block benchmark in static block mode.
!BENCH!
1 parent 3878908 commit 92bf264

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

Diff for: sample/Farkle.Samples.FSharp/FsLexYacc.fs

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ let parseString x =
1212
let lexBuf = LexBuffer<_>.FromString x
1313
Parser.value Lexer.read lexBuf
1414

15-
let parseFile path =
15+
let parseTextReader tr =
1616
// Holy fuzzy, they didn't even
1717
// make LexBuffers disposable!
18-
use f = File.OpenText path
19-
let lexBuf = LexBuffer<_>.FromTextReader f
18+
let lexBuf = LexBuffer<_>.FromTextReader tr
2019
Parser.value Lexer.read lexBuf
20+
21+
let parseFile path =
22+
use f = File.OpenText path
23+
parseTextReader f

Diff for: tests/Farkle.Benchmarks/JsonBenchmark.fs

+21-9
Original file line numberDiff line numberDiff line change
@@ -12,54 +12,66 @@ open Farkle.Common
1212
open Farkle.JSON
1313
open Farkle.PostProcessor
1414
open FParsec
15-
open System.Text
15+
open System.IO
1616

1717
type JsonBenchmark() =
1818

1919
// File I/O during parsing will affect them, but the benchmarks measure
2020
// parsing when not the entire file is available on memory.
2121
let jsonFile = "generated.json"
2222

23+
let jsonData = File.ReadAllText jsonFile
24+
25+
let createTR() = new StringReader(jsonData) :> TextReader
26+
2327
let syntaxChecker = RuntimeFarkle.changePostProcessor PostProcessor.syntaxCheck FSharp.Language.runtime
2428

2529
[<Benchmark>]
2630
// There are performance differences between the F# and C# editions.
2731
// The separate benchmarks will stay for now.
2832
member __.FarkleCSharp() =
29-
RuntimeFarkle.parseFile CSharp.Language.Runtime ignore jsonFile
33+
RuntimeFarkle.parseTextReader CSharp.Language.Runtime ignore <| createTR()
3034
|> returnOrFail
3135

3236
[<Benchmark>]
3337
member __.FarkleFSharp() =
34-
RuntimeFarkle.parseFile FSharp.Language.runtime ignore jsonFile
38+
RuntimeFarkle.parseTextReader FSharp.Language.runtime ignore <| createTR()
39+
|> returnOrFail
40+
41+
[<Benchmark>]
42+
// The pseudo-static block mode (a dynamic block with
43+
// a preloaded buffer as large as the input) is quite
44+
// the waste on large inputs. That's why we parse from
45+
// string readers earlier.
46+
member __.FarkleFSharpStaticBlock() =
47+
RuntimeFarkle.parse FSharp.Language.runtime jsonData
3548
|> returnOrFail
3649

37-
[<Benchmark>]
50+
[<Benchmark>]
3851
member __.FarkleSyntaxCheck() =
39-
RuntimeFarkle.parseFile syntaxChecker ignore jsonFile
52+
RuntimeFarkle.parseTextReader syntaxChecker ignore <| createTR()
4053
|> returnOrFail
4154

4255
[<Benchmark(Baseline = true)>]
4356
// Chiron uses FParsec underneath, which is the main competitor of Farkle.
4457
// I could use the Big Data edition, but it is not branded as their main
4558
// edition, and I am not going to do them any favors by allowing unsafe code.
4659
member __.Chiron() =
47-
let parseResult = runParserOnFile !jsonR () jsonFile Encoding.UTF8
60+
let parseResult = run !jsonR jsonData
4861
match parseResult with
4962
| Success (json, _, _) -> json
5063
| Failure _ -> failwithf "Error while parsing '%s'" jsonFile
5164

5265
#if BENCHMARK_3RD_PARTY
5366
[<Benchmark>]
5467
// Holy fuzzy, that was unbelievably fast! 🚄
55-
member __.FsLexYacc() = FsLexYacc.JSON.JSONParser.parseFile jsonFile
68+
member __.FsLexYacc() = FsLexYacc.JSON.JSONParser.parseTextReader <| createTR()
5669

5770
[<Benchmark>]
5871
// I was reluctant to add this, but I did, after I saw FsLexYacc in action.
5972
// I am interested to know how fast it can be. And yes, I know
6073
// about System.Text.Json! 😛
6174
member __.JsonNET() =
62-
use f = System.IO.File.OpenText jsonFile
63-
use jtr = new Newtonsoft.Json.JsonTextReader(f)
75+
use jtr = new Newtonsoft.Json.JsonTextReader(createTR())
6476
Newtonsoft.Json.Linq.JToken.ReadFrom jtr
6577
#endif

0 commit comments

Comments
 (0)