Skip to content
Open
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
2 changes: 1 addition & 1 deletion test/Microsoft.ML.AutoML.Tests/DatasetUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public static IEnumerable<ImageData> LoadImagesFromDirectory(string folder)
public static string DownloadImageSet(string imagesDownloadFolder)
{
string fileName = "flower_photos_tiny_set_for_unit_tests.zip";
string url = $"https://aka.ms/mlnet-resources/datasets/flower_photos_tiny_set_for_unit_test.zip";
string url = "https://mlpublicassets.blob.core.windows.net/assets/datasets/flower_photos_tiny_set_for_unit_tests.zip";

Download(url, imagesDownloadFolder, fileName).Wait();
UnZip(Path.Combine(imagesDownloadFolder, fileName), imagesDownloadFolder);
Expand Down
29 changes: 11 additions & 18 deletions test/Microsoft.ML.PerformanceTests/BenchmarkBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

using System;
using System.IO;
using Microsoft.ML.Internal.Utilities;
using Microsoft.ML.Runtime;
using Microsoft.ML.TestFrameworkCommon;

namespace Microsoft.ML.PerformanceTests
Expand Down Expand Up @@ -41,25 +39,20 @@ public static string GetBenchmarkDataPathAndEnsureData(string name, string path
if (File.Exists(filePath))
return filePath;

var mlContext = new MLContext(1);
int timeout = 10 * 60 * 1000;
string url = $"benchmarks/{name}";
var localPath = path == "" ?
Path.GetFullPath(DataDir) :
Path.GetFullPath(Path.Combine(DataDir, path));

using (var ch = (mlContext as IHostEnvironment).Start("Ensuring dataset files are present."))
string url;
if (path == "")
{
url = $"https://raw.githubusercontent.com/dotnet/machinelearning/main/test/data/{name.Replace('\\', '/')}";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking forward, do you think we might need to handle servicing release branches or is that not a concern in ML.NET? Just wondering if by taking data from main branch we might hit an issue later. Note, I haven't onboarded into the full release process so maybe this is not an issue at all.

}
else
{
var ensureModel = ResourceManagerUtils.Instance.EnsureResourceAsync(
mlContext, ch, url, name, localPath, timeout);
ensureModel.Wait();
var errorResult = ResourceManagerUtils.GetErrorMessage(out var errorMessage, ensureModel.Result);
if (errorResult != null)
{
throw ch.Except($"{errorMessage}\n{name} could not be downloaded!");
}
var blobName = Path.GetFileName(name);
if (blobName == "WikiDetoxAnnotated160kRows.tsv")
blobName = "wikiDetoxAnnotated160kRows.tsv";
url = $"https://mlpublicassets.blob.core.windows.net/assets/benchmarks/{blobName}";
}

TestDownloadUtils.DownloadFile(url, filePath, TimeSpan.FromMinutes(10));
return filePath;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static string DownloadImageSet(string imagesDownloadFolder)

//SINGLE SMALL FLOWERS IMAGESET (200 files)
string fileName = "flower_photos_small_set.zip";
string url = $"https://aka.ms/mlnet-resources/datasets/flower_photos_small_set.zip/";
string url = "https://mlpublicassets.blob.core.windows.net/assets/datasets/flower_photos_small_set.zip";

Download(url, imagesDownloadFolder, fileName);
UnZip(Path.Combine(imagesDownloadFolder, fileName), imagesDownloadFolder);
Expand Down
35 changes: 3 additions & 32 deletions test/Microsoft.ML.TensorFlow.Tests/TensorflowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
using System.IO.Compression;
using System.Linq;
using Microsoft.ML.Data;
using Microsoft.ML.Internal.Utilities;
using Microsoft.ML.Runtime;
using Microsoft.ML.TensorFlow;
using Microsoft.ML.TestFramework;
using Microsoft.ML.TestFramework.Attributes;
Expand Down Expand Up @@ -61,7 +59,6 @@ public sealed class TensorFlowScenariosTests : BaseTestClass, IClassFixture<Tens
{
private readonly string _fullImagesetFolderPath;
private readonly string _finalImagesFolderName;
private string _timeOutOldValue;
private MLContext _mlContext = new MLContext(seed: 1);

public TensorFlowScenariosTests(ITestOutputHelper output) : base(output)
Expand All @@ -77,19 +74,6 @@ public TensorFlowScenariosTests(ITestOutputHelper output) : base(output)
imagesDownloadFolderPath, _finalImagesFolderName);
}

protected override void Initialize()
{
// set timeout to 3 minutes, download sometimes will stuck so set smaller timeout to fail fast and retry download
_timeOutOldValue = Environment.GetEnvironmentVariable(ResourceManagerUtils.TimeoutEnvVariable);
Environment.SetEnvironmentVariable(ResourceManagerUtils.TimeoutEnvVariable, (3 * 60 * 1000).ToString());
}

protected override void Cleanup()
{
// set back timeout value
Environment.SetEnvironmentVariable(ResourceManagerUtils.TimeoutEnvVariable, _timeOutOldValue);
}

private class TestData
{
[VectorType(4)]
Expand Down Expand Up @@ -1952,7 +1936,7 @@ public string DownloadImageSet(string imagesDownloadFolder)
{
string fileName = "flower_photos_tiny_set_for_unit_tests.zip";
string filenameAlias = "FPTSUT"; // FPTSUT = flower photos tiny set for unit tests
string url = "datasets/flower_photos_tiny_set_for_unit_test.zip";
string url = "https://mlpublicassets.blob.core.windows.net/assets/datasets/flower_photos_tiny_set_for_unit_tests.zip";

Download(url, imagesDownloadFolder, fileName);
UnZip(Path.Combine(imagesDownloadFolder, fileName), imagesDownloadFolder);
Expand All @@ -1965,7 +1949,7 @@ public string DownloadImageSet(string imagesDownloadFolder)
public string DownloadBadImageSet(string imagesDownloadFolder)
{
string fileName = "CatsVsDogs_tiny_for_unit_tests.zip";
string url = "datasets/CatsVsDogs_tiny_for_unit_tests.zip";
string url = "https://mlpublicassets.blob.core.windows.net/assets/datasets/CatsVsDogs_tiny_for_unit_tests.zip";

Download(url, imagesDownloadFolder, fileName);
UnZip(Path.Combine(imagesDownloadFolder, fileName), imagesDownloadFolder);
Expand All @@ -1985,20 +1969,7 @@ private bool Download(string url, string destDir, string destFileName)
if (File.Exists(relativeFilePath))
return false;

int timeout = 10 * 60 * 1000;
using (var ch = (_mlContext as IHostEnvironment).Start("Ensuring image files are present."))
{
var ensureModel = ResourceManagerUtils.Instance.EnsureResourceAsync(_mlContext, ch, url, destFileName, destDir, timeout);
ensureModel.Wait();
var errorResult = ResourceManagerUtils.GetErrorMessage(out var errorMessage, ensureModel.Result);
if (errorResult != null)
{
var directory = Path.GetDirectoryName(errorResult.FileName);
var name = Path.GetFileName(errorResult.FileName);
throw ch.Except($"{errorMessage}\nImage file could not be downloaded!");
}
}

TestDownloadUtils.DownloadFile(url, relativeFilePath, TimeSpan.FromMinutes(3));
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

<Import Project="..\TestFrameworkDependency.props" />

<PropertyGroup>
<Nullable>annotations</Nullable>
</PropertyGroup>

<ItemGroup>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand All @@ -13,4 +17,8 @@
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\..\src\Common\tests\RetryHelper.cs" Link="RetryHelper.cs" />
</ItemGroup>

</Project>
64 changes: 64 additions & 0 deletions test/Microsoft.ML.TestFrameworkCommon/TestDownloadUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.ML.TestFrameworkCommon
{
public static class TestDownloadUtils
{
public static void DownloadFile(string url, string filePath, TimeSpan timeout)
{
var directory = Path.GetDirectoryName(filePath);
if (!string.IsNullOrEmpty(directory))
Directory.CreateDirectory(directory);

string temporaryFilePath = $"{filePath}.{Guid.NewGuid():N}.download";
using (var client = new HttpClient { Timeout = Timeout.InfiniteTimeSpan })
{
RetryHelper.Execute(() =>
{
try
{
if (File.Exists(filePath))
return;

using (var cancellationSource = new CancellationTokenSource(timeout))
using (var response = client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, cancellationSource.Token).GetAwaiter().GetResult())
{
response.EnsureSuccessStatusCode();
long? expectedLength = response.Content.Headers.ContentLength;
using (var contentStream = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult())
using (var fileStream = new FileStream(temporaryFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
contentStream.CopyToAsync(fileStream, 81920, cancellationSource.Token).GetAwaiter().GetResult();
if (expectedLength.HasValue && fileStream.Length != expectedLength.Value)
throw new IOException($"Expected {expectedLength.Value} bytes from '{url}', but downloaded {fileStream.Length} bytes.");
}
}

try
{
File.Move(temporaryFilePath, filePath);
}
catch (IOException) when (File.Exists(filePath))
{
// Another caller completed the same download first.
}
}
finally
{
if (File.Exists(temporaryFilePath))
File.Delete(temporaryFilePath);
}
}, backoffFunc: attempt => 10_000,
retryWhen: ex => ex is HttpRequestException || ex is TaskCanceledException || ex is IOException);
}
}
}
}
Loading