Skip to content

Commit 9305f5d

Browse files
authored
Merge pull request #358 from SyncfusionExamples/Benchmark-Samples
Add Syncfusion XlsIO performance benchmark sample
2 parents e072d66 + fe2491b commit 9305f5d

4 files changed

Lines changed: 233 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System.Data;
2+
using System.Diagnostics;
3+
4+
static class BenchmarkHost
5+
{
6+
public const int RowCount = 100_000;
7+
public const int ColumnCount = 50;
8+
9+
public static int Main(string[] args)
10+
{
11+
if (args.Length < 1)
12+
{
13+
Console.WriteLine("Usage: XlsIO.PerformanceBenchmark <Method>");
14+
Console.WriteLine("Methods: OpenAndSave, CreateAndSave, ImportDataTable, ExportDataTable, ExcelToCsv, ExcelToJson, ExcelToPdf");
15+
return 1;
16+
}
17+
18+
string method = args[0];
19+
string dataDir = Path.Combine(AppContext.BaseDirectory, "Output");
20+
Directory.CreateDirectory(dataDir);
21+
22+
DataTable table = BuildTable(RowCount, ColumnCount);
23+
string sourcePath = Path.Combine(dataDir, "source.xlsx");
24+
if (!File.Exists(sourcePath))
25+
{
26+
WriteSourceWorkbook(sourcePath, table);
27+
}
28+
29+
var clock = Stopwatch.StartNew();
30+
SyncfusionXlsIO.Run(method, sourcePath, table, Path.Combine(dataDir, method));
31+
clock.Stop();
32+
33+
Console.WriteLine($"{method} rows={RowCount} time={clock.Elapsed.TotalMilliseconds:F0} ms");
34+
return 0;
35+
}
36+
37+
public static DataTable BuildTable(int rows, int columns)
38+
{
39+
var table = new DataTable("Sales");
40+
for (int c = 0; c < columns; c++)
41+
{
42+
table.Columns.Add(c % 5 == 4 ? $"Amount{c}" : $"Col{c}", c % 5 == 4 ? typeof(double) : typeof(string));
43+
}
44+
45+
for (int r = 0; r < rows; r++)
46+
{
47+
var values = new object[columns];
48+
for (int c = 0; c < columns; c++)
49+
{
50+
values[c] = c % 5 == 4 ? (object)((r * 17 + c) % 10000 / 100.0) : $"R{r}C{c}";
51+
}
52+
53+
table.Rows.Add(values);
54+
}
55+
56+
return table;
57+
}
58+
59+
static void WriteSourceWorkbook(string path, DataTable table)
60+
{
61+
using var engine = new Syncfusion.XlsIO.ExcelEngine();
62+
Syncfusion.XlsIO.IApplication application = engine.Excel;
63+
application.DefaultVersion = Syncfusion.XlsIO.ExcelVersion.Xlsx;
64+
Syncfusion.XlsIO.IWorkbook workbook = application.Workbooks.Create(1);
65+
workbook.Worksheets[0].ImportDataTable(table, true, 1, 1);
66+
using FileStream stream = File.Create(path);
67+
workbook.SaveAs(stream);
68+
workbook.Close();
69+
}
70+
71+
public static void EnsureRows(int actual, int expected)
72+
{
73+
if (actual < expected || actual > expected + 2)
74+
{
75+
throw new InvalidOperationException($"Unexpected row count {actual}; expected {expected}.");
76+
}
77+
}
78+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Performance benchmark
2+
3+
`XlsIO.PerformanceBenchmark` is a .NET 9 console app that measures the elapsed time of Syncfusion XlsIO operations on a workbook with 100,000 rows by 50 columns. Each run executes one method:
4+
5+
- `OpenAndSave`
6+
- `CreateAndSave`
7+
- `ImportDataTable`
8+
- `ExportDataTable`
9+
- `ExcelToCsv`
10+
- `ExcelToJson`
11+
- `ExcelToPdf`
12+
13+
```bash
14+
dotnet run --project XlsIO.PerformanceBenchmark -c Release -- ExcelToCsv
15+
```
16+
17+
Generated files are written to the `Output` folder next to the built application.
18+
19+
NuGet packages: `Syncfusion.XlsIO.Net.Core`, `Syncfusion.XlsIORenderer.Net.Core`, and `Syncfusion.Pdf.Net.Core` (35.1.41).
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using System.Data;
2+
using Syncfusion.XlsIO;
3+
using Syncfusion.XlsIORenderer;
4+
using Syncfusion.Pdf;
5+
6+
static class SyncfusionXlsIO
7+
{
8+
public static void Run(string method, string sourcePath, DataTable table, string outputStem)
9+
{
10+
switch (method)
11+
{
12+
case "OpenAndSave": OpenAndSave(sourcePath, outputStem); break;
13+
case "CreateAndSave": CreateAndSave(table, outputStem); break;
14+
case "ImportDataTable": ImportDataTable(table, outputStem); break;
15+
case "ExportDataTable": ExportDataTable(sourcePath, table.Rows.Count); break;
16+
case "ExcelToCsv": ExcelToCsv(sourcePath, outputStem); break;
17+
case "ExcelToJson": ExcelToJson(sourcePath, outputStem); break;
18+
case "ExcelToPdf": ExcelToPdf(sourcePath, outputStem); break;
19+
default: throw new ArgumentException($"Unknown method '{method}'.");
20+
}
21+
}
22+
23+
public static void OpenAndSave(string sourcePath, string outputStem)
24+
{
25+
using var engine = new ExcelEngine();
26+
IApplication application = engine.Excel;
27+
application.DefaultVersion = ExcelVersion.Xlsx;
28+
IWorkbook workbook = application.Workbooks.Open(sourcePath);
29+
using FileStream stream = File.Create(outputStem + ".xlsx");
30+
workbook.SaveAs(stream);
31+
workbook.Close();
32+
}
33+
34+
public static void CreateAndSave(DataTable table, string outputStem)
35+
{
36+
using var engine = new ExcelEngine();
37+
IApplication application = engine.Excel;
38+
application.DefaultVersion = ExcelVersion.Xlsx;
39+
IWorkbook workbook = application.Workbooks.Create(1);
40+
IWorksheet sheet = workbook.Worksheets[0];
41+
WriteGrid(sheet, table);
42+
using FileStream stream = File.Create(outputStem + ".xlsx");
43+
workbook.SaveAs(stream);
44+
workbook.Close();
45+
}
46+
47+
public static void ImportDataTable(DataTable table, string outputStem)
48+
{
49+
using var engine = new ExcelEngine();
50+
IApplication application = engine.Excel;
51+
application.DefaultVersion = ExcelVersion.Xlsx;
52+
IWorkbook workbook = application.Workbooks.Create(1);
53+
workbook.Worksheets[0].ImportDataTable(table, true, 1, 1);
54+
using FileStream stream = File.Create(outputStem + ".xlsx");
55+
workbook.SaveAs(stream);
56+
workbook.Close();
57+
}
58+
59+
public static void ExportDataTable(string sourcePath, int expectedRows)
60+
{
61+
using var engine = new ExcelEngine();
62+
IApplication application = engine.Excel;
63+
IWorkbook workbook = application.Workbooks.Open(sourcePath);
64+
DataTable exported = workbook.Worksheets[0].ExportDataTable(workbook.Worksheets[0].UsedRange, ExcelExportDataTableOptions.ColumnNames);
65+
BenchmarkHost.EnsureRows(exported.Rows.Count, expectedRows);
66+
workbook.Close();
67+
}
68+
69+
public static void ExcelToCsv(string sourcePath, string outputStem)
70+
{
71+
using var engine = new ExcelEngine();
72+
IApplication application = engine.Excel;
73+
IWorkbook workbook = application.Workbooks.Open(sourcePath);
74+
workbook.SaveAs(outputStem + ".csv", ",");
75+
workbook.Close();
76+
}
77+
78+
public static void ExcelToJson(string sourcePath, string outputStem)
79+
{
80+
using var engine = new ExcelEngine();
81+
IApplication application = engine.Excel;
82+
IWorkbook workbook = application.Workbooks.Open(sourcePath);
83+
workbook.SaveAsJson(outputStem + ".json", workbook.Worksheets[0]);
84+
workbook.Close();
85+
}
86+
87+
public static void ExcelToPdf(string sourcePath, string outputStem)
88+
{
89+
using var engine = new ExcelEngine();
90+
IApplication application = engine.Excel;
91+
IWorkbook workbook = application.Workbooks.Open(sourcePath);
92+
var renderer = new XlsIORenderer();
93+
using PdfDocument pdf = renderer.ConvertToPDF(workbook);
94+
using FileStream stream = File.Create(outputStem + ".pdf");
95+
pdf.Save(stream);
96+
workbook.Close();
97+
}
98+
99+
static void WriteGrid(IWorksheet sheet, DataTable table)
100+
{
101+
int columns = table.Columns.Count;
102+
for (int c = 0; c < columns; c++)
103+
{
104+
sheet.SetValue(1, c + 1, table.Columns[c].ColumnName);
105+
}
106+
107+
for (int r = 0; r < table.Rows.Count; r++)
108+
{
109+
int row = r + 2;
110+
DataRow dataRow = table.Rows[r];
111+
for (int c = 0; c < columns; c++)
112+
{
113+
sheet.SetValue(row, c + 1, dataRow[c] is double amount ? amount.ToString(System.Globalization.CultureInfo.InvariantCulture) : (string)dataRow[c]);
114+
}
115+
}
116+
}
117+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<ServerGarbageCollection>true</ServerGarbageCollection>
9+
<ConcurrentGarbageCollection>true</ConcurrentGarbageCollection>
10+
<StartupObject>BenchmarkHost</StartupObject>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="35.1.41" />
15+
<PackageReference Include="Syncfusion.XlsIORenderer.Net.Core" Version="35.1.41" />
16+
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="35.1.41" />
17+
</ItemGroup>
18+
19+
</Project>

0 commit comments

Comments
 (0)