Skip to content

Commit b148454

Browse files
fix benchmark test hanging issue (#4985)
1 parent 76c2f76 commit b148454

15 files changed

+66
-44
lines changed

test/Microsoft.ML.Benchmarks.Tests/BenchmarksTest.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,6 @@ where Attribute.IsDefined(type, typeof(CIBenchmark))
5252
[MemberData(nameof(GetBenchmarks))]
5353
public void BenchmarksProjectIsNotBroken(Type type)
5454
{
55-
// TODO: [TEST_STABILITY]: Benchmark test sometime hangs on windows of dotnet core 3.1
56-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) &&
57-
AppDomain.CurrentDomain.GetData("FX_PRODUCT_VERSION") != null)
58-
{
59-
return;
60-
}
61-
6255
var summary = BenchmarkRunner.Run(type, new TestConfig().With(new OutputLogger(output)));
6356

6457
Assert.False(summary.HasCriticalValidationErrors, "The \"Summary\" should have NOT \"HasCriticalValidationErrors\"");
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.IO;
6+
using Microsoft.ML.TestFrameworkCommon;
7+
8+
namespace Microsoft.ML.Benchmarks
9+
{
10+
public class BenchmarkBase
11+
{
12+
// Make sure DataDir is initialized before benchmark running.
13+
static BenchmarkBase()
14+
{
15+
RootDir = TestCommon.GetRepoRoot();
16+
DataDir = Path.Combine(RootDir, "test", "data");
17+
}
18+
19+
protected static string RootDir { get; }
20+
protected static string DataDir { get; }
21+
22+
// Don't use BaseTestClass's GetDataPath method instead for benchmark.
23+
// BaseTestClass's static constructor is not guaranteed to be called before
24+
// benchmark running (depending on CLR version this has different behaviour).
25+
// The problem with executing BaseTestClass's static constructor when benchmark
26+
// is running is it sometime cause process hanging when the constructor trying
27+
// to load MKL, this is related to below issue:
28+
// https://github.com/dotnet/machinelearning/issues/1073
29+
public static string GetBenchmarkDataPath(string name)
30+
{
31+
if (string.IsNullOrWhiteSpace(name))
32+
return null;
33+
return Path.GetFullPath(Path.Combine(DataDir, name));
34+
}
35+
}
36+
}

