-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExcelBenchmark.cs
84 lines (71 loc) · 2.18 KB
/
ExcelBenchmark.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using BenchmarkDotNet.Attributes;
using ExcelBattle.Models;
using ExcelBattle.Sut;
namespace ExcelBattle;
[MemoryDiagnoser]
[ThreadingDiagnoser]
public class ExcelBenchmark
{
private const string TemplateDir = "D:\\tmp\\";
private static string GenerateOutputFileName() => $"{TemplateDir}\\{Guid.NewGuid()}.xlsx";
private TemplateData _data = default!;
[Params(10, 1000, 100000)]
public int TotalRow { get; set; }
[GlobalSetup]
public void GlobalSetup()
{
_data = new TemplateData
{
Addresses = TestUtils.GenerateAddresses(TotalRow),
Companies = TestUtils.GenerateCompanies(TotalRow),
Contacts = TestUtils.GenerateContacts(TotalRow),
People = TestUtils.GeneratePeople(TotalRow),
Products = TestUtils.GenerateProducts(TotalRow)
};
}
[GlobalCleanup]
public void GlobalCleanup()
{
var di = new DirectoryInfo(TemplateDir);
foreach (var file in di.GetFiles())
{
file.Delete();
}
}
[Benchmark(Baseline = true)]
public void UseNpoi()
{
var fileName = GenerateOutputFileName();
NpoiExcelWriter.Write(_data, fileName);
}
[Benchmark]
public void UseCloseXml()
{
var fileName = GenerateOutputFileName();
CloseXmlExcelWriter.Write(_data, fileName);
}
[Benchmark]
public void UseMiniExcel()
{
var fileName = GenerateOutputFileName();
MiniExcelWriter.Write(_data, fileName);
}
[Benchmark]
public void UseOpenXmlSdk()
{
var fileName = GenerateOutputFileName();
OpenXmlSdkWriter.Write(_data, fileName);
}
public static void WriteSamples()
{
var sample = new ExcelBenchmark
{
TotalRow = 10
};
sample.GlobalSetup();
NpoiExcelWriter.Write(sample._data, $"{TemplateDir}\\ExportWithNpoi.xlsx");
CloseXmlExcelWriter.Write(sample._data, $"{TemplateDir}\\ExportWithCloseXML.xlsx");
MiniExcelWriter.Write(sample._data, $"{TemplateDir}\\ExportWithMiniExcel.xlsx");
OpenXmlSdkWriter.Write(sample._data, $"{TemplateDir}\\ExportWithOpenXmlSdk.xlsx");
}
}