Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 327c311

Browse files
committed
simplify benchmarking multiple similar methods
1 parent 2e28711 commit 327c311

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

src/perf_tests/BenchmarkTests.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Runtime.CompilerServices;
56
using System.Reflection;
67

78
using BenchmarkDotNet.Reports;
@@ -12,25 +13,36 @@ namespace Python.PerformanceTests
1213
{
1314
public class BenchmarkTests
1415
{
16+
Summary summary;
17+
1518
[OneTimeSetUp]
1619
public void SetUp()
1720
{
1821
Environment.CurrentDirectory = Path.Combine(DeploymentRoot, "new");
22+
this.summary = BenchmarkRunner.Run<PythonCallingNetBenchmark>();
23+
Assert.IsNotEmpty(this.summary.Reports);
24+
Assert.IsTrue(this.summary.Reports.All(r => r.Success));
1925
}
2026

2127
[Test]
22-
public void PythonCallingNet()
28+
public void ReadInt64Property()
2329
{
24-
var summary = BenchmarkRunner.Run<PythonCallingNetBenchmark>();
25-
26-
Assert.IsNotEmpty(summary.Reports);
27-
Assert.IsTrue(summary.Reports.All(r => r.Success));
30+
double optimisticPerfRatio = GetOptimisticPerfRatio(this.summary.Reports);
31+
Assert.LessOrEqual(optimisticPerfRatio, 0.68);
32+
}
2833

29-
double optimisticPerfRatio = GetOptimisticPerfRatio(summary.Reports);
30-
Assert.LessOrEqual(optimisticPerfRatio, 1.03);
34+
[Test]
35+
public void WriteInt64Property()
36+
{
37+
double optimisticPerfRatio = GetOptimisticPerfRatio(this.summary.Reports);
38+
Assert.LessOrEqual(optimisticPerfRatio, 0.66);
3139
}
3240

33-
static double GetOptimisticPerfRatio(IReadOnlyList<BenchmarkReport> reports) {
41+
static double GetOptimisticPerfRatio(
42+
IReadOnlyList<BenchmarkReport> reports,
43+
[CallerMemberName] string methodName = null)
44+
{
45+
reports = reports.Where(r => r.BenchmarkCase.Descriptor.WorkloadMethod.Name == methodName).ToArray();
3446
var baseline = reports.Single(r => r.BenchmarkCase.Job.ResolvedId == "baseline").ResultStatistics;
3547
var @new = reports.Single(r => r.BenchmarkCase.Job.ResolvedId != "baseline").ResultStatistics;
3648
double newTimeOptimistic = @new.Mean - (@new.StandardDeviation + baseline.StandardDeviation) * 0.5;

src/perf_tests/PythonCallingNetBenchmark.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,36 @@ namespace Python.PerformanceTests
1111
public class PythonCallingNetBenchmark: BaselineComparisonBenchmarkBase
1212
{
1313
[Benchmark]
14-
public void ReadIntProperty()
14+
public void ReadInt64Property()
1515
{
1616
using (Py.GIL())
1717
{
1818
var locals = new PyDict();
1919
locals.SetItem("a", new NetObject().ToPython());
20-
PythonEngine.Exec(@"
20+
PythonEngine.Exec($@"
2121
s = 0
22-
for i in range(1000000):
23-
s += a.IntProperty
22+
for i in range(300000):
23+
s += a.{nameof(NetObject.LongProperty)}
24+
", locals: locals.Handle);
25+
}
26+
}
27+
28+
[Benchmark]
29+
public void WriteInt64Property() {
30+
using (Py.GIL()) {
31+
var locals = new PyDict();
32+
locals.SetItem("a", new NetObject().ToPython());
33+
PythonEngine.Exec($@"
34+
s = 0
35+
for i in range(300000):
36+
a.{nameof(NetObject.LongProperty)} += i
2437
", locals: locals.Handle);
2538
}
2639
}
2740
}
2841

2942
class NetObject
3043
{
31-
public int IntProperty { get; set; } = 42;
44+
public long LongProperty { get; set; } = 42;
3245
}
3346
}

0 commit comments

Comments
 (0)