-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathProgram.fs
More file actions
129 lines (96 loc) · 3.86 KB
/
Program.fs
File metadata and controls
129 lines (96 loc) · 3.86 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
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 }
[<Struct>]
type TestStructRecord =
{ name: string
thing: bool voption
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,1000)>]
member val ArrayLength = 0 with get, set
member val InstanceArray = [||] with get, set
member val Serialized = "" with get, set
[<GlobalSetup>]
member this.InitArray () =
this.InstanceArray <- Array.replicate this.ArrayLength instance
this.Serialized <- this.InstanceArray |> JsonConvert.SerializeObject
[<Benchmark>]
member this.Serialize_Newtonsoft () = JsonConvert.SerializeObject this.InstanceArray
[<Benchmark>]
member this.Serialize_SystemTextJson () = System.Text.Json.JsonSerializer.Serialize(this.InstanceArray, systemTextOptions)
[<Benchmark>]
member this.Deserialize_Newtonsoft () = JsonConvert.DeserializeObject<'t[]> this.Serialized
[<Benchmark>]
member this.Deserialize_SystemTextJson () = System.Text.Json.JsonSerializer.Deserialize<'t[]>(this.Serialized, systemTextOptions)
let recordInstance : TestRecord =
{ name = "sample"
thing = Some true
time = System.DateTimeOffset.UnixEpoch.AddDays(200.) }
type Records () =
inherit ArrayTestBase<TestRecord>(recordInstance)
let recordStructInstance : TestStructRecord =
{ name = "sample"
thing = ValueSome true
time = System.DateTimeOffset.UnixEpoch.AddDays(200.) }
type StructRecords () =
inherit ArrayTestBase<TestStructRecord>(recordStructInstance)
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
let config =
ManualConfig
.Create(DefaultConfig.Instance)
.With(Job.ShortRun.With(Runtime.Core))
.With(MemoryDiagnoser.Default)
.With(MarkdownExporter.GitHub)
.With(ExecutionValidator.FailOnError)
let defaultSwitch () =
BenchmarkSwitcher([| typeof<Records>; typeof<StructRecords>; typeof<Classes>; typeof<ReflectionComparison> |])
[<EntryPoint>]
let main argv =
let _summary = defaultSwitch().Run(argv, config)
0