Skip to content

Commit be42aed

Browse files
author
Jeff Treuting
committed
POC for loading assembly and running tests on a couple methods of the users choosing
1 parent 9f9bf8a commit be42aed

8 files changed

Lines changed: 282 additions & 1 deletion

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("SharpBenchmark.ExampleDll")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("SharpBenchmark.ExampleDll")]
13+
[assembly: AssemblyCopyright("Copyright © 2013")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("6b46f92e-0bdc-4208-b4a0-7ad34ad2d567")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{0ACEB135-2314-4A6F-A9B6-3D2EF5B013B9}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>SharpBenchmark.ExampleDll</RootNamespace>
11+
<AssemblyName>SharpBenchmark.ExampleDll</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<ItemGroup>
33+
<Reference Include="System" />
34+
<Reference Include="System.Core" />
35+
<Reference Include="System.Xml.Linq" />
36+
<Reference Include="System.Data.DataSetExtensions" />
37+
<Reference Include="Microsoft.CSharp" />
38+
<Reference Include="System.Data" />
39+
<Reference Include="System.Xml" />
40+
</ItemGroup>
41+
<ItemGroup>
42+
<Compile Include="Test1.cs" />
43+
<Compile Include="Properties\AssemblyInfo.cs" />
44+
</ItemGroup>
45+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
46+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
47+
Other similar extension points exist, see Microsoft.Common.targets.
48+
<Target Name="BeforeBuild">
49+
</Target>
50+
<Target Name="AfterBuild">
51+
</Target>
52+
-->
53+
</Project>

SharpBenchmark.ExampleDll/Test1.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Threading;
2+
3+
namespace SharpBenchmark.ExampleDll
4+
{
5+
public class Test1
6+
{
7+
public void Algorithm1()
8+
{
9+
Thread.Sleep(125);
10+
}
11+
12+
public void Algorithm2()
13+
{
14+
Thread.Sleep(235);
15+
}
16+
}
17+
18+
public class Test2
19+
{
20+
public void Algorithm1(int delay)
21+
{
22+
Thread.Sleep(delay);
23+
}
24+
25+
public void Algorithm2(int delay)
26+
{
27+
Thread.Sleep(delay);
28+
}
29+
}
30+
}

SharpBenchmark.Samples/App.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
<startup>
44
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
55
</startup>
6+
<appSettings>
7+
<add key="PathToAssembly" value="C:\Projects\SharpBenchmark\SharpBenchmark.ExampleDll\bin\Debug\SharpBenchmark.ExampleDll.dll"/>
8+
</appSettings>
69
</configuration>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using System.Reflection;
6+
7+
namespace SharpBenchmark.Samples
8+
{
9+
public class DelegateBuilder
10+
{
11+
public static T BuildDelegate<T>(MethodInfo method, params object[] missingParamValues)
12+
{
13+
var queueMissingParams = new Queue<object>(missingParamValues);
14+
15+
var dgtMi = typeof(T).GetMethod("Invoke");
16+
var dgtRet = dgtMi.ReturnType;
17+
var dgtParams = dgtMi.GetParameters();
18+
19+
var paramsOfDelegate = dgtParams
20+
.Select(tp => Expression.Parameter(tp.ParameterType, tp.Name))
21+
.ToArray();
22+
23+
var methodParams = method.GetParameters();
24+
25+
if (method.IsStatic)
26+
{
27+
var paramsToPass = methodParams
28+
.Select((p, i) => CreateParam(paramsOfDelegate, i, p, queueMissingParams))
29+
.ToArray();
30+
31+
var expr = Expression.Lambda<T>(
32+
Expression.Call(method, paramsToPass),
33+
paramsOfDelegate);
34+
35+
return expr.Compile();
36+
}
37+
else
38+
{
39+
var paramThis = Expression.Convert(paramsOfDelegate[0], method.DeclaringType);
40+
41+
var paramsToPass = methodParams
42+
.Select((p, i) => CreateParam(paramsOfDelegate, i + 1, p, queueMissingParams))
43+
.ToArray();
44+
45+
var expr = Expression.Lambda<T>(
46+
Expression.Call(paramThis, method, paramsToPass),
47+
paramsOfDelegate);
48+
49+
return expr.Compile();
50+
}
51+
}
52+
53+
private static Expression CreateParam(ParameterExpression[] paramsOfDelegate, int i, ParameterInfo callParamType, Queue<object> queueMissingParams)
54+
{
55+
if (i < paramsOfDelegate.Length)
56+
return Expression.Convert(paramsOfDelegate[i], callParamType.ParameterType);
57+
58+
if (queueMissingParams.Count > 0)
59+
return Expression.Constant(queueMissingParams.Dequeue());
60+
61+
if (callParamType.ParameterType.IsValueType)
62+
return Expression.Constant(Activator.CreateInstance(callParamType.ParameterType));
63+
64+
return Expression.Constant(null);
65+
}
66+
}
67+
}

SharpBenchmark.Samples/Program.cs

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Configuration;
23
using System.Linq;
34

45
namespace SharpBenchmark.Samples
@@ -9,14 +10,23 @@ static void Main(string[] args)
910
{
1011
var allSamples = typeof(ISample).Assembly.GetTypes();
1112

13+
14+
15+
1216
while (true)
1317
{
14-
Console.Write("Which sample would you like to run? (enter q to quit): ");
18+
Console.Write("Which sample would you like to run? (enter q to quit, enter load to load a dll): ");
1519
var input = Console.ReadLine();
1620

1721
if (input == "q" || input == null)
1822
break;
1923

24+
if (input == "load")
25+
{
26+
LoadDll();
27+
continue;
28+
}
29+
2030
var parts = input.Split(' ');
2131
var sampleNum = 0;
2232
if (!Int32.TryParse(parts[0], out sampleNum))
@@ -42,5 +52,79 @@ static void Main(string[] args)
4252
sample.Execute(tmp.ToArray());
4353
}
4454
}
55+
56+
static void LoadDll()
57+
{
58+
Console.WriteLine("Full Path to DLL (or blank to use testing DLL): ");
59+
var path = Console.ReadLine();
60+
//Console.WriteLine("Full Path to DLL: ");
61+
if (String.IsNullOrEmpty(path))
62+
path = @"C:\Projects\SharpBenchmark\SharpBenchmark.ExampleDll\bin\Debug\SharpBenchmark.ExampleDll.dll";
63+
64+
if (!String.IsNullOrEmpty(path))
65+
{
66+
var dll = System.Reflection.Assembly.LoadFrom(path);
67+
68+
var types = dll.GetTypes();
69+
70+
Console.WriteLine("Types in the DLL");
71+
Console.WriteLine("-------------------------");
72+
73+
var i = 1;
74+
foreach (var type in types)
75+
{
76+
Console.WriteLine("{0}) {1} ", i, type.FullName);
77+
78+
i++;
79+
}
80+
81+
Console.WriteLine();
82+
Console.WriteLine("Enter the number of the type you'd like to use: ");
83+
var input = Console.ReadLine();
84+
85+
var typeNum = Int32.Parse(input);
86+
var selectedType = types[typeNum - 1];
87+
88+
Console.WriteLine();
89+
Console.WriteLine("Methods in this type");
90+
Console.WriteLine("-------------------------");
91+
92+
// get all public methods
93+
i = 1;
94+
var methods = selectedType.GetMethods();
95+
foreach (var method in methods)
96+
{
97+
Console.Write("{0}) {1}(", i, method.Name);
98+
99+
foreach (var pi in method.GetParameters())
100+
{
101+
Console.Write("{0} {1}, ", pi.ParameterType, pi.Name);
102+
}
103+
104+
Console.WriteLine(")");
105+
i++;
106+
}
107+
108+
Console.WriteLine();
109+
Console.WriteLine("Enter the number(s) of the method you'd like to use: ");
110+
input = Console.ReadLine();
111+
112+
var benchmarker = new Benchmarker();
113+
114+
foreach (var methodNum in input.Split(',').Select(x => Int32.Parse(x.Trim())))
115+
{
116+
var selectedMethod = methods[methodNum - 1];
117+
118+
// create an action with this method
119+
// TODO: make this work with methods that have parameters, right now it won't
120+
var newType = Activator.CreateInstance(selectedType);
121+
var del = (Action)Delegate.CreateDelegate(typeof(Action), newType, selectedMethod);
122+
123+
benchmarker.AddTest(selectedMethod.Name, del);
124+
}
125+
126+
benchmarker.RunTests(20);
127+
}
128+
}
45129
}
46130
}

