@@ -12,54 +12,66 @@ open Farkle.Common
12
12
open Farkle.JSON
13
13
open Farkle.PostProcessor
14
14
open FParsec
15
- open System.Text
15
+ open System.IO
16
16
17
17
type JsonBenchmark () =
18
18
19
19
// File I/O during parsing will affect them, but the benchmarks measure
20
20
// parsing when not the entire file is available on memory.
21
21
let jsonFile = " generated.json"
22
22
23
+ let jsonData = File.ReadAllText jsonFile
24
+
25
+ let createTR () = new StringReader( jsonData) :> TextReader
26
+
23
27
let syntaxChecker = RuntimeFarkle.changePostProcessor PostProcessor.syntaxCheck FSharp.Language.runtime
24
28
25
29
[<Benchmark>]
26
30
// There are performance differences between the F# and C# editions.
27
31
// The separate benchmarks will stay for now.
28
32
member __.FarkleCSharp () =
29
- RuntimeFarkle.parseFile CSharp.Language.Runtime ignore jsonFile
33
+ RuntimeFarkle.parseTextReader CSharp.Language.Runtime ignore <| createTR ()
30
34
|> returnOrFail
31
35
32
36
[<Benchmark>]
33
37
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
35
48
|> returnOrFail
36
49
37
- [<Benchmark>]
50
+ [<Benchmark>]
38
51
member __.FarkleSyntaxCheck () =
39
- RuntimeFarkle.parseFile syntaxChecker ignore jsonFile
52
+ RuntimeFarkle.parseTextReader syntaxChecker ignore <| createTR ()
40
53
|> returnOrFail
41
54
42
55
[<Benchmark( Baseline = true ) >]
43
56
// Chiron uses FParsec underneath, which is the main competitor of Farkle.
44
57
// I could use the Big Data edition, but it is not branded as their main
45
58
// edition, and I am not going to do them any favors by allowing unsafe code.
46
59
member __.Chiron () =
47
- let parseResult = runParserOnFile ! jsonR () jsonFile Encoding.UTF8
60
+ let parseResult = run ! jsonR jsonData
48
61
match parseResult with
49
62
| Success ( json, _, _) -> json
50
63
| Failure _ -> failwithf " Error while parsing '%s '" jsonFile
51
64
52
65
#if BENCHMARK_ 3RD_ PARTY
53
66
[<Benchmark>]
54
67
// Holy fuzzy, that was unbelievably fast! 🚄
55
- member __.FsLexYacc () = FsLexYacc.JSON.JSONParser.parseFile jsonFile
68
+ member __.FsLexYacc () = FsLexYacc.JSON.JSONParser.parseTextReader <| createTR ()
56
69
57
70
[<Benchmark>]
58
71
// I was reluctant to add this, but I did, after I saw FsLexYacc in action.
59
72
// I am interested to know how fast it can be. And yes, I know
60
73
// about System.Text.Json! 😛
61
74
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())
64
76
Newtonsoft.Json.Linq.JToken.ReadFrom jtr
65
77
#endif
0 commit comments