Skip to content

Instantly share code, notes, and snippets.

@sachinsu
Created May 5, 2020 06:18
Show Gist options
  • Save sachinsu/b2d914a563b49d4dd50a4143166f27ec to your computer and use it in GitHub Desktop.
Save sachinsu/b2d914a563b49d4dd50a4143166f27ec to your computer and use it in GitHub Desktop.
Using BenchmarkDotNET to benchmark File Generation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bogus;
namespace CSVGeneration
{
public class Card
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
public string CardNo { get; set; }
public string Address { get; set; }
public static List<Card> GenerateCardData(int count)
{
List<Card> cards = new List<Card>(count);
var fObject = new Faker<Card>()
.RuleFor(o => o.FirstName, f => f.Random.String(5, 10))
.RuleFor(o => o.LastName, f => f.Random.String(5, 10))
.RuleFor(o => o.BirthDate, f => f.Date.Past())
.RuleFor(o=>o.CardNo,f=>f.Random.AlphaNumeric(16))
.RuleFor(o => o.Address, f => f.Random.String(50, 100));
for (var i = 0;i < count;i++)
{
cards.Add(fObject.Generate());
}
return cards;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using CsvHelper;
using System.Globalization;
namespace CSVGeneration
{
public class CardWriter
{
private string GetFileWithPath()
{
return Path.Combine(@"d:\temp\", Path.GetRandomFileName());
}
public void UseFileIONoStringBuilder(List<Card> cards,int BufferSize = 2048)
{
string fileToWrite = GetFileWithPath();
System.Diagnostics.Debug.WriteLine("UseFileIONoStringBuilder, File Name:{0}",fileToWrite);
using (StreamWriter writer = new StreamWriter(fileToWrite, false, Encoding.UTF8, BufferSize)) {
writer.AutoFlush = false;
cards.ForEach((card) =>
{
writer.WriteLine(string.Format("{0}|{1}|{2}|{3}",
card.FirstName, card.LastName, card.BirthDate, card.Address));
});
writer.Flush();
}
}
public void UseFileIOWithStringBuilder(List<Card> cards, int BufferSize = 2048)
{
string fileToWrite = GetFileWithPath();
StringBuilder builder = new StringBuilder(BufferSize);
System.Diagnostics.Debug.WriteLine("UseFileIOWithStringBuilder, File Name:{0}", fileToWrite);
using (StreamWriter writer = new StreamWriter(fileToWrite, false, Encoding.UTF8, BufferSize))
{
writer.AutoFlush = false;
cards.ForEach((card) =>
{
builder.AppendLine(string.Format("{0}|{1}|{2}|{3}",
card.FirstName, card.LastName, card.BirthDate, card.Address));
if (builder.Length >= BufferSize)
{
writer.Write(builder.ToString());
builder.Clear();
writer.Flush();
}
});
writer.Flush();
}
}
public void UseFileIOWithCsvHelper(List<Card> cards, int BufferSize = 2048)
{
string fileToWrite = GetFileWithPath();
System.Diagnostics.Debug.WriteLine("UseFileIOWithCsvHelper, File Name:{0}", fileToWrite);
var config = new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture);
config.BufferSize = BufferSize;
config.Delimiter = "|";
using (StreamWriter writer = new StreamWriter(fileToWrite,false,Encoding.UTF8,BufferSize)) {
using(var csWriter = new CsvWriter(writer, config))
{
csWriter.WriteRecords(cards);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
namespace CSVGeneration
{
[SimpleJob(RuntimeMoniker.Net461, baseline: true)]
[SimpleJob(RuntimeMoniker.Net48)]
[MemoryDiagnoser]
[RPlotExporter]
[RyuJitX64Job]
public class FIleGeneratorBenchmark
{
private CardWriter cardwriter = new CardWriter();
private List<Card> data;
[Params(10000)]
public int RecordCount;
[Params(2048,4192)]
public int BufferSize;
[GlobalSetup]
public void Setup()
{
data = Card.GenerateCardData(RecordCount);
}
[Benchmark]
public void streamwriter() => cardwriter.UseFileIONoStringBuilder(data, BufferSize);
[Benchmark]
public void withstringbuilder() => cardwriter.UseFileIOWithStringBuilder(data, BufferSize);
[Benchmark]
public void WithCsvWriter() => cardwriter.UseFileIOWithCsvHelper(data, BufferSize);
}
class Program
{
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<FIleGeneratorBenchmark>();
System.Diagnostics.Debug.WriteLine(summary);
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment