Skip to content

Adds performance tests, that compare to published NuGet #975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ indent_size = 2
[*.{csproj,pyproj,config}]
indent_size = 2

# .NET formatting settings
[*.{cs,vb}]
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = true

[*.cs]
csharp_new_line_before_open_brace = true
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true

# Solution
[*.sln]
indent_style = tab
Expand Down
191 changes: 184 additions & 7 deletions pythonnet.15.sln

Large diffs are not rendered by default.

69 changes: 69 additions & 0 deletions src/perf_tests/BaselineComparisonBenchmarkBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

using Python.Runtime;

namespace Python.PerformanceTests
{
public class BaselineComparisonBenchmarkBase
{
public BaselineComparisonBenchmarkBase()
{
Console.WriteLine($"CWD: {Environment.CurrentDirectory}");
Console.WriteLine($"Using Python.Runtime from {typeof(PythonEngine).Assembly.Location} {typeof(PythonEngine).Assembly.GetName()}");

try {
PythonEngine.Initialize();
Console.WriteLine("Python Initialized");
if (PythonEngine.BeginAllowThreads() == IntPtr.Zero)
throw new PythonException();
Console.WriteLine("Threading enabled");
}
catch (Exception e) {
Console.WriteLine(e);
throw;
}
}

static BaselineComparisonBenchmarkBase()
{
string pythonRuntimeDll = Environment.GetEnvironmentVariable(BaselineComparisonConfig.EnvironmentVariableName);
if (string.IsNullOrEmpty(pythonRuntimeDll))
{
throw new ArgumentException(
"Required environment variable is missing",
BaselineComparisonConfig.EnvironmentVariableName);
}

Console.WriteLine("Preloading " + pythonRuntimeDll);
Assembly.LoadFrom(pythonRuntimeDll);
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) {
if (assembly.FullName.StartsWith("Python.Runtime"))
Console.WriteLine(assembly.Location);
foreach(var dependency in assembly.GetReferencedAssemblies())
if (dependency.FullName.Contains("Python.Runtime")) {
Console.WriteLine($"{assembly} -> {dependency}");
}
}

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainOnAssemblyResolve;
}

static Assembly CurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args) {
if (!args.Name.StartsWith("Python.Runtime"))
return null;

var preloaded = AppDomain.CurrentDomain.GetAssemblies()
.FirstOrDefault(a => a.GetName().Name == "Python.Runtime");
if (preloaded != null) return preloaded;

string pythonRuntimeDll = Environment.GetEnvironmentVariable(BaselineComparisonConfig.EnvironmentVariableName);
if (string.IsNullOrEmpty(pythonRuntimeDll))
return null;

return Assembly.LoadFrom(pythonRuntimeDll);
}
}
}
47 changes: 47 additions & 0 deletions src/perf_tests/BaselineComparisonConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;

namespace Python.PerformanceTests
{
public class BaselineComparisonConfig : ManualConfig
{
public const string EnvironmentVariableName = "PythonRuntimeDLL";

public BaselineComparisonConfig()
{
this.Options |= ConfigOptions.DisableOptimizationsValidator;

string deploymentRoot = BenchmarkTests.DeploymentRoot;

var baseJob = Job.Default;
this.Add(baseJob
.WithId("baseline")
.WithEnvironmentVariable(EnvironmentVariableName,
Path.Combine(deploymentRoot, "baseline", "Python.Runtime.dll"))
.WithBaseline(true));
this.Add(baseJob
.WithId("new")
.WithEnvironmentVariable(EnvironmentVariableName,
Path.Combine(deploymentRoot, "new", "Python.Runtime.dll")));
}

static BaselineComparisonConfig() {
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainOnAssemblyResolve;
}

static Assembly CurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args) {
Console.WriteLine(args.Name);
if (!args.Name.StartsWith("Python.Runtime"))
return null;
string pythonRuntimeDll = Environment.GetEnvironmentVariable(EnvironmentVariableName);
if (string.IsNullOrEmpty(pythonRuntimeDll))
pythonRuntimeDll = Path.Combine(BenchmarkTests.DeploymentRoot, "baseline", "Python.Runtime.dll");
return Assembly.LoadFrom(pythonRuntimeDll);
}
}
}
63 changes: 63 additions & 0 deletions src/perf_tests/BenchmarkTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Reflection;

using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
using NUnit.Framework;

namespace Python.PerformanceTests
{
public class BenchmarkTests
{
Summary summary;

[OneTimeSetUp]
public void SetUp()
{
Environment.CurrentDirectory = Path.Combine(DeploymentRoot, "new");
this.summary = BenchmarkRunner.Run<PythonCallingNetBenchmark>();
Assert.IsNotEmpty(this.summary.Reports);
Assert.IsTrue(this.summary.Reports.All(r => r.Success));
}

[Test]
public void ReadInt64Property()
{
double optimisticPerfRatio = GetOptimisticPerfRatio(this.summary.Reports);
Assert.LessOrEqual(optimisticPerfRatio, 0.68);
}

[Test]
public void WriteInt64Property()
{
double optimisticPerfRatio = GetOptimisticPerfRatio(this.summary.Reports);
Assert.LessOrEqual(optimisticPerfRatio, 0.66);
}

static double GetOptimisticPerfRatio(
IReadOnlyList<BenchmarkReport> reports,
[CallerMemberName] string methodName = null)
{
reports = reports.Where(r => r.BenchmarkCase.Descriptor.WorkloadMethod.Name == methodName).ToArray();
if (reports.Count == 0)
throw new ArgumentException(
message: $"No reports found for {methodName}. "
+ "You have to match test method name to benchmark method name or "
+ "pass benchmark method name explicitly",
paramName: nameof(methodName));

var baseline = reports.Single(r => r.BenchmarkCase.Job.ResolvedId == "baseline").ResultStatistics;
var @new = reports.Single(r => r.BenchmarkCase.Job.ResolvedId != "baseline").ResultStatistics;

double newTimeOptimistic = @new.Mean - (@new.StandardDeviation + baseline.StandardDeviation) * 0.5;

return newTimeOptimistic / baseline.Mean;
}

public static string DeploymentRoot => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
}
}
34 changes: 34 additions & 0 deletions src/perf_tests/Python.PerformanceTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net461</TargetFramework>
<Configurations>DebugMono;DebugMonoPY3;ReleaseMono;ReleaseMonoPY3;DebugWin;DebugWinPY3;ReleaseWin;ReleaseWinPY3</Configurations>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.11.5" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="pythonnet" Version="2.3.0" GeneratePathProperty="true">
<IncludeAssets>compile</IncludeAssets>
</PackageReference>
</ItemGroup>

<Target Name="GetRuntimeLibBuildOutput" BeforeTargets="Build">
<MSBuild Projects="..\runtime\Python.Runtime.15.csproj" Properties="PYTHONNET_PY3_VERSION=PYTHON35;Configuration=$(Configuration);TargetFramework=net40;Python3Version=PYTHON35;OutputPath=bin\for_perf\">
<Output TaskParameter="TargetOutputs" ItemName="NewPythonRuntime" />
</MSBuild>
</Target>

<Target Name="CopyBaseline" AfterTargets="Build">
<Copy SourceFiles="$(Pkgpythonnet)\lib\net40\Python.Runtime.dll" DestinationFolder="$(OutDir)\baseline" />
</Target>

<Target Name="CopyNewBuild" AfterTargets="Build">
<Copy SourceFiles="@(NewPythonRuntime)" DestinationFolder="$(OutDir)\new" />
</Target>

</Project>
46 changes: 46 additions & 0 deletions src/perf_tests/PythonCallingNetBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Text;

using BenchmarkDotNet.Attributes;
using Python.Runtime;

namespace Python.PerformanceTests
{
[Config(typeof(BaselineComparisonConfig))]
public class PythonCallingNetBenchmark: BaselineComparisonBenchmarkBase
{
[Benchmark]
public void ReadInt64Property()
{
using (Py.GIL())
{
var locals = new PyDict();
locals.SetItem("a", new NetObject().ToPython());
PythonEngine.Exec($@"
s = 0
for i in range(300000):
s += a.{nameof(NetObject.LongProperty)}
", locals: locals.Handle);
}
}

[Benchmark]
public void WriteInt64Property() {
using (Py.GIL()) {
var locals = new PyDict();
locals.SetItem("a", new NetObject().ToPython());
PythonEngine.Exec($@"
s = 0
for i in range(300000):
a.{nameof(NetObject.LongProperty)} += i
", locals: locals.Handle);
}
}
}

class NetObject
{
public long LongProperty { get; set; } = 42;
}
}