2024-08-23 00:35:34 -07:00
import { $ , which } from 'zx' ;
2024-08-22 01:33:11 -07:00
import { Scenario } from '@/types/scenario' ;
/ * *
* Executes test scenarios using k6
* /
export class K6Executor {
constructor (
private readonly k6ExecutablePath : string ,
private readonly n8nApiBaseUrl : string ,
) { }
async executeTestScenario ( scenario : Scenario ) {
// For 1 min with 5 virtual users
const stage = '1m:5' ;
2024-08-23 00:35:34 -07:00
const k6ExecutablePath = await this . resolveK6ExecutablePath ( ) ;
2024-08-22 01:33:11 -07:00
const processPromise = $ ( {
cwd : scenario.scenarioDirPath ,
env : {
API_BASE_URL : this.n8nApiBaseUrl ,
} ,
2024-08-23 00:35:34 -07:00
} ) ` ${ k6ExecutablePath } run --quiet --stage ${ stage } ${ scenario . scriptPath } ` ;
2024-08-22 01:33:11 -07:00
for await ( const chunk of processPromise . stdout ) {
console . log ( chunk . toString ( ) ) ;
}
}
2024-08-23 00:35:34 -07:00
/ * *
* @returns Resolved path to the k6 executable
* /
private async resolveK6ExecutablePath ( ) : Promise < string > {
const k6ExecutablePath = await which ( this . k6ExecutablePath , { nothrow : true } ) ;
if ( ! k6ExecutablePath ) {
throw new Error (
'Could not find k6 executable based on your `PATH`. Please ensure k6 is available in your system and add it to your `PATH` or specify the path to the k6 executable using the `K6_PATH` environment variable.' ,
) ;
}
return k6ExecutablePath ;
}
2024-08-22 01:33:11 -07:00
}