Skip to content

Commit

Permalink
Add Benchmark project
Browse files Browse the repository at this point in the history
Co-Authored-By: BinarySpace <29510799+YetAnotherBinarySpace@users.noreply.github.com>
Co-Authored-By: Luzifix <7042325+luzifix@users.noreply.github.com>
  • Loading branch information
3 people committed Aug 8, 2024
1 parent 25224bb commit 7dd1077
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
40 changes: 40 additions & 0 deletions DBCD.Benchmark/Benchmarks/WritingBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using BenchmarkDotNet.Attributes;
using DBCD.Benchmark.Utilities;
using DBCD.Providers;

namespace DBCD.Benchmark.Benchmarks
{
[MemoryDiagnoser]
public class WritingBenchmark
{
public static GithubDBDProvider DBDProvider { get; } = new GithubDBDProvider(true);
public static string InputPath { get; } = $"{Directory.GetCurrentDirectory()}\\dbc";
public static DBCD InputDBCD { get; } = new DBCD(new FilesystemDBCProvider(InputPath), DBDProvider);
public static DBCD SavedDBCD { get; } = new DBCD(new FilesystemDBCProvider("tmp"), DBDProvider);

public static string Build { get; } = "9.1.0.39653";

[Benchmark]
public void TestWritingAllDB2s()
{
string[] allDB2s = Directory.GetFiles(InputPath, "*.db2", SearchOption.TopDirectoryOnly);

if (Directory.Exists("tmp"))
Directory.Delete("tmp", true);
Directory.CreateDirectory("tmp");

foreach (var db2File in allDB2s)
{
if (Utilities.IO.TryGetExactPath(db2File, out string exactPath))
{
var tableName = Path.GetFileNameWithoutExtension(exactPath);

var originalStorage = InputDBCD.Load(tableName, Build);
originalStorage.Save($"tmp/{tableName}.db2");
}
}

Directory.Delete("tmp", true);
}
}
}
18 changes: 18 additions & 0 deletions DBCD.Benchmark/DBCD.Benchmark.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DBCD\DBCD.csproj" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions DBCD.Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// See https://aka.ms/new-console-template for more information
using BenchmarkDotNet.Running;
using DBCD.Benchmark.Benchmarks;

BenchmarkRunner.Run<WritingBenchmark>();
53 changes: 53 additions & 0 deletions DBCD.Benchmark/Utilities/IO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;

namespace DBCD.Benchmark.Utilities
{
internal class IO
{
public static bool TryGetExactPath(string path, out string exactPath)
{
bool result = false;
exactPath = null;

// DirectoryInfo accepts either a file path or a directory path, and most of its properties work for either.
// However, its Exists property only works for a directory path.
DirectoryInfo directory = new DirectoryInfo(path);
if (File.Exists(path) || directory.Exists)
{
List<string> parts = new List<string>();

DirectoryInfo parentDirectory = directory.Parent;
while (parentDirectory != null)
{
FileSystemInfo entry = parentDirectory.EnumerateFileSystemInfos(directory.Name).First();
parts.Add(entry.Name);

directory = parentDirectory;
parentDirectory = directory.Parent;
}

// Handle the root part (i.e., drive letter or UNC \\server\share).
string root = directory.FullName;
if (root.Contains(':'))
{
root = root.ToUpper();
}
else
{
string[] rootParts = root.Split('\\');
root = string.Join("\\", rootParts.Select(part => CultureInfo.CurrentCulture.TextInfo.ToTitleCase(part)));
}

parts.Add(root);
parts.Reverse();
exactPath = Path.Combine(parts.ToArray());
result = true;
}

return result;
}
}
}
6 changes: 6 additions & 0 deletions DBCD.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DBCD.IO", "DBCD.IO\DBCD.IO.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DBCD.Tests", "DBCD.Tests\DBCD.Tests.csproj", "{96CFC512-3818-487F-8FB6-7632E340ABB9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DBCD.Benchmark", "DBCD.Benchmark\DBCD.Benchmark.csproj", "{5EA3D33B-9901-48CB-B558-0D8A90F2CD7C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +29,10 @@ Global
{96CFC512-3818-487F-8FB6-7632E340ABB9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{96CFC512-3818-487F-8FB6-7632E340ABB9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{96CFC512-3818-487F-8FB6-7632E340ABB9}.Release|Any CPU.Build.0 = Release|Any CPU
{5EA3D33B-9901-48CB-B558-0D8A90F2CD7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5EA3D33B-9901-48CB-B558-0D8A90F2CD7C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5EA3D33B-9901-48CB-B558-0D8A90F2CD7C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5EA3D33B-9901-48CB-B558-0D8A90F2CD7C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 7dd1077

Please sign in to comment.