Skip to content

Instantly share code, notes, and snippets.

@rdingwall
Created July 19, 2012 15:51
Show Gist options
  • Save rdingwall/3144861 to your computer and use it in GitHub Desktop.
Save rdingwall/3144861 to your computer and use it in GitHub Desktop.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using CsvHelper.Configuration;
using Xunit;
namespace CsvHelper.Tests
{
public class CsvWriterBenchmarks
{
readonly Random random = new Random();
TestRecord GenerateRandomTestRecord()
{
return new TestRecord
{
IntColumn = random.Next(),
StringColumn = Path.GetRandomFileName(),
FirstColumn = Guid.NewGuid().ToString()
};
}
[Fact]
public void WriteRecordsTest()
{
var records = Enumerable.Range(0, 100000).Select(_ => GenerateRandomTestRecord()).ToArray();
Stopwatch sw;
using (var stream = File.OpenWrite(@"W:\rdingwall\ignore.csv"))
{
using (var writer = new StreamWriter(stream))
//var writer = new StreamWriter(stream, Encoding.Default, 8 * 1024 * 1024);
//using (var writer = new StreamWriter(stream, Encoding.Default, 512 * 1024))
using (var csv = new CsvWriter(writer))
{
sw = Stopwatch.StartNew();
csv.WriteRecords(records);
}
}
sw.Stop();
Console.WriteLine(sw.Elapsed);
}
[TypeConverter("type name")]
private class TestRecord
{
[CsvField(Index = 1, Name = "Int Column")]
[TypeConverter(typeof(Int32Converter))]
public int IntColumn { get; set; }
[TypeConverter("String")]
public string StringColumn { get; set; }
[CsvField(Ignore = true)]
public string IgnoredColumn { get; set; }
[CsvField(Index = 0)]
public string FirstColumn { get; set; }
[TypeConverter(typeof(TestTypeConverter))]
public string TypeConvertedColumn { get; set; }
}
private class TestTypeConverter : TypeConverter
{
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
return "test";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment