@@ -9,6 +9,7 @@ import { Config, ToolType } from './config';
99import { DEFAULT_INDEX_HTML } from './default_index_html' ;
1010import { leavePRComment } from './comment/leavePRComment' ;
1111import { leaveCommitComment } from './comment/leaveCommitComment' ;
12+ import { addBenchmarkEntry } from './addBenchmarkEntry' ;
1213
1314export type BenchmarkSuites = { [ name : string ] : Benchmark [ ] } ;
1415export interface DataJson {
@@ -321,39 +322,16 @@ function addBenchmarkToDataJson(
321322 bench : Benchmark ,
322323 data : DataJson ,
323324 maxItems : number | null ,
324- ) : Benchmark | null {
325+ ) : { prevBench : Benchmark | null ; normalizedCurrentBench : Benchmark } {
325326 const repoMetadata = getCurrentRepoMetadata ( ) ;
326327 const htmlUrl = repoMetadata . html_url ?? '' ;
327328
328- let prevBench : Benchmark | null = null ;
329329 data . lastUpdate = Date . now ( ) ;
330330 data . repoUrl = htmlUrl ;
331331
332- // Add benchmark result
333- if ( data . entries [ benchName ] === undefined ) {
334- data . entries [ benchName ] = [ bench ] ;
335- core . debug ( `No suite was found for benchmark '${ benchName } ' in existing data. Created` ) ;
336- } else {
337- const suites = data . entries [ benchName ] ;
338- // Get last suite which has different commit ID for alert comment
339- for ( const e of suites . slice ( ) . reverse ( ) ) {
340- if ( e . commit . id !== bench . commit . id ) {
341- prevBench = e ;
342- break ;
343- }
344- }
332+ const { prevBench, normalizedCurrentBench } = addBenchmarkEntry ( benchName , bench , data . entries , maxItems ) ;
345333
346- suites . push ( bench ) ;
347-
348- if ( maxItems !== null && suites . length > maxItems ) {
349- suites . splice ( 0 , suites . length - maxItems ) ;
350- core . debug (
351- `Number of data items for '${ benchName } ' was truncated to ${ maxItems } due to max-items-in-charts` ,
352- ) ;
353- }
354- }
355-
356- return prevBench ;
334+ return { prevBench, normalizedCurrentBench } ;
357335}
358336
359337function isRemoteRejectedError ( err : unknown ) : err is Error {
@@ -367,7 +345,7 @@ async function writeBenchmarkToGitHubPagesWithRetry(
367345 bench : Benchmark ,
368346 config : Config ,
369347 retry : number ,
370- ) : Promise < Benchmark | null > {
348+ ) : Promise < { prevBench : Benchmark | null ; normalizedCurrentBench : Benchmark } > {
371349 const {
372350 name,
373351 tool,
@@ -421,7 +399,7 @@ async function writeBenchmarkToGitHubPagesWithRetry(
421399 await io . mkdirP ( benchmarkDataDirFullPath ) ;
422400
423401 const data = await loadDataJs ( dataPath ) ;
424- const prevBench = addBenchmarkToDataJson ( name , bench , data , maxItemsInChart ) ;
402+ const { prevBench, normalizedCurrentBench } = addBenchmarkToDataJson ( name , bench , data , maxItemsInChart ) ;
425403
426404 await storeDataJs ( dataPath , data ) ;
427405
@@ -469,10 +447,13 @@ async function writeBenchmarkToGitHubPagesWithRetry(
469447 ) ;
470448 }
471449
472- return prevBench ;
450+ return { prevBench, normalizedCurrentBench } ;
473451}
474452
475- async function writeBenchmarkToGitHubPages ( bench : Benchmark , config : Config ) : Promise < Benchmark | null > {
453+ async function writeBenchmarkToGitHubPages (
454+ bench : Benchmark ,
455+ config : Config ,
456+ ) : Promise < { prevBench : Benchmark | null ; normalizedCurrentBench : Benchmark } > {
476457 const { ghPagesBranch, skipFetchGhPages, ghRepository, githubToken } = config ;
477458 if ( ! ghRepository ) {
478459 if ( ! skipFetchGhPages ) {
@@ -508,14 +489,14 @@ async function writeBenchmarkToExternalJson(
508489 bench : Benchmark ,
509490 jsonFilePath : string ,
510491 config : Config ,
511- ) : Promise < Benchmark | null > {
492+ ) : Promise < { prevBench : Benchmark | null ; normalizedCurrentBench : Benchmark } > {
512493 const { name, maxItemsInChart, saveDataFile } = config ;
513494 const data = await loadDataJson ( jsonFilePath ) ;
514- const prevBench = addBenchmarkToDataJson ( name , bench , data , maxItemsInChart ) ;
495+ const { prevBench, normalizedCurrentBench } = addBenchmarkToDataJson ( name , bench , data , maxItemsInChart ) ;
515496
516497 if ( ! saveDataFile ) {
517498 core . debug ( 'Skipping storing benchmarks in external data file' ) ;
518- return prevBench ;
499+ return { prevBench, normalizedCurrentBench } ;
519500 }
520501
521502 try {
@@ -526,12 +507,12 @@ async function writeBenchmarkToExternalJson(
526507 throw new Error ( `Could not store benchmark data as JSON at ${ jsonFilePath } : ${ err } ` ) ;
527508 }
528509
529- return prevBench ;
510+ return { prevBench, normalizedCurrentBench } ;
530511}
531512
532513export async function writeBenchmark ( bench : Benchmark , config : Config ) {
533514 const { name, externalDataJsonPath } = config ;
534- const prevBench = externalDataJsonPath
515+ const { prevBench, normalizedCurrentBench } = externalDataJsonPath
535516 ? await writeBenchmarkToExternalJson ( bench , externalDataJsonPath , config )
536517 : await writeBenchmarkToGitHubPages ( bench , config ) ;
537518
@@ -540,9 +521,9 @@ export async function writeBenchmark(bench: Benchmark, config: Config) {
540521 if ( prevBench === null ) {
541522 core . debug ( 'Alert check was skipped because previous benchmark result was not found' ) ;
542523 } else {
543- await handleComment ( name , bench , prevBench , config ) ;
544- await handleSummary ( name , bench , prevBench , config ) ;
545- await handleAlert ( name , bench , prevBench , config ) ;
524+ await handleComment ( name , normalizedCurrentBench , prevBench , config ) ;
525+ await handleSummary ( name , normalizedCurrentBench , prevBench , config ) ;
526+ await handleAlert ( name , normalizedCurrentBench , prevBench , config ) ;
546527 }
547528}
548529
0 commit comments