浏览代码

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

Marcin Ziąbek 4 年之前
父节点
当前提交
520bfc840c

+ 7 - 1
QuestPDF.Examples/ImageExamples.cs

@@ -21,8 +21,14 @@ namespace QuestPDF.Examples
                     page.Padding(25).Stack(stack =>
                     page.Padding(25).Stack(stack =>
                     {
                     {
                         stack.Spacing(25);
                         stack.Spacing(25);
-                        stack.Item().Image(File.ReadAllBytes("logo.png"));
+                        
                         stack.Item().Image("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
QuestPDF.ReportSample/DataSource.cs

@@ -113,8 +113,8 @@ namespace QuestPDF.ReportSample
                     Date = DateTime.Now - TimeSpan.FromDays(Helpers.Random.NextDouble() * 100),
                     Date = DateTime.Now - TimeSpan.FromDays(Helpers.Random.NextDouble() * 100),
                     Location = Helpers.RandomLocation(),
                     Location = Helpers.RandomLocation(),
 
 
-                    MapContextSource = x => Placeholders.Image(400, 300),
-                    MapDetailsSource = x => Placeholders.Image(400, 300)
+                    MapContextSource = Placeholders.Image,
+                    MapDetailsSource = Placeholders.Image
                 };
                 };
             }
             }
         }
         }

+ 20 - 0
QuestPDF.ReportSample/Layouts/ImagePlaceholder.cs

@@ -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);
+        }
+    }
+}

+ 0 - 32
QuestPDF.ReportSample/Layouts/ImageTemplate.cs

@@ -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));
-        }
-    }
-}

+ 3 - 3
QuestPDF.ReportSample/Layouts/PhotoTemplate.cs

@@ -30,14 +30,14 @@ namespace QuestPDF.ReportSample.Layouts
             container
             container
                 .Row(row =>
                 .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 =>
                     row.RelativeColumn().PaddingLeft(5).Stack(stack =>
                     {
                     {
                         stack.Spacing(7f);
                         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>();
                     });
                     });
                 });
                 });
         }
         }

+ 2 - 2
QuestPDF.ReportSample/Layouts/SectionTemplate.cs

@@ -59,7 +59,7 @@ namespace QuestPDF.ReportSample.Layouts
             {
             {
                 stack.Spacing(5);
                 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());
                 stack.Item().Text(model.Location.Format());
             });
             });
         }
         }
@@ -77,7 +77,7 @@ namespace QuestPDF.ReportSample.Layouts
                 grid.Spacing(5);
                 grid.Spacing(5);
                 grid.Columns(3);
                 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
QuestPDF.ReportSample/Tests.cs

@@ -15,7 +15,7 @@ namespace QuestPDF.ReportSample
         {
         {
             var reportModel = DataSource.GetReport();
             var reportModel = DataSource.GetReport();
             var report = new StandardReport(reportModel);
             var report = new StandardReport(reportModel);
-            
+
             var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"test_result.pdf");
             var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"test_result.pdf");
             report.GeneratePdf(path);
             report.GeneratePdf(path);
             
             
@@ -23,13 +23,16 @@ namespace QuestPDF.ReportSample
         }
         }
 
 
         [Test] 
         [Test] 
-        public void PerformanceBenchmark()
+         public void PerformanceBenchmark()
         {
         {
             // target document length should be around 100 pages
             // target document length should be around 100 pages
             
             
             // test size
             // test size
             const int testSize = 10;
             const int testSize = 10;
             const decimal performanceTarget = 1; // documents per second
             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
             // create report models
             var reports = Enumerable
             var reports = Enumerable
@@ -45,9 +48,14 @@ namespace QuestPDF.ReportSample
             var sw = new Stopwatch();
             var sw = new Stopwatch();
             
             
             sw.Start();
             sw.Start();
-            var totalSize = reports.Select(x => x.GeneratePdf()).Sum(x => (long)x.Length);
+            var totalSize = BenchmarkAndGenerate();
             sw.Stop();
             sw.Stop();
 
 
+            long BenchmarkAndGenerate()
+            {
+                return reports.Select(x => x.GeneratePdf()).Sum(x => (long)x.Length);;
+            }
+            
             // show summary
             // show summary
             Console.WriteLine($"Total size: {totalSize:N0} bytes");
             Console.WriteLine($"Total size: {totalSize:N0} bytes");