SharpBenchmark.Samples/SharpBenchmark.Samples.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
</PropertyGroup>
3434
<ItemGroup>
3535
<Reference Include="System" />
36+
<Reference Include="System.Configuration" />
3637
<Reference Include="System.Core" />
3738
<Reference Include="System.Xml.Linq" />
3839
<Reference Include="System.Data.DataSetExtensions" />
@@ -42,6 +43,7 @@
4243
</ItemGroup>
4344
<ItemGroup>
4445
<Compile Include="Code\Sample2.cs" />
46+
<Compile Include="DelegateBuilder.cs" />
4547
<Compile Include="ISample.cs" />
4648
<Compile Include="Program.cs" />
4749
<Compile Include="Properties\AssemblyInfo.cs" />

SharpBenchmark.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpBenchmark", "SharpBenc
55
EndProject
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpBenchmark.Samples", "SharpBenchmark.Samples\SharpBenchmark.Samples.csproj", "{78A8F10A-B3CA-4C45-8EB7-D5AEE0B7E73F}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpBenchmark.ExampleDll", "SharpBenchmark.ExampleDll\SharpBenchmark.ExampleDll.csproj", "{0ACEB135-2314-4A6F-A9B6-3D2EF5B013B9}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -19,6 +21,10 @@ Global
1921
{78A8F10A-B3CA-4C45-8EB7-D5AEE0B7E73F}.Debug|Any CPU.Build.0 = Debug|Any CPU
2022
{78A8F10A-B3CA-4C45-8EB7-D5AEE0B7E73F}.Release|Any CPU.ActiveCfg = Release|Any CPU
2123
{78A8F10A-B3CA-4C45-8EB7-D5AEE0B7E73F}.Release|Any CPU.Build.0 = Release|Any CPU
24+
{0ACEB135-2314-4A6F-A9B6-3D2EF5B013B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25+
{0ACEB135-2314-4A6F-A9B6-3D2EF5B013B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
26+
{0ACEB135-2314-4A6F-A9B6-3D2EF5B013B9}.Release|Any CPU.ActiveCfg = Release|Any CPU
27+
{0ACEB135-2314-4A6F-A9B6-3D2EF5B013B9}.Release|Any CPU.Build.0 = Release|Any CPU
2228
EndGlobalSection
2329
GlobalSection(SolutionProperties) = preSolution
2430
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)