@@ -35,6 +35,17 @@ class PerformanceBenchmark {
3535 private testFiles : { name : string ; size : 'small' | 'medium' | 'large' } [ ] = [ ] ;
3636 private overallStartTime : bigint = BigInt ( 0 ) ;
3737
38+ private readonly baseline = {
39+ factoryCreation : { average : 0.0001675 , median : 0.0001522 } ,
40+ documentFormatting : {
41+ small : { average : 0.1429 , median : 0.1162 } ,
42+ medium : { average : 0.2358 , median : 0.2186 } ,
43+ large : { average : 4.2929 , median : 2.2453 }
44+ }
45+ } ;
46+
47+ private readonly thresholds = { improvement : 0.5 , warning : 1.5 , failure : 2 } ;
48+
3849 constructor ( ) {
3950 this . results = {
4051 factoryCreation : {
@@ -52,6 +63,55 @@ class PerformanceBenchmark {
5263 } ;
5364 }
5465
66+ private checkPerformanceRegression ( ) : void {
67+ console . log ( `\n📊 Comparing against hard-coded baseline` ) ;
68+
69+ // Factory creation comparison
70+ let hasRegression = this . comparePerformance (
71+ 'Factory creation' ,
72+ this . results . factoryCreation . average ,
73+ this . baseline . factoryCreation . average ,
74+ 6
75+ ) ;
76+
77+ // Document formatting comparisons
78+ for ( const [ size , results ] of Object . entries ( this . results . documentFormatting ) ) {
79+ if ( this . baseline . documentFormatting [ size ] ) {
80+ hasRegression = hasRegression || this . comparePerformance (
81+ `Document formatting (${ size } )` ,
82+ results . average ,
83+ this . baseline . documentFormatting [ size ] . average
84+ ) ;
85+ }
86+ }
87+
88+ if ( hasRegression ) {
89+ throw new Error ( 'Performance regression detected! Benchmark failed due to >25% performance degradation.' ) ;
90+ }
91+ }
92+
93+ private comparePerformance ( component : string , current : number , baseline : number , precision : number = 3 ) : boolean {
94+ const ratio = current / baseline ;
95+ const change = ( ratio - 1 ) * 100 ;
96+
97+ let isFailure : boolean = false ;
98+ let message : string ;
99+
100+ if ( ratio <= this . thresholds . improvement ) {
101+ message = `🎉 ${ component } : ${ current . toFixed ( precision ) } ms vs baseline ${ baseline . toFixed ( precision ) } ms (${ Math . abs ( change ) . toFixed ( 1 ) } % better)` ;
102+ } else if ( ratio >= this . thresholds . failure ) {
103+ isFailure = true ;
104+ message = `❌ ${ component } FAILURE: ${ current . toFixed ( precision ) } ms vs baseline ${ baseline . toFixed ( precision ) } ms (${ change . toFixed ( 1 ) } % slower)` ;
105+ } else if ( ratio >= this . thresholds . warning ) {
106+ message = `⚠️ ${ component } : ${ current . toFixed ( precision ) } ms vs baseline ${ baseline . toFixed ( precision ) } ms (${ change . toFixed ( 1 ) } % slower)` ;
107+ } else {
108+ message = `✅ ${ component } : ${ current . toFixed ( precision ) } ms vs baseline ${ baseline . toFixed ( precision ) } ms (${ change . toFixed ( 1 ) } % change)` ;
109+ }
110+
111+ console . log ( message ) ;
112+ return isFailure ;
113+ }
114+
55115 async loadTestFiles ( ) : Promise < void > {
56116 const resourcesDir = path . resolve ( __dirname , "resources/" ) ;
57117 const files = fs . readdirSync ( resourcesDir ) ;
@@ -189,6 +249,8 @@ class PerformanceBenchmark {
189249
190250 this . printResults ( ) ;
191251 this . saveResults ( ) ;
252+
253+ this . checkPerformanceRegression ( ) ;
192254 }
193255
194256 private printResults ( ) : void {
0 commit comments