From 4ee3b79034e79585bed71b4a10b3c4eccd45d39a Mon Sep 17 00:00:00 2001 From: Steve Harter Date: Wed, 7 Sep 2022 16:17:06 -0500 Subject: [PATCH 1/3] Reduce randomness of invoke benchmarks --- .../micro/runtime/System.Reflection/Invoke.cs | 90 ++++++++++++++++--- 1 file changed, 76 insertions(+), 14 deletions(-) diff --git a/src/benchmarks/micro/runtime/System.Reflection/Invoke.cs b/src/benchmarks/micro/runtime/System.Reflection/Invoke.cs index 8e3af911129..50b01b52509 100644 --- a/src/benchmarks/micro/runtime/System.Reflection/Invoke.cs +++ b/src/benchmarks/micro/runtime/System.Reflection/Invoke.cs @@ -10,6 +10,8 @@ namespace System.Reflection [BenchmarkCategory(Categories.Runtime, Categories.Reflection)] public class Invoke { + private const int Iterations = 1_000; // Reduce the randomness of these short-lived calls. + private static MyClass s_MyClass = new MyClass(); private static object[] s_args = new object[] { 42, "Hello", default(MyBlittableStruct), s_MyClass }; private static int s_dummy; @@ -24,6 +26,9 @@ public class Invoke private static FieldInfo s_field_int; private static FieldInfo s_field_class; + public static int s_int; + public static object s_class; + [GlobalSetup] public void Setup() { @@ -83,80 +88,137 @@ public void Method0_NoParms() // Include the array allocation and population for a typical scenario. public void StaticMethod4_arrayNotCached_int_string_struct_class() { - object[] args = new object[] { 42, "Hello", default(MyBlittableStruct), s_MyClass }; - s_method_int_string_struct_class.Invoke(null, args); + for (int i = 0; i < Iterations; i++) + { + object[] args = new object[] { 42, "Hello", default(MyBlittableStruct), s_MyClass }; + s_method_int_string_struct_class.Invoke(null, args); + } } [Benchmark] public void StaticMethod4_int_string_struct_class() { - s_method_int_string_struct_class.Invoke(null, s_args); + for (int i = 0; i < Iterations; i++) + { + s_method_int_string_struct_class.Invoke(null, s_args); + } } [Benchmark] public void StaticMethod4_ByRefParams_int_string_struct_class() { - s_method_byref_int_string_struct_class.Invoke(null, s_args); + for (int i = 0; i < Iterations; i++) + { + s_method_byref_int_string_struct_class.Invoke(null, s_args); + } } [Benchmark] public void Ctor0_NoParams() { - s_ctor_NoParams.Invoke(null); + for (int i = 0; i < Iterations; i++) + { + s_ctor_NoParams.Invoke(null); + } } [Benchmark] public void Ctor0_ActivatorCreateInstance_NoParams() { - Activator.CreateInstance(typeof(MyClass)); + for (int i = 0; i < Iterations; i++) + { + Activator.CreateInstance(typeof(MyClass)); + } } [Benchmark] public void Ctor4_int_string_struct_class() { - s_ctor_int_string_struct_class.Invoke(s_args); + for (int i = 0; i < Iterations; i++) + { + s_ctor_int_string_struct_class.Invoke(s_args); + } } [Benchmark] public void Ctor4_ActivatorCreateInstance() { - Activator.CreateInstance(typeof(MyClass), s_args); + for (int i = 0; i < Iterations; i++) + { + Activator.CreateInstance(typeof(MyClass), s_args); + } } [Benchmark] public void Property_Get_int() { - s_property_int.GetValue(s_MyClass); + for (int i = 0; i < Iterations; i++) + { + s_int = (int)s_property_int.GetValue(s_MyClass); + } } [Benchmark] public void Property_Get_class() { - s_property_class.GetValue(s_MyClass); + for (int i = 0; i < Iterations; i++) + { + s_class = s_property_class.GetValue(s_MyClass); + } } [Benchmark] public void Property_Set_int() { - s_property_int.SetValue(s_MyClass, 42); + for (int i = 0; i < 1000; i++) + { + s_property_int.SetValue(s_MyClass, 42); + } } [Benchmark] public void Property_Set_class() { - s_property_class.SetValue(s_MyClass, null); + for (int i = 0; i < Iterations; i++) + { + s_property_class.SetValue(s_MyClass, null); + } } [Benchmark] public void Field_Get_int() { - s_field_int.GetValue(s_MyClass); + for (int i = 0; i < Iterations; i++) + { + s_int = (int)s_field_int.GetValue(s_MyClass); + } + } + + [Benchmark] + public void Field_Get_class() + { + for (int i = 0; i < Iterations; i++) + { + s_class = s_field_class.GetValue(s_MyClass); + } } [Benchmark] public void Field_Set_int() { - s_field_int.SetValue(s_MyClass, 42); + for (int i = 0; i < Iterations; i++) + { + s_field_int.SetValue(s_MyClass, 42); + } + } + + [Benchmark] + public void Field_Set_class() + { + for (int i = 0; i < Iterations; i++) + { + s_field_class.SetValue(s_MyClass, 42); + } } public struct MyBlittableStruct From 186efa919e0e378f532d8d2619c8b84c4712dcb9 Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Sun, 25 Sep 2022 14:07:34 -0600 Subject: [PATCH 2/3] add OPI --- .../micro/runtime/System.Reflection/Invoke.cs | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/benchmarks/micro/runtime/System.Reflection/Invoke.cs b/src/benchmarks/micro/runtime/System.Reflection/Invoke.cs index 50b01b52509..abc3bb297fe 100644 --- a/src/benchmarks/micro/runtime/System.Reflection/Invoke.cs +++ b/src/benchmarks/micro/runtime/System.Reflection/Invoke.cs @@ -78,13 +78,12 @@ public static void Method_nullableInt(int? i) s_dummy++; } - [Benchmark] public void Method0_NoParms() { s_method.Invoke(s_MyClass, null); } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] // Include the array allocation and population for a typical scenario. public void StaticMethod4_arrayNotCached_int_string_struct_class() { @@ -95,7 +94,7 @@ public void StaticMethod4_arrayNotCached_int_string_struct_class() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void StaticMethod4_int_string_struct_class() { for (int i = 0; i < Iterations; i++) @@ -104,7 +103,7 @@ public void StaticMethod4_int_string_struct_class() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void StaticMethod4_ByRefParams_int_string_struct_class() { for (int i = 0; i < Iterations; i++) @@ -113,7 +112,7 @@ public void StaticMethod4_ByRefParams_int_string_struct_class() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Ctor0_NoParams() { for (int i = 0; i < Iterations; i++) @@ -122,7 +121,7 @@ public void Ctor0_NoParams() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Ctor0_ActivatorCreateInstance_NoParams() { for (int i = 0; i < Iterations; i++) @@ -131,7 +130,7 @@ public void Ctor0_ActivatorCreateInstance_NoParams() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Ctor4_int_string_struct_class() { for (int i = 0; i < Iterations; i++) @@ -140,7 +139,7 @@ public void Ctor4_int_string_struct_class() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Ctor4_ActivatorCreateInstance() { for (int i = 0; i < Iterations; i++) @@ -149,7 +148,7 @@ public void Ctor4_ActivatorCreateInstance() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Property_Get_int() { for (int i = 0; i < Iterations; i++) @@ -158,7 +157,7 @@ public void Property_Get_int() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Property_Get_class() { for (int i = 0; i < Iterations; i++) @@ -167,7 +166,7 @@ public void Property_Get_class() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Property_Set_int() { for (int i = 0; i < 1000; i++) @@ -176,7 +175,7 @@ public void Property_Set_int() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Property_Set_class() { for (int i = 0; i < Iterations; i++) @@ -185,7 +184,7 @@ public void Property_Set_class() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Field_Get_int() { for (int i = 0; i < Iterations; i++) @@ -194,7 +193,7 @@ public void Field_Get_int() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Field_Get_class() { for (int i = 0; i < Iterations; i++) @@ -203,7 +202,7 @@ public void Field_Get_class() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Field_Set_int() { for (int i = 0; i < Iterations; i++) @@ -212,7 +211,7 @@ public void Field_Set_int() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Field_Set_class() { for (int i = 0; i < Iterations; i++) From 3e7cc74ebd3eee2ac2a8c25b662205f2145b8c31 Mon Sep 17 00:00:00 2001 From: Steve Harter Date: Thu, 6 Oct 2022 12:24:32 -0500 Subject: [PATCH 3/3] Address feedback --- .../micro/runtime/System.Reflection/Invoke.cs | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/benchmarks/micro/runtime/System.Reflection/Invoke.cs b/src/benchmarks/micro/runtime/System.Reflection/Invoke.cs index 50b01b52509..46bc84a0014 100644 --- a/src/benchmarks/micro/runtime/System.Reflection/Invoke.cs +++ b/src/benchmarks/micro/runtime/System.Reflection/Invoke.cs @@ -78,13 +78,16 @@ public static void Method_nullableInt(int? i) s_dummy++; } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Method0_NoParms() { - s_method.Invoke(s_MyClass, null); + for (int i = 0; i < Iterations; i++) + { + s_method.Invoke(s_MyClass, null); + } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] // Include the array allocation and population for a typical scenario. public void StaticMethod4_arrayNotCached_int_string_struct_class() { @@ -95,7 +98,7 @@ public void StaticMethod4_arrayNotCached_int_string_struct_class() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void StaticMethod4_int_string_struct_class() { for (int i = 0; i < Iterations; i++) @@ -104,7 +107,7 @@ public void StaticMethod4_int_string_struct_class() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void StaticMethod4_ByRefParams_int_string_struct_class() { for (int i = 0; i < Iterations; i++) @@ -113,7 +116,7 @@ public void StaticMethod4_ByRefParams_int_string_struct_class() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Ctor0_NoParams() { for (int i = 0; i < Iterations; i++) @@ -122,7 +125,7 @@ public void Ctor0_NoParams() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Ctor0_ActivatorCreateInstance_NoParams() { for (int i = 0; i < Iterations; i++) @@ -131,7 +134,7 @@ public void Ctor0_ActivatorCreateInstance_NoParams() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Ctor4_int_string_struct_class() { for (int i = 0; i < Iterations; i++) @@ -140,7 +143,7 @@ public void Ctor4_int_string_struct_class() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Ctor4_ActivatorCreateInstance() { for (int i = 0; i < Iterations; i++) @@ -149,7 +152,7 @@ public void Ctor4_ActivatorCreateInstance() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Property_Get_int() { for (int i = 0; i < Iterations; i++) @@ -158,7 +161,7 @@ public void Property_Get_int() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Property_Get_class() { for (int i = 0; i < Iterations; i++) @@ -167,7 +170,7 @@ public void Property_Get_class() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Property_Set_int() { for (int i = 0; i < 1000; i++) @@ -176,7 +179,7 @@ public void Property_Set_int() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Property_Set_class() { for (int i = 0; i < Iterations; i++) @@ -185,7 +188,7 @@ public void Property_Set_class() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Field_Get_int() { for (int i = 0; i < Iterations; i++) @@ -194,7 +197,7 @@ public void Field_Get_int() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Field_Get_class() { for (int i = 0; i < Iterations; i++) @@ -203,7 +206,7 @@ public void Field_Get_class() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Field_Set_int() { for (int i = 0; i < Iterations; i++) @@ -212,7 +215,7 @@ public void Field_Set_int() } } - [Benchmark] + [Benchmark(OperationsPerInvoke = Iterations)] public void Field_Set_class() { for (int i = 0; i < Iterations; i++)