Skip to content

Commit

Permalink
Try to shorten the log file name to avoid TooLongPath on Windows (#2272)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyAkinshin committed Feb 21, 2023
1 parent 401030e commit e2de0ef
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/BenchmarkDotNet/Running/BenchmarkRunnerClean.cs
Expand Up @@ -17,6 +17,7 @@
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Mathematics;
using BenchmarkDotNet.Portability;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Toolchains;
using BenchmarkDotNet.Toolchains.Parameters;
Expand All @@ -41,8 +42,11 @@ internal static Summary[] Run(BenchmarkRunInfo[] benchmarkRunInfos)
var resolver = DefaultResolver;
var artifactsToCleanup = new List<string>();

var title = GetTitle(benchmarkRunInfos);
var rootArtifactsFolderPath = GetRootArtifactsFolderPath(benchmarkRunInfos);
var maxTitleLength = RuntimeInformation.IsWindows()
? 254 - rootArtifactsFolderPath.Length
: int.MaxValue;
var title = GetTitle(benchmarkRunInfos, maxTitleLength);
var resultsFolderPath = GetResultsFolderPath(rootArtifactsFolderPath, benchmarkRunInfos);
var logFilePath = Path.Combine(rootArtifactsFolderPath, title + ".log");
var idToResume = GetIdToResume(rootArtifactsFolderPath, title, benchmarkRunInfos);
Expand Down Expand Up @@ -573,7 +577,7 @@ private static string GetRootArtifactsFolderPath(BenchmarkRunInfo[] benchmarkRun
return customPath != default ? customPath.CreateIfNotExists() : defaultPath;
}

private static string GetTitle(BenchmarkRunInfo[] benchmarkRunInfos)
private static string GetTitle(BenchmarkRunInfo[] benchmarkRunInfos, int desiredMaxLength = int.MaxValue)
{
// few types might have the same name: A.Name and B.Name will both report "Name"
// in that case, we can not use the type name as file name because they would be getting overwritten #529
Expand All @@ -582,8 +586,22 @@ private static string GetTitle(BenchmarkRunInfo[] benchmarkRunInfos)
var fileNamePrefix = (uniqueTargetTypes.Length == 1)
? FolderNameHelper.ToFolderName(uniqueTargetTypes[0])
: "BenchmarkRun";
string dateTimeSuffix = DateTime.Now.ToString(DateTimeFormat);

return $"{fileNamePrefix}-{DateTime.Now.ToString(DateTimeFormat)}";
int maxFileNamePrefixLength = desiredMaxLength - dateTimeSuffix.Length - 1;
if (maxFileNamePrefixLength <= 2)
return dateTimeSuffix;

if (fileNamePrefix.Length > maxFileNamePrefixLength)
{
int length1 = maxFileNamePrefixLength / 2;
int length2 = maxFileNamePrefixLength - length1 - 1;
fileNamePrefix = fileNamePrefix.Substring(0, length1) +
"-" +
fileNamePrefix.Substring(fileNamePrefix.Length - length2, length2);
}

return $"{fileNamePrefix}-{dateTimeSuffix}";
}

private static string GetResultsFolderPath(string rootArtifactsFolderPath, BenchmarkRunInfo[] benchmarkRunInfos)
Expand Down
21 changes: 21 additions & 0 deletions tests/BenchmarkDotNet.IntegrationTests/PathTooLongTests.cs
@@ -0,0 +1,21 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Tests.XUnit;

namespace BenchmarkDotNet.IntegrationTests
{
public class PathTooLongTests : BenchmarkTestExecutor
{
[FactWindowsOnly("Testing Windows long path limitation")]
public void PathTooLongTest() =>
CanExecute<
VeryLongName012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789>();

[DryJob]
public class
VeryLongName012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
{
[Benchmark]
public void Foo() { }
}
}
}

0 comments on commit e2de0ef

Please sign in to comment.