benchmarkReporter.spec.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { expect, test } from '@jupyterlab/galata';
  4. import { TestResult } from '@playwright/test/reporter';
  5. import BenchmarkReporter, {
  6. benchmark,
  7. IReportRecord
  8. } from '../../src/benchmarkReporter';
  9. import fs from 'fs';
  10. import path from 'path';
  11. import { JSONObject } from '@lumino/coreutils';
  12. const MOCK_DATA = {
  13. nSamples: 20,
  14. browser: 'chromium',
  15. file: 'large_code_notebook',
  16. project: 'benchmark',
  17. test: 'open',
  18. time: 18204,
  19. reference: 'actual'
  20. };
  21. const GENERAL_CONFIG = {
  22. $schema: 'https://vega.github.io/schema/vega-lite/v5.json',
  23. description: 'Box plots of some action time.',
  24. title: 'Duration of common actions',
  25. data: {
  26. values: [{ x: 0, y: 0 }]
  27. },
  28. mark: 'bar'
  29. };
  30. function mockTestResult(
  31. attachments: {
  32. name: string;
  33. path?: string;
  34. body?: Buffer;
  35. contentType: string;
  36. }[]
  37. ): TestResult {
  38. return {
  39. retry: 0,
  40. workerIndex: 0,
  41. startTime: new Date(),
  42. duration: 0,
  43. status: 'passed',
  44. attachments,
  45. stdout: [],
  46. stderr: [],
  47. steps: []
  48. };
  49. }
  50. function createReporter(options?: {
  51. outputFile?: string;
  52. comparison?: 'snapshot' | 'project';
  53. vegaLiteConfigFactory?: (
  54. allData: Array<IReportRecord>,
  55. comparison: 'snapshot' | 'project'
  56. ) => JSONObject;
  57. textReportFactory?: (
  58. allData: Array<IReportRecord>
  59. ) => Promise<[string, string]>;
  60. }): BenchmarkReporter {
  61. const rootDir = '../../';
  62. const reporter = new BenchmarkReporter(options);
  63. reporter.onBegin({ rootDir } as any, null);
  64. reporter.onTestEnd(
  65. null,
  66. mockTestResult([
  67. {
  68. name: benchmark.DEFAULT_NAME_ATTACHMENT,
  69. contentType: 'application/json',
  70. body: Buffer.from(JSON.stringify(MOCK_DATA))
  71. }
  72. ])
  73. );
  74. return reporter;
  75. }
  76. test.describe('BenchmarkReporter', () => {
  77. test('should generate report with default builders', async () => {
  78. const reporter = createReporter({
  79. outputFile: 'test.json',
  80. comparison: 'snapshot'
  81. });
  82. await reporter.onEnd({ status: 'passed' });
  83. const outputJson = path.resolve('.', `benchmark-results`, 'test.json');
  84. const outputData = JSON.parse(fs.readFileSync(outputJson, 'utf-8'));
  85. expect(outputData['values'][0]['time']).toBe(MOCK_DATA['time']);
  86. const outputMd = path.resolve('.', `benchmark-results`, 'test.md');
  87. const mdData = fs.readFileSync(outputMd, 'utf-8');
  88. expect(mdData).toContain('## Benchmark report');
  89. const outputPng = path.resolve('.', `benchmark-results`, 'test.png');
  90. const pngData = fs.readFileSync(outputPng);
  91. expect(pngData).toMatchSnapshot('test.png');
  92. });
  93. test('should generate report with user defined builders', async () => {
  94. const reporter = createReporter({
  95. outputFile: 'test.json',
  96. comparison: 'snapshot',
  97. textReportFactory: async allData => ['## This is a custom table', 'txt'],
  98. vegaLiteConfigFactory: (allData, comparison) => GENERAL_CONFIG
  99. });
  100. await reporter.onEnd({ status: 'passed' });
  101. const outputMd = path.resolve('.', 'benchmark-results', 'test.txt');
  102. const mdData = fs.readFileSync(outputMd, 'utf-8');
  103. expect(mdData).toContain('## This is a custom table');
  104. const outputPng = path.resolve('.', 'benchmark-results', 'test.png');
  105. const pngData = fs.readFileSync(outputPng);
  106. expect(pngData).toMatchSnapshot('customized_test.png');
  107. });
  108. });