Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace StructBenchmarking
- {
- public interface IFactory
- {
- object CreateInstanceOf(Type instanceType, string requiredInterfaceName, params object[] constructArgs);
- object CreateInstanceOf(Type instanceType, params object[] constructArgs);
- }
- public interface IFactory<T>
- {
- T CreateInstanceOf(Type instanceType, string requiredInterfaceName, params object[] constructArgs);
- T CreateInstanceOf(Type instanceType, params object[] constructArgs);
- }
- public class Factory<T> : IFactory, IFactory<T>
- {
- object IFactory.CreateInstanceOf(Type instanceType, string requiredInterfaceName, params object[] constructArgs)
- {
- if (instanceType.GetInterface(requiredInterfaceName) == null)
- throw new ArgumentException($"Type {instanceType} doesn't implements interface {requiredInterfaceName}");
- return Activator.CreateInstance(instanceType, constructArgs);
- }
- object IFactory.CreateInstanceOf(Type instanceType, params object[] constructArgs)
- {
- return Activator.CreateInstance(instanceType, constructArgs);
- }
- public T CreateInstanceOf(Type instanceType, string requiredInterfaceName, params object[] constructArgs)
- {
- if (instanceType.GetInterface(requiredInterfaceName) == null)
- throw new ArgumentException($"Type {instanceType} doesn't implements interface {requiredInterfaceName}");
- return CreateInstanceOf(instanceType, constructArgs);
- }
- public T CreateInstanceOf(Type instanceType, params object[] constructArgs)
- {
- var instance = Activator.CreateInstance(instanceType, constructArgs);
- if (typeof(T).IsInstanceOfType(instance))
- return (T)instance;
- throw new ArgumentException($"Can't create instance of {instanceType}, cause {instanceType} is not {typeof(T)} or {typeof(T)}'s derivate type");
- }
- }
- public class Experiments
- {
- public static ChartData BuildChartDataForArrayCreation(
- IBenchmark benchmark, int repetitionsCount)
- {
- return BuildChart(benchmark,
- typeof(ClassArrayCreationTask),
- typeof(StructArrayCreationTask),
- "Create array",
- repetitionsCount);
- }
- public static ChartData BuildChartDataForMethodCall(
- IBenchmark benchmark, int repetitionsCount)
- {
- return BuildChart(benchmark,
- typeof(MethodCallWithClassArgumentTask),
- typeof(MethodCallWithStructArgumentTask),
- "Call method with argument",
- repetitionsCount);
- }
- private static ChartData BuildChart(IBenchmark benchmark,
- Type clsTaskType,
- Type structTaskType,
- string chartTitle,
- int timerRepeatCount)
- {
- if (clsTaskType.GetInterface("ITask") == null || structTaskType.GetInterface("ITask") == null)
- throw new ArgumentException(
- $"Type {clsTaskType} doesn't implements interface ITask and can not use with any IBenchmark benchmarks");
- if (structTaskType.GetInterface("ITask") == null)
- throw new ArgumentException(
- $"Type {structTaskType} doesn't implements interface ITask and can not use with any IBenchmark benchmarks");
- var clsTime = ExecuteExperiment(benchmark, clsTaskType, timerRepeatCount);
- var structTime = ExecuteExperiment(benchmark, structTaskType, timerRepeatCount);
- return new ChartData()
- {
- Title = chartTitle,
- ClassPoints = clsTime,
- StructPoints = structTime
- };
- }
- private static List<ExperimentResult> ExecuteExperiment(
- IBenchmark benchmark, Type task, int repeat)
- {
- if (task.GetInterface("ITask") == null)
- throw new ArgumentException(
- $"Cant Run benchmarking with type {task}, cause this type dont implement 'ITask' interface");
- var result = new List<ExperimentResult>();
- var factory = new Factory<ITask>();
- foreach (var size in Constants.FieldCounts)
- {
- result.Add(
- new ExperimentResult(
- size,
- benchmark.MeasureDurationInMs(factory.CreateInstanceOf(task, size),
- repeat)));
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement