Skip to content

Commit

Permalink
Adding benchmark project (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
shibayan authored Jun 8, 2020
1 parent 3fc792b commit 654845c
Show file tree
Hide file tree
Showing 7 changed files with 703 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

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

<ItemGroup>
<ProjectReference Include="..\CsvHelper.FastDynamic\CsvHelper.FastDynamic.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="sampledata\SFO_Airport_Monthly_Utility_Consumption_for_Natural_Gas__Water__and_Electricity.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions CsvHelper.FastDynamic.Performance/Internal/CsvReaderExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections.Generic;
using System.Linq;

namespace CsvHelper.FastDynamic.Performance.Internal
{
internal static class CsvReaderExtension
{
internal static IList<IDictionary<string, object>> GetDictionaryRecords(this CsvReader csvReader)
{
// Read Header
csvReader.Read();
csvReader.ReadHeader();

var headerRecord = csvReader.Context
.HeaderRecord
.Select((x, i) => csvReader.Configuration.PrepareHeaderForMatch(x, i))
.ToArray();

var result = new List<IDictionary<string, object>>();

while (csvReader.Read())
{
var record = new Dictionary<string, object>(headerRecord.Length);

for (int i = 0; i < headerRecord.Length; i++)
{
record[headerRecord[i]] = csvReader[i];
}

result.Add(record);
}

return result;
}
}
}
12 changes: 12 additions & 0 deletions CsvHelper.FastDynamic.Performance/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using BenchmarkDotNet.Running;

namespace CsvHelper.FastDynamic.Performance
{
class Program
{
static void Main(string[] args)
{
BenchmarkRunner.Run<ReaderBenchmark>();
}
}
}
48 changes: 48 additions & 0 deletions CsvHelper.FastDynamic.Performance/ReaderBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;

using BenchmarkDotNet.Attributes;

using CsvHelper.FastDynamic.Performance.Internal;

namespace CsvHelper.FastDynamic.Performance
{
[MemoryDiagnoser]
public class ReaderBenchmark
{
private const string SampleCsvFile = @".\sampledata\SFO_Airport_Monthly_Utility_Consumption_for_Natural_Gas__Water__and_Electricity.csv";

public ReaderBenchmark()
{
_sampleCsvData = File.ReadAllText(SampleCsvFile);
}

private readonly string _sampleCsvData;

[Benchmark]
public IReadOnlyList<dynamic> GetRecords()
{
using var csvReader = new CsvReader(new StringReader(_sampleCsvData), CultureInfo.InvariantCulture);

return csvReader.GetRecords<dynamic>().ToArray();
}

[Benchmark]
public IReadOnlyList<IDictionary<string, object>> GetDictionaryRecords()
{
using var csvReader = new CsvReader(new StringReader(_sampleCsvData), CultureInfo.InvariantCulture);

return csvReader.GetDictionaryRecords().ToArray();
}

[Benchmark]
public IReadOnlyList<dynamic> GetDynamicRecords()
{
using var csvReader = new CsvReader(new StringReader(_sampleCsvData), CultureInfo.InvariantCulture);

return csvReader.GetDynamicRecords();
}
}
}
Loading

0 comments on commit 654845c

Please sign in to comment.