|
1 | | -module FSharp.SystemTextJson.Benchmarks |
2 | | - |
3 | | -open FSharp.Reflection |
4 | | - |
5 | | -open System |
6 | | -open BenchmarkDotNet.Attributes |
7 | | -open BenchmarkDotNet.Diagnosers |
8 | | -open BenchmarkDotNet.Configs |
9 | | -open BenchmarkDotNet.Jobs |
10 | | -open BenchmarkDotNet.Running |
11 | | -open BenchmarkDotNet.Validators |
12 | | -open BenchmarkDotNet.Exporters |
13 | | -open BenchmarkDotNet.Environments |
14 | | -open System.Reflection |
15 | | -open BenchmarkDotNet.Configs |
16 | | - |
17 | | -open System.Text.Json |
18 | | -open System.Text.Json.Serialization |
19 | | -open Newtonsoft.Json |
20 | | -open Newtonsoft.Json.Linq |
21 | | - |
22 | | -type TestRecord = |
23 | | - { name: string |
24 | | - thing: bool option |
25 | | - time: System.DateTimeOffset } |
26 | | - |
27 | | -type SimpleClass() = |
28 | | - member val Name: string = null with get, set |
29 | | - member val Thing: bool option = None with get, set |
30 | | - member val Time: DateTimeOffset = DateTimeOffset.MinValue with get, set |
31 | | - |
32 | | -type ArrayTestBase<'t>(instance: 't) = |
33 | | - let systemTextOptions = |
34 | | - let options = JsonSerializerOptions() |
35 | | - options.Converters.Add(JsonFSharpConverter()) |
36 | | - options |
37 | | - |
38 | | - |
39 | | - [<Params(10, 100)>] |
40 | | - member val ArrayLength = 0 with get, set |
41 | | - |
42 | | - member val InstanceArray = [||] with get, set |
43 | | - |
44 | | - [<GlobalSetup>] |
45 | | - member this.InitArray() = |
46 | | - this.InstanceArray <- Array.replicate this.ArrayLength instance |
47 | | - |
48 | | - [<Benchmark>] |
49 | | - member this.Newtonsoft() = |
50 | | - JsonConvert.SerializeObject this.InstanceArray |
51 | | - |
52 | | - [<Benchmark>] |
53 | | - member this.SystemTextJson() = |
54 | | - System.Text.Json.JsonSerializer.Serialize(this.InstanceArray, systemTextOptions) |
55 | | - |
56 | | -let recordInstance = |
57 | | - { name = "sample" |
58 | | - thing = Some true |
59 | | - time = System.DateTimeOffset.UnixEpoch.AddDays(200.) } |
60 | | - |
61 | | - |
62 | | -type Records() = |
63 | | - inherit ArrayTestBase<TestRecord>(recordInstance) |
64 | | - |
65 | | -type Classes() = |
66 | | - inherit ArrayTestBase<SimpleClass>(SimpleClass( |
67 | | - Name = "sample", |
68 | | - Thing = Some true, |
69 | | - Time = DateTimeOffset.UnixEpoch.AddDays(200.) |
70 | | - )) |
71 | | - |
72 | | - |
73 | | -type ReflectionComparison() = |
74 | | - |
75 | | - [<Params(10, 100, 1000)>] |
76 | | - member val Iterations = 0 with get, set |
77 | | - |
78 | | - [<Benchmark>] |
79 | | - member this.FSharpUnion() = |
80 | | - for i in 0 .. this.Iterations do |
81 | | - FSharpType.IsUnion(typeof<bool option>, true) |> ignore |
82 | | - |
83 | | - [<Benchmark>] |
84 | | - member this.FSharpUnionCached() = |
85 | | - for i in 0 .. this.Iterations do |
86 | | - TypeCache.isUnion typeof<bool option> |> ignore |
87 | | - |
88 | | - [<Benchmark>] |
89 | | - member this.FSharpRecord() = |
90 | | - for i in 0 .. this.Iterations do |
91 | | - Reflection.FSharpType.IsRecord(typeof<TestRecord>, true) |> ignore |
92 | | - |
93 | | - [<Benchmark>] |
94 | | - member this.FSharpRecordCached() = |
95 | | - for i in 0 .. this.Iterations do |
96 | | - TypeCache.isRecord typeof<TestRecord> |> ignore |
97 | | - |
98 | | - |
99 | | -let config = |
100 | | - ManualConfig |
101 | | - .Create(DefaultConfig.Instance) |
102 | | - .AddDiagnoser(MemoryDiagnoser.Default) |
103 | | - .AddExporter(MarkdownExporter.GitHub) |
104 | | - .AddValidator(ExecutionValidator.FailOnError) |
105 | | - |
106 | | -let defaultSwitch () = |
107 | | - BenchmarkSwitcher( |
108 | | - [| typeof<Records> |
109 | | - typeof<Classes> |
110 | | - typeof<ReflectionComparison> |] |
111 | | - ) |
112 | | - |
113 | | - |
114 | | -[<EntryPoint>] |
115 | | -let main argv = |
116 | | - let _summary = defaultSwitch().Run(argv, config) |
117 | | - 0 |
| 1 | +module FSharp.SystemTextJson.Benchmarks |
| 2 | + |
| 3 | +open FSharp.Reflection |
| 4 | + |
| 5 | +open System |
| 6 | +open BenchmarkDotNet.Attributes |
| 7 | +open BenchmarkDotNet.Diagnosers |
| 8 | +open BenchmarkDotNet.Configs |
| 9 | +open BenchmarkDotNet.Jobs |
| 10 | +open BenchmarkDotNet.Running |
| 11 | +open BenchmarkDotNet.Validators |
| 12 | +open BenchmarkDotNet.Exporters |
| 13 | +open BenchmarkDotNet.Environments |
| 14 | +open System.Reflection |
| 15 | +open BenchmarkDotNet.Configs |
| 16 | + |
| 17 | +open System.Text.Json |
| 18 | +open System.Text.Json.Serialization |
| 19 | +open Newtonsoft.Json |
| 20 | +open Newtonsoft.Json.Linq |
| 21 | + |
| 22 | +type TestRecord = |
| 23 | + { name: string |
| 24 | + thing: bool option |
| 25 | + time: System.DateTimeOffset } |
| 26 | + |
| 27 | +type SimpleClass() = |
| 28 | + member val Name: string = null with get, set |
| 29 | + member val Thing: bool option = None with get, set |
| 30 | + member val Time: DateTimeOffset = DateTimeOffset.MinValue with get, set |
| 31 | + |
| 32 | +type ArrayTestBase<'t>(instance: 't) = |
| 33 | + let systemTextOptions = |
| 34 | + let options = JsonSerializerOptions() |
| 35 | + options.Converters.Add(JsonFSharpConverter()) |
| 36 | + options |
| 37 | + |
| 38 | + |
| 39 | + [<Params(10, 100)>] |
| 40 | + member val ArrayLength = 0 with get, set |
| 41 | + |
| 42 | + member val InstanceArray = [||] with get, set |
| 43 | + |
| 44 | + [<GlobalSetup>] |
| 45 | + member this.InitArray() = |
| 46 | + this.InstanceArray <- Array.replicate this.ArrayLength instance |
| 47 | + |
| 48 | + [<Benchmark>] |
| 49 | + member this.Newtonsoft() = |
| 50 | + JsonConvert.SerializeObject this.InstanceArray |
| 51 | + |
| 52 | + [<Benchmark>] |
| 53 | + member this.SystemTextJson() = |
| 54 | + System.Text.Json.JsonSerializer.Serialize(this.InstanceArray, systemTextOptions) |
| 55 | + |
| 56 | +let recordInstance = |
| 57 | + { name = "sample" |
| 58 | + thing = Some true |
| 59 | + time = System.DateTimeOffset.UnixEpoch.AddDays(200.) } |
| 60 | + |
| 61 | + |
| 62 | +type Records() = |
| 63 | + inherit ArrayTestBase<TestRecord>(recordInstance) |
| 64 | + |
| 65 | +type Classes() = |
| 66 | + inherit |
| 67 | + ArrayTestBase<SimpleClass>( |
| 68 | + SimpleClass(Name = "sample", Thing = Some true, Time = DateTimeOffset.UnixEpoch.AddDays(200.)) |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +type ReflectionComparison() = |
| 73 | + |
| 74 | + [<Params(10, 100, 1000)>] |
| 75 | + member val Iterations = 0 with get, set |
| 76 | + |
| 77 | + [<Benchmark>] |
| 78 | + member this.FSharpUnion() = |
| 79 | + for i in 0 .. this.Iterations do |
| 80 | + FSharpType.IsUnion(typeof<bool option>, true) |> ignore |
| 81 | + |
| 82 | + [<Benchmark>] |
| 83 | + member this.FSharpUnionCached() = |
| 84 | + for i in 0 .. this.Iterations do |
| 85 | + TypeCache.isUnion typeof<bool option> |> ignore |
| 86 | + |
| 87 | + [<Benchmark>] |
| 88 | + member this.FSharpRecord() = |
| 89 | + for i in 0 .. this.Iterations do |
| 90 | + Reflection.FSharpType.IsRecord(typeof<TestRecord>, true) |> ignore |
| 91 | + |
| 92 | + [<Benchmark>] |
| 93 | + member this.FSharpRecordCached() = |
| 94 | + for i in 0 .. this.Iterations do |
| 95 | + TypeCache.isRecord typeof<TestRecord> |> ignore |
| 96 | + |
| 97 | + |
| 98 | +let config = |
| 99 | + ManualConfig |
| 100 | + .Create(DefaultConfig.Instance) |
| 101 | + .AddDiagnoser(MemoryDiagnoser.Default) |
| 102 | + .AddExporter(MarkdownExporter.GitHub) |
| 103 | + .AddValidator(ExecutionValidator.FailOnError) |
| 104 | + |
| 105 | +let defaultSwitch () = |
| 106 | + BenchmarkSwitcher( |
| 107 | + [| typeof<Records> |
| 108 | + typeof<Classes> |
| 109 | + typeof<ReflectionComparison> |] |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +[<EntryPoint>] |
| 114 | +let main argv = |
| 115 | + let _summary = defaultSwitch().Run(argv, config) |
| 116 | + 0 |
0 commit comments