Advertisement
Unchpokable

Ulearn "Experiments" prectic + stupid factory pattern impl

Dec 26th, 2020
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace StructBenchmarking
  5. {
  6.     public interface IFactory
  7.     {
  8.         object CreateInstanceOf(Type instanceType, string requiredInterfaceName, params object[] constructArgs);
  9.  
  10.         object CreateInstanceOf(Type instanceType, params object[] constructArgs);
  11.     }
  12.  
  13.     public interface IFactory<T>
  14.     {
  15.         T CreateInstanceOf(Type instanceType, string requiredInterfaceName, params object[] constructArgs);
  16.  
  17.         T CreateInstanceOf(Type instanceType, params object[] constructArgs);
  18.     }
  19.  
  20.     public class Factory<T> : IFactory, IFactory<T>
  21.     {
  22.         object IFactory.CreateInstanceOf(Type instanceType, string requiredInterfaceName, params object[] constructArgs)
  23.         {
  24.             if (instanceType.GetInterface(requiredInterfaceName) == null)
  25.                 throw new ArgumentException($"Type {instanceType} doesn't implements interface {requiredInterfaceName}");
  26.             return Activator.CreateInstance(instanceType, constructArgs);
  27.         }
  28.  
  29.         object IFactory.CreateInstanceOf(Type instanceType, params object[] constructArgs)
  30.         {
  31.             return Activator.CreateInstance(instanceType, constructArgs);
  32.         }
  33.  
  34.         public T CreateInstanceOf(Type instanceType, string requiredInterfaceName, params object[] constructArgs)
  35.         {
  36.             if (instanceType.GetInterface(requiredInterfaceName) == null)
  37.                 throw new ArgumentException($"Type {instanceType} doesn't implements interface {requiredInterfaceName}");
  38.             return CreateInstanceOf(instanceType, constructArgs);
  39.         }
  40.  
  41.         public T CreateInstanceOf(Type instanceType, params object[] constructArgs)
  42.         {
  43.             var instance = Activator.CreateInstance(instanceType, constructArgs);
  44.             if (typeof(T).IsInstanceOfType(instance))
  45.                 return (T)instance;
  46.             throw new ArgumentException($"Can't create instance of {instanceType}, cause {instanceType} is not {typeof(T)} or {typeof(T)}'s derivate type");
  47.         }
  48.     }
  49.  
  50.     public class Experiments
  51.     {
  52.         public static ChartData BuildChartDataForArrayCreation(
  53.             IBenchmark benchmark, int repetitionsCount)
  54.         {
  55.             return BuildChart(benchmark,
  56.                 typeof(ClassArrayCreationTask),
  57.                 typeof(StructArrayCreationTask),
  58.                 "Create array",
  59.                 repetitionsCount);
  60.         }
  61.  
  62.         public static ChartData BuildChartDataForMethodCall(
  63.             IBenchmark benchmark, int repetitionsCount)
  64.         {
  65.             return BuildChart(benchmark,
  66.                 typeof(MethodCallWithClassArgumentTask),
  67.                 typeof(MethodCallWithStructArgumentTask),
  68.                 "Call method with argument",
  69.                 repetitionsCount);
  70.         }
  71.  
  72.         private static ChartData BuildChart(IBenchmark benchmark,
  73.                                             Type clsTaskType,
  74.                                             Type structTaskType,
  75.                                             string chartTitle,
  76.                                             int timerRepeatCount)
  77.         {
  78.             if (clsTaskType.GetInterface("ITask") == null || structTaskType.GetInterface("ITask") == null)
  79.                 throw new ArgumentException(
  80.                 $"Type {clsTaskType} doesn't implements interface ITask and can not use with any IBenchmark benchmarks");
  81.             if (structTaskType.GetInterface("ITask") == null)
  82.                 throw new ArgumentException(
  83.                 $"Type {structTaskType} doesn't implements interface ITask and can not use with any IBenchmark benchmarks");
  84.  
  85.             var clsTime = ExecuteExperiment(benchmark, clsTaskType, timerRepeatCount);
  86.             var structTime = ExecuteExperiment(benchmark, structTaskType, timerRepeatCount);
  87.             return new ChartData()
  88.             {
  89.                 Title = chartTitle,
  90.                 ClassPoints = clsTime,
  91.                 StructPoints = structTime
  92.             };
  93.         }
  94.  
  95.         private static List<ExperimentResult> ExecuteExperiment(
  96.                     IBenchmark benchmark, Type task, int repeat)
  97.         {
  98.             if (task.GetInterface("ITask") == null)
  99.                 throw new ArgumentException(
  100.                 $"Cant Run benchmarking with type {task}, cause this type dont implement 'ITask' interface");
  101.  
  102.             var result = new List<ExperimentResult>();
  103.             var factory = new Factory<ITask>();
  104.             foreach (var size in Constants.FieldCounts)
  105.             {
  106.                 result.Add(
  107.                     new ExperimentResult(
  108.                         size,
  109.                         benchmark.MeasureDurationInMs(factory.CreateInstanceOf(task, size),
  110.                         repeat)));
  111.             }
  112.             return result;
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement