Skip to content

Commit 7e2ec4d

Browse files
filmorlostmsu
andauthored
Performance tests with baseline from Pypi (pythonnet#1667)
* reenable perf tests for 3.0 * Get baseline dll from pypi fixes pythonnet#1368 Co-authored-by: Victor Nova <[email protected]>
1 parent 7450c5c commit 7e2ec4d

8 files changed

+53
-21
lines changed

.github/workflows/main.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,10 @@ jobs:
7272
- name: Python tests run from .NET
7373
run: dotnet test --runtime any-${{ matrix.platform }} src/python_tests_runner/
7474

75-
# TODO: Run perf tests
75+
- name: Perf tests
76+
if: ${{ matrix.python == '3.8' }}
77+
run: |
78+
pip install --force --no-deps --target src/perf_tests/baseline/ pythonnet==2.5.2
79+
dotnet test --configuration Release --runtime any-${{ matrix.platform }} --logger "console;verbosity=detailed" src/perf_tests/
80+
7681
# TODO: Run mono tests on Windows?

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/src/runtime/interopNative.cs
2+
/src/perf_tests/baseline/
23

34
# General binaries and Build results
45
*.dll

src/perf_tests/BaselineComparisonBenchmarkBase.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
45
using System.Reflection;
56

@@ -17,8 +18,7 @@ public BaselineComparisonBenchmarkBase()
1718
try {
1819
PythonEngine.Initialize();
1920
Console.WriteLine("Python Initialized");
20-
if (PythonEngine.BeginAllowThreads() == IntPtr.Zero)
21-
throw new PythonException();
21+
Trace.Assert(PythonEngine.BeginAllowThreads() != IntPtr.Zero);
2222
Console.WriteLine("Threading enabled");
2323
}
2424
catch (Exception e) {
@@ -28,6 +28,11 @@ public BaselineComparisonBenchmarkBase()
2828
}
2929

3030
static BaselineComparisonBenchmarkBase()
31+
{
32+
SetupRuntimeResolve();
33+
}
34+
35+
public static void SetupRuntimeResolve()
3136
{
3237
string pythonRuntimeDll = Environment.GetEnvironmentVariable(BaselineComparisonConfig.EnvironmentVariableName);
3338
if (string.IsNullOrEmpty(pythonRuntimeDll))

src/perf_tests/BaselineComparisonConfig.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
using BenchmarkDotNet.Configs;
77
using BenchmarkDotNet.Jobs;
8-
using BenchmarkDotNet.Horology;
8+
9+
using Perfolizer.Horology;
910

1011
namespace Python.PerformanceTests
1112
{

src/perf_tests/BenchmarkTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ public void SetUp()
3030
public void ReadInt64Property()
3131
{
3232
double optimisticPerfRatio = GetOptimisticPerfRatio(this.summary.Reports);
33-
AssertPerformanceIsBetterOrSame(optimisticPerfRatio, target: 0.57);
33+
AssertPerformanceIsBetterOrSame(optimisticPerfRatio, target: 1.35);
3434
}
3535

3636
[Test]
3737
public void WriteInt64Property()
3838
{
3939
double optimisticPerfRatio = GetOptimisticPerfRatio(this.summary.Reports);
40-
AssertPerformanceIsBetterOrSame(optimisticPerfRatio, target: 0.57);
40+
AssertPerformanceIsBetterOrSame(optimisticPerfRatio, target: 1.25);
4141
}
4242

4343
static double GetOptimisticPerfRatio(

src/perf_tests/Python.PerformanceTests.csproj

+19-11
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,25 @@
33
<PropertyGroup>
44
<TargetFramework>net472</TargetFramework>
55
<IsPackable>false</IsPackable>
6-
<Platforms>x64;x86</Platforms>
6+
<Platforms>x64</Platforms>
7+
<PlatformTarget>x64</PlatformTarget>
8+
79
</PropertyGroup>
810

911
<ItemGroup>
10-
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
12+
<Content Include="baseline\Python.Runtime.dll">
13+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
14+
</Content>
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<Reference Include="baseline/Python.Runtime.dll">
19+
<Private>false</Private>
20+
</Reference>
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
1125
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0">
1226
<PrivateAssets>all</PrivateAssets>
1327
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
@@ -17,23 +31,17 @@
1731
<PrivateAssets>all</PrivateAssets>
1832
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1933
</PackageReference>
20-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
21-
<PackageReference Include="pythonnet" Version="2.3.0" GeneratePathProperty="true">
22-
<IncludeAssets>compile</IncludeAssets>
23-
</PackageReference>
34+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
2435
</ItemGroup>
2536

2637
<Target Name="GetRuntimeLibBuildOutput" BeforeTargets="Build">
27-
<MSBuild Projects="..\runtime\Python.Runtime.csproj" Properties="PYTHONNET_PY3_VERSION=PYTHON38;Python3Version=PYTHON38;OutputPath=bin\for_perf\">
38+
<MSBuild Projects="..\runtime\Python.Runtime.csproj" Properties="OutputPath=bin\for_perf\;Configuration=Release;TargetFramework=netstandard2.0" Targets="Build">
2839
<Output TaskParameter="TargetOutputs" ItemName="NewPythonRuntime" />
2940
</MSBuild>
3041
</Target>
3142

32-
<Target Name="CopyBaseline" AfterTargets="Build">
33-
<Copy SourceFiles="$(Pkgpythonnet)\lib\net40\Python.Runtime.dll" DestinationFolder="$(OutDir)\baseline" />
34-
</Target>
35-
3643
<Target Name="CopyNewBuild" AfterTargets="Build">
44+
<Message Text="Outputs: @(NewPythonRuntime)" Importance="high" />
3745
<Copy SourceFiles="@(NewPythonRuntime)" DestinationFolder="$(OutDir)\new" />
3846
</Target>
3947

src/perf_tests/PythonCallingNetBenchmark.cs

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Reflection;
34
using System.Text;
45

56
using BenchmarkDotNet.Attributes;
@@ -17,11 +18,11 @@ public void ReadInt64Property()
1718
{
1819
var locals = new PyDict();
1920
locals.SetItem("a", new NetObject().ToPython());
20-
PythonEngine.Exec($@"
21+
Exec($@"
2122
s = 0
2223
for i in range(50000):
2324
s += a.{nameof(NetObject.LongProperty)}
24-
", locals: locals.Handle);
25+
", locals: locals);
2526
}
2627
}
2728

@@ -30,13 +31,24 @@ public void WriteInt64Property() {
3031
using (Py.GIL()) {
3132
var locals = new PyDict();
3233
locals.SetItem("a", new NetObject().ToPython());
33-
PythonEngine.Exec($@"
34+
Exec($@"
3435
s = 0
3536
for i in range(50000):
3637
a.{nameof(NetObject.LongProperty)} += i
37-
", locals: locals.Handle);
38+
", locals: locals);
3839
}
3940
}
41+
42+
static void Exec(string code, PyDict locals)
43+
{
44+
MethodInfo exec = typeof(PythonEngine).GetMethod(nameof(PythonEngine.Exec));
45+
object localsArg = typeof(PyObject).Assembly.GetName().Version.Major >= 3
46+
? locals : locals.Handle;
47+
exec.Invoke(null, new[]
48+
{
49+
code, localsArg, null
50+
});
51+
}
4052
}
4153

4254
class NetObject

src/perf_tests/baseline/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)