-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathProgram.fs
More file actions
141 lines (108 loc) · 3.9 KB
/
Program.fs
File metadata and controls
141 lines (108 loc) · 3.9 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
module FSharp.SystemTextJson.Benchmarks
open FSharp.Reflection
open System
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Diagnosers
open BenchmarkDotNet.Configs
open BenchmarkDotNet.Jobs
open BenchmarkDotNet.Running
open BenchmarkDotNet.Validators
open BenchmarkDotNet.Exporters
open BenchmarkDotNet.Environments
open System.Reflection
open BenchmarkDotNet.Configs
open System.Text.Json
open System.Text.Json.Serialization
open Newtonsoft.Json
open Newtonsoft.Json.Linq
type TestRecord =
{ name: string
thing: bool option
time: System.DateTimeOffset }
type SimpleClass() =
member val Name: string = null with get, set
member val Thing: bool option = None with get, set
member val Time: DateTimeOffset = DateTimeOffset.MinValue with get, set
type ArrayTestBase<'t>(instance: 't) =
let systemTextOptions =
let options = JsonSerializerOptions()
options.Converters.Add(JsonFSharpConverter())
options
[<Params(10, 100)>]
member val ArrayLength = 0 with get, set
member val InstanceArray = [||] with get, set
[<GlobalSetup>]
member this.InitArray() =
this.InstanceArray <- Array.replicate this.ArrayLength instance
[<Benchmark>]
member this.Newtonsoft() =
JsonConvert.SerializeObject this.InstanceArray
[<Benchmark>]
member this.SystemTextJson() =
System.Text.Json.JsonSerializer.Serialize(this.InstanceArray, systemTextOptions)
let recordInstance =
{ name = "sample"
thing = Some true
time = System.DateTimeOffset.UnixEpoch.AddDays(200.) }
type Records() =
inherit ArrayTestBase<TestRecord>(recordInstance)
type Classes() =
inherit
ArrayTestBase<SimpleClass>(
SimpleClass(Name = "sample", Thing = Some true, Time = DateTimeOffset.UnixEpoch.AddDays(200.))
)
type ReflectionComparison() =
[<Params(10, 100, 1000)>]
member val Iterations = 0 with get, set
[<Benchmark>]
member this.FSharpUnion() =
for i in 0 .. this.Iterations do
FSharpType.IsUnion(typeof<bool option>, true) |> ignore
[<Benchmark>]
member this.FSharpUnionCached() =
for i in 0 .. this.Iterations do
TypeCache.isUnion typeof<bool option> |> ignore
[<Benchmark>]
member this.FSharpRecord() =
for i in 0 .. this.Iterations do
Reflection.FSharpType.IsRecord(typeof<TestRecord>, true) |> ignore
[<Benchmark>]
member this.FSharpRecordCached() =
for i in 0 .. this.Iterations do
TypeCache.isRecord typeof<TestRecord> |> ignore
type TupleComparison() =
let fsharpOptions = JsonFSharpOptions.Default()
let specializedOptions = fsharpOptions.ToJsonSerializerOptions()
let genericOptions =
let o = JsonSerializerOptions()
o.Converters.Add(JsonTupleConverter(fsharpOptions, true))
o
[<Benchmark>]
member this.StructGeneric() =
System.Text.Json.JsonSerializer.Deserialize<struct (int * bool)>("[1,true]", genericOptions)
[<Benchmark>]
member this.StructSpecialized() =
System.Text.Json.JsonSerializer.Deserialize<struct (int * bool)>("[1,true]", specializedOptions)
[<Benchmark>]
member this.RefGeneric() =
System.Text.Json.JsonSerializer.Deserialize<int * bool>("[1,true]", genericOptions)
[<Benchmark>]
member this.RefSpecialized() =
System.Text.Json.JsonSerializer.Deserialize<int * bool>("[1,true]", specializedOptions)
let config =
ManualConfig
.Create(DefaultConfig.Instance)
.AddDiagnoser(MemoryDiagnoser.Default)
.AddExporter(MarkdownExporter.GitHub)
.AddValidator(ExecutionValidator.FailOnError)
let defaultSwitch () =
BenchmarkSwitcher(
[| typeof<Records>
typeof<Classes>
typeof<ReflectionComparison>
typeof<TupleComparison> |]
)
[<EntryPoint>]
let main argv =
let _summary = defaultSwitch().Run(argv, config)
0