Improved code benchmarking (removed an unnecessary cost of image placeholder generation)

This commit is contained in:
Marcin Ziąbek
2021-11-03 21:22:08 +01:00
parent 22cdad69b7
commit 520bfc840c
7 changed files with 45 additions and 43 deletions
+7 -1
View File
@@ -21,8 +21,14 @@ namespace QuestPDF.Examples
page.Padding(25).Stack(stack =>
{
stack.Spacing(25);
stack.Item().Image(File.ReadAllBytes("logo.png"));
stack.Item().Image("logo.png");
var binaryData = File.ReadAllBytes("logo.png");
stack.Item().Image(binaryData);
using var stream = new FileStream("logo.png", FileMode.Open);
stack.Item().Image(stream);
});
});
}
+2 -2
View File
@@ -113,8 +113,8 @@ namespace QuestPDF.ReportSample
Date = DateTime.Now - TimeSpan.FromDays(Helpers.Random.NextDouble() * 100),
Location = Helpers.RandomLocation(),
MapContextSource = x => Placeholders.Image(400, 300),
MapDetailsSource = x => Placeholders.Image(400, 300)
MapContextSource = Placeholders.Image,
MapDetailsSource = Placeholders.Image
};
}
}
@@ -0,0 +1,20 @@
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
namespace QuestPDF.ReportSample.Layouts
{
public class ImagePlaceholder : IComponent
{
public static bool Solid { get; set; } = false;
public void Compose(IContainer container)
{
if (Solid)
container.Background(Placeholders.Color());
else
container.Image(Placeholders.Image);
}
}
}
@@ -1,32 +0,0 @@
using System;
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
namespace QuestPDF.ReportSample.Layouts
{
public class ImageTemplate : IComponent
{
private Func<Size, byte[]> Source { get; }
private float AspectRatio { get; }
public ImageTemplate(byte[] source, float aspectRatio = 1.333333f) : this(_ => source, aspectRatio)
{
}
public ImageTemplate(Func<Size, byte[]> source, float aspectRatio = 1.333333f)
{
Source = source;
AspectRatio = aspectRatio;
}
public void Compose(IContainer container)
{
container
.AspectRatio(AspectRatio)
.Background(Colors.Grey.Lighten3)
.Image(Source(Size.Zero));
}
}
}
@@ -30,14 +30,14 @@ namespace QuestPDF.ReportSample.Layouts
container
.Row(row =>
{
row.RelativeColumn(2).AspectRatio(4 / 3f).Image(Placeholders.Image);
row.RelativeColumn(2).AspectRatio(4 / 3f).Component<ImagePlaceholder>();
row.RelativeColumn().PaddingLeft(5).Stack(stack =>
{
stack.Spacing(7f);
stack.Item().AspectRatio(4 / 3f).Image(Placeholders.Image);
stack.Item().AspectRatio(4 / 3f).Image(Placeholders.Image);
stack.Item().AspectRatio(4 / 3f).Component<ImagePlaceholder>();
stack.Item().AspectRatio(4 / 3f).Component<ImagePlaceholder>();
});
});
}
@@ -59,7 +59,7 @@ namespace QuestPDF.ReportSample.Layouts
{
stack.Spacing(5);
stack.Item().MaxWidth(250).AspectRatio(4 / 3f).Image(Placeholders.Image);
stack.Item().MaxWidth(250).AspectRatio(4 / 3f).Component<ImagePlaceholder>();
stack.Item().Text(model.Location.Format());
});
}
@@ -77,7 +77,7 @@ namespace QuestPDF.ReportSample.Layouts
grid.Spacing(5);
grid.Columns(3);
model.Photos.ForEach(x => grid.Item().AspectRatio(4 / 3f).Image(Placeholders.Image));
model.Photos.ForEach(x => grid.Item().AspectRatio(4 / 3f).Component<ImagePlaceholder>());
});
}
}
+11 -3
View File
@@ -15,7 +15,7 @@ namespace QuestPDF.ReportSample
{
var reportModel = DataSource.GetReport();
var report = new StandardReport(reportModel);
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"test_result.pdf");
report.GeneratePdf(path);
@@ -23,13 +23,16 @@ namespace QuestPDF.ReportSample
}
[Test]
public void PerformanceBenchmark()
public void PerformanceBenchmark()
{
// target document length should be around 100 pages
// test size
const int testSize = 10;
const decimal performanceTarget = 1; // documents per second
// generating placeholder images is slow, for benchmarking reasons, replace images with simple colorful boxes
ImagePlaceholder.Solid = true;
// create report models
var reports = Enumerable
@@ -45,9 +48,14 @@ namespace QuestPDF.ReportSample
var sw = new Stopwatch();
sw.Start();
var totalSize = reports.Select(x => x.GeneratePdf()).Sum(x => (long)x.Length);
var totalSize = BenchmarkAndGenerate();
sw.Stop();
long BenchmarkAndGenerate()
{
return reports.Select(x => x.GeneratePdf()).Sum(x => (long)x.Length);;
}
// show summary
Console.WriteLine($"Total size: {totalSize:N0} bytes");