@@ -124,4 +124,46 @@ export function assertPerformance(
124124 `Errors ${ result . errors } exceeds threshold ${ thresholds . maxErrors } `
125125 ) ;
126126 }
127- }
127+
128+ testResults = result ;
129+ }
130+
131+ const PERF_LOGGING_API_KEY = process . env . POSTHOG_PERF_API_KEY ;
132+ let testResults : PerformanceResult | undefined = undefined ;
133+ beforeEach ( ( ) => {
134+ testResults = undefined ;
135+ } ) ;
136+
137+ afterEach ( async function ( ) {
138+ if ( PERF_LOGGING_API_KEY ) {
139+ if ( ! this . currentTest ) throw new Error ( "Can't log perf results without a test context" ) ;
140+ if ( this . currentTest . state !== 'passed' ) return ;
141+ if ( ! testResults ) throw new Error ( "Can't log perf results without test results" ) ;
142+
143+ const testTitle = this . currentTest . fullTitle ( ) ;
144+
145+ const result = await fetch ( "https://eu.i.posthog.com/capture/" , {
146+ method : "POST" ,
147+ headers : {
148+ "Content-Type" : "application/json" ,
149+ } ,
150+ body : JSON . stringify ( {
151+ api_key : PERF_LOGGING_API_KEY ,
152+ event : "performance_test_result" ,
153+ distinct_id : `mockttp-${ process . env . ImageOS || 'local' } ` ,
154+ properties : {
155+ $process_person_profile : false ,
156+ sha : process . env . GITHUB_SHA || 'local' ,
157+ test_name : "Mockttp - " + testTitle ,
158+ throughput : testResults . throughput ,
159+ mean_latency : testResults . latency . mean ,
160+ p99_latency : testResults . latency . p99 ,
161+ errors : testResults . errors ,
162+
163+ } ,
164+ } ) ,
165+ } ) ;
166+
167+ expect ( result . ok ) . to . equal ( true , `Failed to log performance results: ${ await result . text ( ) } ` ) ;
168+ }
169+ } ) ;
0 commit comments