Tests.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using NUnit.Framework;
  6. using QuestPDF.Fluent;
  7. using QuestPDF.Infrastructure;
  8. using QuestPDF.ReportSample.Layouts;
  9. namespace QuestPDF.ReportSample
  10. {
  11. public class ReportGeneration
  12. {
  13. [Test]
  14. public void GenerateAndShow()
  15. {
  16. var reportModel = DataSource.GetReport();
  17. var report = new StandardReport(reportModel);
  18. var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"test_result.pdf");
  19. using var stream = new FileStream(path, FileMode.Create);
  20. report.GeneratePdf(stream);
  21. Process.Start("explorer.exe", path);
  22. }
  23. [Test]
  24. public void PerformanceBenchmark()
  25. {
  26. // test size
  27. const int testSize = 1000;
  28. const decimal performanceTarget = 5; // documents per second
  29. // create report models
  30. var reports = Enumerable
  31. .Range(0, testSize)
  32. .Select(x =>
  33. {
  34. var reportModel = DataSource.GetReport();
  35. return new StandardReport(reportModel);
  36. })
  37. .ToList();
  38. // generate documents
  39. var sw = new Stopwatch();
  40. sw.Start();
  41. var totalSize = reports.Select(x => x.GeneratePdf()).Sum(x => (long)x.Length);
  42. sw.Stop();
  43. // show summary
  44. Console.WriteLine($"Total size: {totalSize:N0} bytes");
  45. var performance = sw.ElapsedMilliseconds / (decimal)testSize;
  46. var speed = 1000M / performance;
  47. Console.WriteLine($"Test time: {sw.ElapsedMilliseconds} ms");
  48. Console.WriteLine($"Time per document: {performance:N} ms");
  49. Console.WriteLine($"Documents per second: {speed:N} d/s");
  50. if (speed < performanceTarget)
  51. throw new Exception("Rendering algorithm is too slow.");
  52. }
  53. }
  54. }