|
1 | 1 | using BenchmarkDotNet.Attributes;
|
2 | 2 | using Hl7.Fhir.Introspection;
|
3 | 3 | using Hl7.Fhir.Model;
|
| 4 | +using Hl7.Fhir.Utility; |
4 | 5 | using System;
|
| 6 | +using System.Linq; |
5 | 7 |
|
6 |
| -namespace Firely.Sdk.Benchmarks |
| 8 | +namespace Firely.Sdk.Benchmarks; |
| 9 | + |
| 10 | +[MemoryDiagnoser] |
| 11 | +public class ModelInspectorBenchmarks |
7 | 12 | {
|
8 |
| - [MemoryDiagnoser] |
9 |
| - public class ModelInspectorBenchmarks |
| 13 | + [GlobalSetup] |
| 14 | + public void BenchmarkSetup() |
10 | 15 | {
|
11 |
| - [GlobalSetup] |
12 |
| - public void BenchmarkSetup() |
13 |
| - { |
14 |
| - // PropertyInfoExtensions.NoCodeGenSupport = true; |
15 |
| - } |
| 16 | + // PropertyInfoExtensions.NoCodeGenSupport = true; |
16 | 17 |
|
| 18 | + var inspector = ScanAssemblies(); |
| 19 | + Console.WriteLine($"ScanAssemblies: Types: {inspector.ClassMappings.Count}, Enums: {inspector.EnumMappings.Count()}"); |
17 | 20 |
|
18 |
| - internal Type[] PopularResources = new Type[] |
19 |
| - { |
20 |
| - typeof(Observation), typeof(Patient), typeof(Organization), |
21 |
| - typeof(Procedure), typeof(StructureDefinition), typeof(MedicationRequest), |
22 |
| - typeof(ValueSet), typeof(Questionnaire), typeof(Appointment), |
23 |
| - typeof(OperationOutcome) |
24 |
| - }; |
25 |
| - |
26 |
| - [Benchmark] |
27 |
| - public void ScanAssemblies() |
28 |
| - { |
29 |
| - // Make sure we work uncached initially on each run |
30 |
| - //ModelInspector.Clear(); |
31 |
| - //ClassMapping.Clear(); |
| 21 | + inspector = ImportTypeAllResources(); |
| 22 | + Console.WriteLine($"ImportTypeAllResources: Types: {inspector.ClassMappings.Count}, Enums: {inspector.EnumMappings.Count()}"); |
| 23 | + |
| 24 | + inspector = NewWithSourceGenMappings(); |
| 25 | + Console.WriteLine($"NewWithSourceGenMappings: Types: {inspector.ClassMappings.Count}, Enums: {inspector.EnumMappings.Count()}"); |
| 26 | + |
| 27 | + inspector = NewWithTypesAllResources(); |
| 28 | + Console.WriteLine($"NewWithTypesAllResources: Types: {inspector.ClassMappings.Count}, Enums: {inspector.EnumMappings.Count()}"); |
| 29 | + |
| 30 | + inspector = ImportType4Resources(); |
| 31 | + Console.WriteLine($"ImportType4Resources: Types: {inspector.ClassMappings.Count}, Enums: {inspector.EnumMappings.Count()}"); |
| 32 | + |
| 33 | + inspector = NewWithTypes4Resources(); |
| 34 | + Console.WriteLine($"NewWithTypes4Resources: Types: {inspector.ClassMappings.Count}, Enums: {inspector.EnumMappings.Count()}"); |
| 35 | + } |
| 36 | + |
| 37 | + internal static readonly Type[] PopularResources = new Type[] |
| 38 | + { |
| 39 | + typeof(Observation), typeof(Patient), typeof(Organization), |
| 40 | + typeof(Procedure), typeof(StructureDefinition), typeof(MedicationRequest), |
| 41 | + typeof(ValueSet), typeof(Questionnaire), typeof(Appointment), |
| 42 | + typeof(OperationOutcome) |
| 43 | + }; |
| 44 | + |
| 45 | + |
| 46 | + internal static readonly Type[] TestResources = |
| 47 | + [ |
| 48 | + typeof(CapabilityStatement), typeof(Appointment), typeof(OperationDefinition), |
| 49 | + ]; |
32 | 50 |
|
33 |
| - _ = ModelInspector.ForAssembly(typeof(ModelInfo).Assembly); |
| 51 | + //[IterationSetup] |
| 52 | + //[IterationCleanup] |
| 53 | + public void ResetCache() |
| 54 | + { |
| 55 | + // Make sure we work uncached initially on each run |
| 56 | + ModelInspector.Clear(); |
| 57 | + ClassMapping.Clear(); |
| 58 | + EnumMapping.Clear(); |
| 59 | + } |
| 60 | + |
| 61 | + [Benchmark] |
| 62 | + public ModelInspector ScanAssemblies() |
| 63 | + { |
| 64 | + ResetCache(); |
| 65 | + return ModelInspector.ForAssembly(typeof(ModelInfo).Assembly); |
| 66 | + } |
| 67 | + |
| 68 | + [Benchmark] |
| 69 | + public ModelInspector ImportTypeAllResources() |
| 70 | + { |
| 71 | + ResetCache(); |
| 72 | + var inspector = new ModelInspector(Hl7.Fhir.Specification.FhirRelease.R5); |
| 73 | + foreach (var t in ModelInfo.GenerateAllFhirTypes()) |
| 74 | + { |
| 75 | + inspector.ImportType(t); |
34 | 76 | }
|
35 | 77 |
|
36 |
| - [Benchmark] |
37 |
| - public void GetPropertiesPopular() |
| 78 | + return inspector; |
| 79 | + } |
| 80 | + |
| 81 | + [Benchmark] |
| 82 | + public ModelInspector ImportType4Resources() |
| 83 | + { |
| 84 | + ResetCache(); |
| 85 | + var inspector = new ModelInspector(Hl7.Fhir.Specification.FhirRelease.R5); //ModelInspector.ForAssembly(typeof(ModelInfo).Assembly); |
| 86 | + foreach (var t in TestResources) |
38 | 87 | {
|
39 |
| - // Make sure we work uncached initially on each run |
40 |
| - //ModelInspector.Clear(); |
41 |
| - //ClassMapping.Clear(); |
42 |
| - |
43 |
| - var inspector = ModelInspector.ForAssembly(typeof(ModelInfo).Assembly); |
44 |
| - foreach (var t in PopularResources) |
45 |
| - { |
46 |
| - var mapping = inspector.FindClassMapping(t); |
47 |
| - _ = mapping.PropertyMappings; |
48 |
| - } |
| 88 | + inspector.ImportType(t); |
49 | 89 | }
|
50 | 90 |
|
51 |
| - //[Benchmark] |
52 |
| - //public void GetPropertiesAll() |
53 |
| - //{ |
54 |
| - // // Make sure we work uncached initially on each run |
55 |
| - // ModelInspector.Clear(); |
56 |
| - // ClassMapping.Clear(); |
| 91 | + return inspector; |
| 92 | + } |
57 | 93 |
|
58 |
| - // var inspector = ModelInspector.ForAssembly(typeof(ModelInfo).Assembly); |
59 |
| - // foreach (var m in inspector.ClassMappings) |
60 |
| - // { |
61 |
| - // _ = m.PropertyMappings; |
62 |
| - // } |
63 |
| - //} |
| 94 | + [Benchmark] |
| 95 | + public ModelInspector NewWithSourceGenMappings() |
| 96 | + { |
| 97 | + FhirReleaseParser.Parse(ModelInfo.Version); |
| 98 | + ResetCache(); |
| 99 | + return new ModelInspector(ModelInfo.GenerateAllClassMappings(), ModelInfo.GenerateAllEnumMappings()) { FhirRelease = Hl7.Fhir.Specification.FhirRelease.R5 }; |
| 100 | + } |
| 101 | + |
| 102 | + [Benchmark] |
| 103 | + public ModelInspector NewWithTypesAllResources() |
| 104 | + { |
| 105 | + FhirReleaseParser.Parse(ModelInfo.Version); |
| 106 | + ResetCache(); |
| 107 | + return ModelInspector.ForTypes(ModelInfo.Version, ModelInfo.GenerateAllFhirTypes()); |
| 108 | + } |
64 | 109 |
|
| 110 | + [Benchmark] |
| 111 | + public ModelInspector NewWithTypes4Resources() |
| 112 | + { |
| 113 | + ResetCache(); |
| 114 | + var inspector = ModelInspector.ForTypes(ModelInfo.Version, [ |
| 115 | + typeof(CapabilityStatement), |
| 116 | + typeof(CapabilityStatement.ConditionalDeleteStatus), |
| 117 | + typeof(CapabilityStatement.ConditionalReadStatus), |
| 118 | + typeof(CapabilityStatement.DocumentMode), |
| 119 | + typeof(CapabilityStatement.EventCapabilityMode), |
| 120 | + typeof(CapabilityStatement.ReferenceHandlingPolicy), |
| 121 | + typeof(CapabilityStatement.ResourceVersionPolicy), |
| 122 | + typeof(CapabilityStatement.RestfulCapabilityMode), |
| 123 | + typeof(CapabilityStatement.SystemRestfulInteraction), |
| 124 | + typeof(CapabilityStatement.TypeRestfulInteraction), |
| 125 | + typeof(CapabilityStatement.DocumentComponent), |
| 126 | + typeof(CapabilityStatement.EndpointComponent), |
| 127 | + typeof(CapabilityStatement.ImplementationComponent), |
| 128 | + typeof(CapabilityStatement.MessagingComponent), |
| 129 | + typeof(CapabilityStatement.OperationComponent), |
| 130 | + typeof(CapabilityStatement.ResourceComponent), |
| 131 | + typeof(CapabilityStatement.ResourceInteractionComponent), |
| 132 | + typeof(CapabilityStatement.RestComponent), |
| 133 | + typeof(CapabilityStatement.SearchParamComponent), |
| 134 | + typeof(CapabilityStatement.SecurityComponent), |
| 135 | + typeof(CapabilityStatement.SoftwareComponent), |
| 136 | + typeof(CapabilityStatement.SupportedMessageComponent), |
| 137 | + typeof(CapabilityStatement.SystemInteractionComponent), |
| 138 | + typeof(Appointment), |
| 139 | + typeof(Appointment.AppointmentStatus), |
| 140 | + typeof(Appointment.IANATimezones), |
| 141 | + typeof(Appointment.ParticipationStatus), |
| 142 | + typeof(Appointment.WeekOfMonth), |
| 143 | + typeof(Appointment.MonthlyTemplateComponent), |
| 144 | + typeof(Appointment.ParticipantComponent), |
| 145 | + typeof(Appointment.RecurrenceTemplateComponent), |
| 146 | + typeof(Appointment.WeeklyTemplateComponent), |
| 147 | + typeof(Appointment.YearlyTemplateComponent), |
| 148 | + typeof(OperationDefinition), |
| 149 | + typeof(OperationDefinition.BindingComponent), |
| 150 | + typeof(OperationDefinition.OperationKind), |
| 151 | + typeof(OperationDefinition.OperationParameterScope), |
| 152 | + typeof(OperationDefinition.OverloadComponent), |
| 153 | + typeof(OperationDefinition.ParameterComponent), |
| 154 | + typeof(OperationDefinition.ReferencedFromComponent), |
| 155 | + ]); |
| 156 | + return inspector; |
65 | 157 | }
|
| 158 | + |
| 159 | + //[Benchmark] |
| 160 | + //public void GetPropertiesAll() |
| 161 | + //{ |
| 162 | + // // Make sure we work uncached initially on each run |
| 163 | + // ModelInspector.Clear(); |
| 164 | + // ClassMapping.Clear(); |
| 165 | + |
| 166 | + // var inspector = ModelInspector.ForAssembly(typeof(ModelInfo).Assembly); |
| 167 | + // foreach (var m in inspector.ClassMappings) |
| 168 | + // { |
| 169 | + // _ = m.PropertyMappings; |
| 170 | + // } |
| 171 | + //} |
| 172 | + |
66 | 173 | }
|
0 commit comments