test/Microsoft.ML.Benchmarks/CacheDataViewBench.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace Microsoft.ML.Benchmarks
1111
{
1212
[CIBenchmark]
13-
public class CacheDataViewBench
13+
public class CacheDataViewBench : BenchmarkBase
1414
{
1515
private const int Length = 100000;
1616

test/Microsoft.ML.Benchmarks/FeaturizeTextBench.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace Microsoft.ML.Benchmarks
1515
{
1616
[Config(typeof(TrainConfig))]
17-
public class FeaturizeTextBench
17+
public class FeaturizeTextBench : BenchmarkBase
1818
{
1919
private MLContext _mlContext;
2020
private IDataView _dataset;

test/Microsoft.ML.Benchmarks/Harness/Metrics.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace Microsoft.ML.Benchmarks
1515
{
16-
public abstract class WithExtraMetrics
16+
public abstract class WithExtraMetrics : BenchmarkBase
1717
{
1818
protected abstract IEnumerable<Metric> GetMetrics();
1919

test/Microsoft.ML.Benchmarks/HashBench.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
namespace Microsoft.ML.Benchmarks
1414
{
1515
[CIBenchmark]
16-
public class HashBench
16+
public class HashBench : BenchmarkBase
1717
{
1818
private sealed class RowImpl : DataViewRow
1919
{

test/Microsoft.ML.Benchmarks/ImageClassificationBench.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
namespace Microsoft.ML.Benchmarks
1919
{
2020
[Config(typeof(TrainConfig))]
21-
public class ImageClassificationBench
21+
public class ImageClassificationBench : BenchmarkBase
2222
{
2323
private MLContext _mlContext;
2424
private IDataView _trainDataset;

test/Microsoft.ML.Benchmarks/KMeansAndLogisticRegressionBench.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
using Microsoft.ML.Benchmarks.Harness;
77
using Microsoft.ML.Calibrators;
88
using Microsoft.ML.Data;
9-
using Microsoft.ML.TestFramework;
109
using Microsoft.ML.Trainers;
1110

1211
namespace Microsoft.ML.Benchmarks
1312
{
1413
[CIBenchmark]
15-
public class KMeansAndLogisticRegressionBench
14+
public class KMeansAndLogisticRegressionBench : BenchmarkBase
1615
{
17-
private readonly string _dataPath = BaseTestClass.GetDataPath("adult.tiny.with-schema.txt");
16+
private readonly string _dataPath = GetBenchmarkDataPath("adult.tiny.with-schema.txt");
1817

1918
[Benchmark]
2019
public CalibratedModelParametersBase<LinearBinaryModelParameters, PlattCalibrator> TrainKMeansAndLR()

test/Microsoft.ML.Benchmarks/Microsoft.ML.Benchmarks.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<ProjectReference Include="..\..\src\Microsoft.ML.ImageAnalytics\Microsoft.ML.ImageAnalytics.csproj" />
2323
<ProjectReference Include="..\..\src\Microsoft.ML.StandardTrainers\Microsoft.ML.StandardTrainers.csproj" />
2424
<ProjectReference Include="..\..\test\Microsoft.ML.TestFramework\Microsoft.ML.TestFramework.csproj" />
25+
<ProjectReference Include="..\..\test\Microsoft.ML.TestFrameworkCommon\Microsoft.ML.TestFrameworkCommon.csproj" />
2526
</ItemGroup>
2627
<ItemGroup>
2728
<NativeAssemblyReference Include="CpuMathNative" />

test/Microsoft.ML.Benchmarks/Numeric/Ranking.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using BenchmarkDotNet.Attributes;
77
using Microsoft.ML.Data;
88
using Microsoft.ML.Trainers.LightGbm;
9-
using Microsoft.ML.TestFramework;
109
using Microsoft.ML.Trainers;
1110
using Microsoft.ML.Trainers.FastTree;
1211
using Microsoft.ML.Transforms;
@@ -15,16 +14,16 @@
1514
namespace Microsoft.ML.Benchmarks
1615
{
1716
[Config(typeof(TrainConfig))]
18-
public class RankingTrain
17+
public class RankingTrain : BenchmarkBase
1918
{
2019
private string _mslrWeb10kValidate;
2120
private string _mslrWeb10kTrain;
2221

2322
[GlobalSetup]
2423
public void SetupTrainingSpeedTests()
2524
{
26-
_mslrWeb10kValidate = BaseTestClass.GetDataPath(TestDatasets.MSLRWeb.validFilename);
27-
_mslrWeb10kTrain = BaseTestClass.GetDataPath(TestDatasets.MSLRWeb.trainFilename);
25+
_mslrWeb10kValidate = GetBenchmarkDataPath(TestDatasets.MSLRWeb.validFilename);
26+
_mslrWeb10kTrain = GetBenchmarkDataPath(TestDatasets.MSLRWeb.trainFilename);
2827

2928
if (!File.Exists(_mslrWeb10kValidate))
3029
throw new FileNotFoundException(string.Format(Errors.DatasetNotFound, _mslrWeb10kValidate));
@@ -63,7 +62,7 @@ public void TrainTest_Ranking_MSLRWeb10K_RawNumericFeatures_LightGBMRanking()
6362
}
6463
}
6564

66-
public class RankingTest
65+
public class RankingTest : BenchmarkBase
6766
{
6867
private string _mslrWeb10kValidate;
6968
private string _mslrWeb10kTrain;
@@ -73,9 +72,9 @@ public class RankingTest
7372
[GlobalSetup]
7473
public void SetupScoringSpeedTests()
7574
{
76-
_mslrWeb10kTest = BaseTestClass.GetDataPath(TestDatasets.MSLRWeb.testFilename);
77-
_mslrWeb10kValidate = BaseTestClass.GetDataPath(TestDatasets.MSLRWeb.validFilename);
78-
_mslrWeb10kTrain = BaseTestClass.GetDataPath(TestDatasets.MSLRWeb.trainFilename);
75+
_mslrWeb10kTest = GetBenchmarkDataPath(TestDatasets.MSLRWeb.testFilename);
76+
_mslrWeb10kValidate = GetBenchmarkDataPath(TestDatasets.MSLRWeb.validFilename);
77+
_mslrWeb10kTrain = GetBenchmarkDataPath(TestDatasets.MSLRWeb.trainFilename);
7978

8079
if (!File.Exists(_mslrWeb10kTest))
8180
throw new FileNotFoundException(string.Format(Errors.DatasetNotFound, _mslrWeb10kTest));

test/Microsoft.ML.Benchmarks/PredictionEngineBench.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
using BenchmarkDotNet.Attributes;
66
using Microsoft.ML.Benchmarks.Harness;
77
using Microsoft.ML.Data;
8-
using Microsoft.ML.TestFramework;
98
using Microsoft.ML.Trainers;
109
using Microsoft.ML.Transforms;
1110

1211
namespace Microsoft.ML.Benchmarks
1312
{
1413
[CIBenchmark]
15-
public class PredictionEngineBench
14+
public class PredictionEngineBench : BenchmarkBase
1615
{
1716
private IrisData _irisExample;
1817
private PredictionEngine<IrisData, IrisPrediction> _irisModel;
@@ -34,7 +33,7 @@ public void SetupIrisPipeline()
3433
PetalWidth = 5.1f,
3534
};
3635

37-
string irisDataPath = BaseTestClass.GetDataPath("iris.txt");
36+
string irisDataPath = GetBenchmarkDataPath("iris.txt");
3837

3938
var env = new MLContext(seed: 1);
4039

@@ -73,7 +72,7 @@ public void SetupSentimentPipeline()
7372
SentimentText = "Not a big fan of this."
7473
};
7574

76-
string sentimentDataPath = BaseTestClass.GetDataPath("wikipedia-detox-250-line-data.tsv");
75+
string sentimentDataPath = GetBenchmarkDataPath("wikipedia-detox-250-line-data.tsv");
7776

7877
var mlContext = new MLContext(seed: 1);
7978

@@ -108,7 +107,7 @@ public void SetupBreastCancerPipeline()
108107
Features = new[] { 5f, 1f, 1f, 1f, 2f, 1f, 3f, 1f, 1f }
109108
};
110109

111-
string breastCancerDataPath = BaseTestClass.GetDataPath("breast-cancer.txt");
110+
string breastCancerDataPath = GetBenchmarkDataPath("breast-cancer.txt");
112111

113112
var env = new MLContext(seed: 1);
114113

test/Microsoft.ML.Benchmarks/RffTransform.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,20 @@
66
using BenchmarkDotNet.Attributes;
77
using Microsoft.ML.Benchmarks.Harness;
88
using Microsoft.ML.Data;
9-
using Microsoft.ML.RunTests;
10-
using Microsoft.ML.TestFramework;
119
using Microsoft.ML.TestFrameworkCommon;
1210
using Microsoft.ML.Transforms;
1311

1412
namespace Microsoft.ML.Benchmarks
1513
{
1614
[CIBenchmark]
17-
public class RffTransformTrain
15+
public class RffTransformTrain : BenchmarkBase
1816
{
1917
private string _dataPathDigits;
2018

2119
[GlobalSetup]
2220
public void SetupTrainingSpeedTests()
2321
{
24-
_dataPathDigits = BaseTestClass.GetDataPath(TestDatasets.Digits.trainFilename);
22+
_dataPathDigits = GetBenchmarkDataPath(TestDatasets.Digits.trainFilename);
2523

2624
if (!File.Exists(_dataPathDigits))
2725
throw new FileNotFoundException(string.Format(Errors.DatasetNotFound, _dataPathDigits));

test/Microsoft.ML.Benchmarks/StochasticDualCoordinateAscentClassifierBench.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using BenchmarkDotNet.Engines;
99
using Microsoft.ML.Benchmarks.Harness;
1010
using Microsoft.ML.Data;
11-
using Microsoft.ML.TestFramework;
1211
using Microsoft.ML.Trainers;
1312
using Microsoft.ML.Transforms;
1413
using Microsoft.ML.Transforms.Text;
@@ -18,8 +17,8 @@ namespace Microsoft.ML.Benchmarks
1817
[CIBenchmark]
1918
public class StochasticDualCoordinateAscentClassifierBench : WithExtraMetrics
2019
{
21-
private readonly string _dataPath = BaseTestClass.GetDataPath("iris.txt");
22-
private readonly string _sentimentDataPath = BaseTestClass.GetDataPath("wikipedia-detox-250-line-data.tsv");
20+
private readonly string _dataPath = GetBenchmarkDataPath("iris.txt");
21+
private readonly string _sentimentDataPath = GetBenchmarkDataPath("wikipedia-detox-250-line-data.tsv");
2322
private readonly Consumer _consumer = new Consumer(); // BenchmarkDotNet utility type used to prevent dead code elimination
2423

2524
private readonly MLContext _mlContext = new MLContext(seed: 1);

test/Microsoft.ML.Benchmarks/Text/MultiClassClassification.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,21 @@
66
using BenchmarkDotNet.Attributes;
77
using Microsoft.ML.Data;
88
using Microsoft.ML.Trainers.LightGbm;
9-
using Microsoft.ML.RunTests;
10-
using Microsoft.ML.TestFramework;
119
using Microsoft.ML.Trainers;
1210
using Microsoft.ML.Transforms;
1311
using Microsoft.ML.TestFrameworkCommon;
1412

1513
namespace Microsoft.ML.Benchmarks
1614
{
1715
[Config(typeof(TrainConfig))]
18-
public class MulticlassClassificationTrain
16+
public class MulticlassClassificationTrain : BenchmarkBase
1917
{
2018
private string _dataPathWiki;
2119

2220
[GlobalSetup]
2321
public void SetupTrainingSpeedTests()
2422
{
25-
_dataPathWiki = BaseTestClass.GetDataPath(TestDatasets.WikiDetox.trainFilename);
23+
_dataPathWiki = GetBenchmarkDataPath(TestDatasets.WikiDetox.trainFilename);
2624

2725
if (!File.Exists(_dataPathWiki))
2826
throw new FileNotFoundException(string.Format(Errors.DatasetNotFound, _dataPathWiki));
@@ -91,15 +89,15 @@ public void CV_Multiclass_WikiDetox_WordEmbeddings_SDCAMC()
9189
}
9290
}
9391

94-
public class MulticlassClassificationTest
92+
public class MulticlassClassificationTest : BenchmarkBase
9593
{
9694
private string _dataPathWiki;
9795
private string _modelPathWiki;
9896

9997
[GlobalSetup]
10098
public void SetupScoringSpeedTests()
10199
{
102-
_dataPathWiki = BaseTestClass.GetDataPath(TestDatasets.WikiDetox.trainFilename);
100+
_dataPathWiki = GetBenchmarkDataPath(TestDatasets.WikiDetox.trainFilename);
103101

104102
if (!File.Exists(_dataPathWiki))
105103
throw new FileNotFoundException(string.Format(Errors.DatasetNotFound, _dataPathWiki));

test/Microsoft.ML.Benchmarks/TextPredictionEngineCreation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
using BenchmarkDotNet.Attributes;
77
using Microsoft.ML;
88
using Microsoft.ML.Benchmarks;
9-
using Microsoft.ML.TestFramework;
109
using Microsoft.ML.Trainers;
1110

1211
namespace micro
1312
{
1413
[SimpleJob]
15-
public class TextPredictionEngineCreationBenchmark
14+
public class TextPredictionEngineCreationBenchmark : BenchmarkBase
1615
{
1716
private MLContext _context;
1817
private ITransformer _trainedModel;
@@ -22,7 +21,8 @@ public class TextPredictionEngineCreationBenchmark
2221
public void Setup()
2322
{
2423
_context = new MLContext(1);
25-
var data = _context.Data.LoadFromTextFile<SentimentData>(BaseTestClass.GetDataPath("wikipedia-detox-250-line-data.tsv"), hasHeader: true);
24+
var data = _context.Data.LoadFromTextFile<SentimentData>(
25+
GetBenchmarkDataPath("wikipedia-detox-250-line-data.tsv"), hasHeader: true);
2626

2727
// Pipeline.
2828
var pipeline = _context.Transforms.Text.FeaturizeText("Features", "SentimentText")

0 commit comments

Comments
 (0)