-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathJsonBenchmarks.fs
More file actions
96 lines (79 loc) · 3.06 KB
/
Copy pathJsonBenchmarks.fs
File metadata and controls
96 lines (79 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
namespace FSharp.Data.Benchmarks
open System
open System.IO
open BenchmarkDotNet.Attributes
open FSharp.Data
[<MemoryDiagnoser>]
[<SimpleJob>]
type JsonBenchmarks() =
let mutable githubJsonText = ""
let mutable twitterJsonText = ""
let mutable worldBankJsonText = ""
let mutable simpleJsonText = ""
let mutable nestedJsonText = ""
[<GlobalSetup>]
member this.Setup() =
let dataPath = Path.Combine(__SOURCE_DIRECTORY__, "../FSharp.Data.Tests/Data")
githubJsonText <- File.ReadAllText(Path.Combine(dataPath, "GitHub.json"))
twitterJsonText <- File.ReadAllText(Path.Combine(dataPath, "TwitterSample.json"))
worldBankJsonText <- File.ReadAllText(Path.Combine(dataPath, "WorldBank.json"))
simpleJsonText <- File.ReadAllText(Path.Combine(dataPath, "Simple.json"))
nestedJsonText <- File.ReadAllText(Path.Combine(dataPath, "Nested.json"))
[<Benchmark>]
member this.ParseSimpleJson() =
JsonValue.Parse(simpleJsonText)
[<Benchmark>]
member this.ParseNestedJson() =
JsonValue.Parse(nestedJsonText)
[<Benchmark>]
member this.ParseGitHubJson() =
JsonValue.Parse(githubJsonText)
[<Benchmark>]
member this.ParseTwitterJson() =
JsonValue.Parse(twitterJsonText)
[<Benchmark>]
member this.ParseWorldBankJson() =
JsonValue.Parse(worldBankJsonText)
[<Benchmark>]
member this.ToStringGitHubJson() =
let json = JsonValue.Parse(githubJsonText)
json.ToString()
[<Benchmark>]
member this.ToStringTwitterJson() =
let json = JsonValue.Parse(twitterJsonText)
json.ToString()
[<MemoryDiagnoser>]
[<SimpleJob>]
type JsonConversionBenchmarks() =
let mutable sampleJson = JsonValue.Null
[<GlobalSetup>]
member this.Setup() =
let dataPath = Path.Combine(__SOURCE_DIRECTORY__, "../FSharp.Data.Tests/Data")
let githubJsonText = File.ReadAllText(Path.Combine(dataPath, "GitHub.json"))
sampleJson <- JsonValue.Parse(githubJsonText)
[<Benchmark>]
member this.AccessProperties() =
match sampleJson with
| JsonValue.Array elements when elements.Length > 0 ->
// Get properties of the first issue in the GitHub issues array
elements.[0].Properties()
|> Array.head
|> fun (_, value) -> value
| JsonValue.Record _ ->
// Fallback for record-type JSON
sampleJson.Properties()
|> Array.head
|> fun (_, value) -> value
| _ -> JsonValue.Null
[<Benchmark>]
member this.GetArrayElements() =
match sampleJson with
| JsonValue.Array elements ->
// GitHub.json is an array of issues, return the array elements
elements
| JsonValue.Record props ->
// Fallback: look for an "items" property
match Array.tryFind (fun (k, _) -> k = "items") props with
| Some (_, JsonValue.Array elements) -> elements
| _ -> [||]
| _ -> [||]