Skip to content

Commit e2de0ef

Browse files
Try to shorten the log file name to avoid TooLongPath on Windows (#2272)
1 parent 401030e commit e2de0ef

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/BenchmarkDotNet/Running/BenchmarkRunnerClean.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using BenchmarkDotNet.Jobs;
1818
using BenchmarkDotNet.Loggers;
1919
using BenchmarkDotNet.Mathematics;
20+
using BenchmarkDotNet.Portability;
2021
using BenchmarkDotNet.Reports;
2122
using BenchmarkDotNet.Toolchains;
2223
using BenchmarkDotNet.Toolchains.Parameters;
@@ -41,8 +42,11 @@ internal static Summary[] Run(BenchmarkRunInfo[] benchmarkRunInfos)
4142
var resolver = DefaultResolver;
4243
var artifactsToCleanup = new List<string>();
4344

44-
var title = GetTitle(benchmarkRunInfos);
4545
var rootArtifactsFolderPath = GetRootArtifactsFolderPath(benchmarkRunInfos);
46+
var maxTitleLength = RuntimeInformation.IsWindows()
47+
? 254 - rootArtifactsFolderPath.Length
48+
: int.MaxValue;
49+
var title = GetTitle(benchmarkRunInfos, maxTitleLength);
4650
var resultsFolderPath = GetResultsFolderPath(rootArtifactsFolderPath, benchmarkRunInfos);
4751
var logFilePath = Path.Combine(rootArtifactsFolderPath, title + ".log");
4852
var idToResume = GetIdToResume(rootArtifactsFolderPath, title, benchmarkRunInfos);
@@ -573,7 +577,7 @@ private static string GetRootArtifactsFolderPath(BenchmarkRunInfo[] benchmarkRun
573577
return customPath != default ? customPath.CreateIfNotExists() : defaultPath;
574578
}
575579

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

586-
return $"{fileNamePrefix}-{DateTime.Now.ToString(DateTimeFormat)}";
591+
int maxFileNamePrefixLength = desiredMaxLength - dateTimeSuffix.Length - 1;
592+
if (maxFileNamePrefixLength <= 2)
593+
return dateTimeSuffix;
594+
595+
if (fileNamePrefix.Length > maxFileNamePrefixLength)
596+
{
597+
int length1 = maxFileNamePrefixLength / 2;
598+
int length2 = maxFileNamePrefixLength - length1 - 1;
599+
fileNamePrefix = fileNamePrefix.Substring(0, length1) +
600+
"-" +
601+
fileNamePrefix.Substring(fileNamePrefix.Length - length2, length2);
602+
}
603+
604+
return $"{fileNamePrefix}-{dateTimeSuffix}";
587605
}
588606

589607
private static string GetResultsFolderPath(string rootArtifactsFolderPath, BenchmarkRunInfo[] benchmarkRunInfos)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Tests.XUnit;
3+
4+
namespace BenchmarkDotNet.IntegrationTests
5+
{
6+
public class PathTooLongTests : BenchmarkTestExecutor
7+
{
8+
[FactWindowsOnly("Testing Windows long path limitation")]
9+
public void PathTooLongTest() =>
10+
CanExecute<
11+
VeryLongName012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789>();
12+
13+
[DryJob]
14+
public class
15+
VeryLongName012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
16+
{
17+
[Benchmark]
18+
public void Foo() { }
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)