Skip to content

Commit 5def695

Browse files
author
Furkan Küçük
committed
Performance tryouts
1 parent a07c8ee commit 5def695

File tree

4 files changed

+133
-3
lines changed

4 files changed

+133
-3
lines changed

src/GitVersion.Configuration.Tests/Configuration/ConfigurationFileLocatorTests.cs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public void ReturnConfigurationFilePathIfCustomConfigurationIsSet()
174174
{
175175
this.workingPath = this.repoPath;
176176

177-
this.gitVersionOptions = new() { ConfigurationInfo = { ConfigurationFile = "Configuration/CustomConfig.yaml" } };
177+
this.gitVersionOptions = new() { ConfigurationInfo = { ConfigurationFile = Path.Combine(this.workingPath, "Configuration", "CustomConfig.yaml") } };
178178

179179
var serviceProvider = GetServiceProvider(this.gitVersionOptions);
180180
this.fileSystem = serviceProvider.GetRequiredService<IFileSystem>();
@@ -188,6 +188,55 @@ public void ReturnConfigurationFilePathIfCustomConfigurationIsSet()
188188
config.ShouldBe(Path.GetFullPath("Configuration/CustomConfig.yaml"));
189189
}
190190

191+
[Test]
192+
public void ReturnConfigurationFilePathIfCustomConfigurationIsSet_PerformanceComparison()
193+
{
194+
this.workingPath = this.repoPath;
195+
196+
this.gitVersionOptions = new() { ConfigurationInfo = { ConfigurationFile = Path.Combine(this.workingPath, "Configuration", "CustomConfig.yaml") } };
197+
198+
var serviceProvider = GetServiceProvider(this.gitVersionOptions);
199+
this.fileSystem = serviceProvider.GetRequiredService<IFileSystem>();
200+
201+
using var _ = this.fileSystem.SetupConfigFile(
202+
path: Path.Combine(this.workingPath, "Configuration"), fileName: "CustomConfig2.yaml"
203+
);
204+
this.configFileLocator = serviceProvider.GetRequiredService<IConfigurationFileLocator>();
205+
Stopwatch stopwatch = new();
206+
stopwatch.Start();
207+
var config = this.configFileLocator.GetConfigurationFile(this.workingPath);
208+
stopwatch.Stop();
209+
long et1 = stopwatch.ElapsedTicks;
210+
211+
stopwatch.Reset();
212+
stopwatch.Start();
213+
config = this.configFileLocator.GetConfigurationFileTemp(this.workingPath);
214+
stopwatch.Stop();
215+
long et2 = stopwatch.ElapsedTicks;
216+
217+
//et1.ShouldBeLessThan(et2);
218+
219+
stopwatch.Reset();
220+
stopwatch.Start();
221+
for (int i = 0; i < 1000000; i++)
222+
{
223+
config = this.configFileLocator.GetConfigurationFile(this.workingPath);
224+
}
225+
stopwatch.Stop();
226+
long et3 = stopwatch.ElapsedMilliseconds;
227+
228+
stopwatch.Reset();
229+
stopwatch.Start();
230+
for (int i = 0; i < 1000000; i++)
231+
{
232+
config = this.configFileLocator.GetConfigurationFileTemp(this.workingPath);
233+
}
234+
stopwatch.Stop();
235+
long et4 = stopwatch.ElapsedMilliseconds;
236+
237+
et3.ShouldBeLessThan(et4);
238+
}
239+
191240
[TestCase(null)]
192241
[TestCase("")]
193242
[TestCase(" ")]

src/GitVersion.Configuration/ConfigurationFileLocator.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,85 @@ public void Verify(string? workingDirectory, string? projectRootDirectory)
7171
return null;
7272
}
7373

74+
public string? GetConfigurationFileTemp(string? directoryPath)
75+
{
76+
// If configuration file overriden and its exists, return it
77+
if (!string.IsNullOrWhiteSpace(this.ConfigurationFile) &&
78+
PathHelper.IsPathRooted(this.ConfigurationFile) &&
79+
fileSystem.File.Exists(this.ConfigurationFile))
80+
{
81+
return this.ConfigurationFile;
82+
}
83+
84+
if (directoryPath is null) return null;
85+
86+
string[] candidates = !string.IsNullOrWhiteSpace(this.ConfigurationFile)
87+
? [this.ConfigurationFile, .. this.SupportedConfigFileNames]
88+
: this.SupportedConfigFileNames;
89+
90+
foreach (var fileName in candidates)
91+
{
92+
this.log.Debug($"Trying to find configuration file {fileName} at '{directoryPath}'");
93+
if (directoryPath != null && fileSystem.Directory.Exists(directoryPath))
94+
{
95+
var files = fileSystem.Directory.GetFiles(directoryPath);
96+
97+
var matchingFile = files.FirstOrDefault(file =>
98+
string.Equals(fileSystem.Path.GetFileName(file), fileName, StringComparison.OrdinalIgnoreCase));
99+
100+
if (matchingFile != null)
101+
{
102+
this.log.Info($"Found configuration file at '{matchingFile}'");
103+
return matchingFile;
104+
}
105+
}
106+
107+
this.log.Debug($"Configuration file {fileName} not found at '{directoryPath}'");
108+
}
109+
110+
return null;
111+
}
112+
113+
public string? GetConfigurationFileCombined(string? directoryPath)
114+
{
115+
if (!string.IsNullOrWhiteSpace(this.ConfigurationFile) &&
116+
PathHelper.IsPathRooted(this.ConfigurationFile) &&
117+
fileSystem.File.Exists(this.ConfigurationFile))
118+
{
119+
return this.ConfigurationFile;
120+
}
121+
122+
if (directoryPath is null) return null;
123+
124+
string[] configurationFilePaths = string.IsNullOrWhiteSpace(this.ConfigurationFile)
125+
? this.SupportedConfigFileNames : [this.ConfigurationFile, .. this.SupportedConfigFileNames];
126+
127+
foreach (var item in configurationFilePaths)
128+
{
129+
this.log.Debug($"Trying to find configuration file {item} at '{directoryPath}'");
130+
131+
string configurationFilePath;
132+
if (!PathHelper.IsPathRooted(item))
133+
{
134+
configurationFilePath = Path.Combine(directoryPath, item);
135+
}
136+
else
137+
{
138+
configurationFilePath = item;
139+
}
140+
141+
if (fileSystem.File.Exists(configurationFilePath))
142+
{
143+
this.log.Info($"Found configuration file at '{configurationFilePath}'");
144+
return configurationFilePath;
145+
}
146+
147+
this.log.Debug($"Configuration file {configurationFilePath} not found at '{directoryPath}'");
148+
}
149+
150+
return null;
151+
}
152+
74153
private void WarnAboutAmbiguousConfigFileSelection(string? workingDirectory, string? projectRootDirectory)
75154
{
76155
var workingConfigFile = GetConfigurationFile(workingDirectory);

src/GitVersion.Core/Configuration/IConfigurationFileLocator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ namespace GitVersion.Configuration;
33
public interface IConfigurationFileLocator
44
{
55
void Verify(string? workingDirectory, string? projectRootDirectory);
6-
string? GetConfigurationFile(string? directory);
6+
string? GetConfigurationFile(string? directoryPath);
7+
string? GetConfigurationFileTemp(string? directoryPath);
78
}

src/GitVersion.Core/PublicAPI.Shipped.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ GitVersion.Configuration.IConfigurationBuilder
113113
GitVersion.Configuration.IConfigurationBuilder.AddOverride(System.Collections.Generic.IReadOnlyDictionary<object!, object?>! value) -> void
114114
GitVersion.Configuration.IConfigurationBuilder.Build() -> GitVersion.Configuration.IGitVersionConfiguration!
115115
GitVersion.Configuration.IConfigurationFileLocator
116-
GitVersion.Configuration.IConfigurationFileLocator.GetConfigurationFile(string? directory) -> string?
116+
GitVersion.Configuration.IConfigurationFileLocator.GetConfigurationFile(string? directoryPath) -> string?
117+
GitVersion.Configuration.IConfigurationFileLocator.GetConfigurationFileTemp(string? directoryPath) -> string?
117118
GitVersion.Configuration.IConfigurationFileLocator.Verify(string? workingDirectory, string? projectRootDirectory) -> void
118119
GitVersion.Configuration.IConfigurationProvider
119120
GitVersion.Configuration.IConfigurationProvider.Provide(System.Collections.Generic.IReadOnlyDictionary<object!, object?>? overrideConfiguration = null) -> GitVersion.Configuration.IGitVersionConfiguration!

0 commit comments

Comments
 (0)