1+ const { exec, execSync } = require ( 'child_process' ) ;
2+ const fs = require ( 'fs' ) ;
3+ const path = require ( 'path' ) ;
4+ const util = require ( 'util' ) ;
5+
6+ const execAsync = util . promisify ( exec ) ;
7+
8+ // Start services and run benchmarks
9+ function killServerOnPort ( port ) {
10+ try {
11+ const pid = execSync ( `lsof -t -i:${ port } ` ) . toString ( ) . trim ( ) ;
12+ if ( pid ) {
13+ execSync ( `kill ${ pid } ` ) ;
14+ console . log ( `Killed process running on port ${ port } ` ) ;
15+ } else {
16+ console . log ( `No process found running on port ${ port } ` ) ;
17+ }
18+ } catch ( error ) {
19+ console . error ( `Error killing server on port ${ port } :` , error . message ) ;
20+ }
21+ }
22+
23+ const bench1Results = [ ] ;
24+ const bench2Results = [ ] ;
25+ const bench3Results = [ ] ;
26+
27+ killServerOnPort ( 3000 ) ;
28+ execSync ( 'sh nginx/run.sh' ) ;
29+
30+ async function runBenchmarkAsync ( serviceScript , bench ) {
31+ let graphqlEndpoint = 'http://localhost:8000/graphql' ;
32+ if ( serviceScript . includes ( 'hasura' ) ) {
33+ graphqlEndpoint = 'http://127.0.0.1:8080/v1/graphql' ;
34+ }
35+
36+ const benchmarkScript = 'wrk/bench.sh' ;
37+ const sanitizedServiceScriptName = serviceScript . replace ( / \/ / g, '_' ) ;
38+ const resultFiles = [
39+ `result1_${ sanitizedServiceScriptName } .txt` ,
40+ `result2_${ sanitizedServiceScriptName } .txt` ,
41+ `result3_${ sanitizedServiceScriptName } .txt`
42+ ] ;
43+
44+ await execAsync ( `bash test_query${ bench } .sh ${ graphqlEndpoint } ` ) ;
45+
46+ // Warmup run
47+ for ( let i = 0 ; i < 3 ; i ++ ) {
48+ await execAsync ( `bash ${ benchmarkScript } ${ graphqlEndpoint } ${ bench } > /dev/null` ) ;
49+ await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ;
50+ }
51+
52+ // 3 benchmark runs
53+ for ( const resultFile of resultFiles ) {
54+ console . log ( `Running benchmark ${ bench } for ${ serviceScript } ` ) ;
55+ const outputFile = `bench${ bench } _${ resultFile } ` ;
56+ await execAsync ( `bash ${ benchmarkScript } ${ graphqlEndpoint } ${ bench } > ${ outputFile } ` ) ;
57+
58+ if ( bench === 1 ) {
59+ bench1Results . push ( outputFile ) ;
60+ } else if ( bench === 2 ) {
61+ bench2Results . push ( outputFile ) ;
62+ } else if ( bench === 3 ) {
63+ bench3Results . push ( outputFile ) ;
64+ }
65+ }
66+ }
67+
68+ async function runBenchmark ( serviceScript ) {
69+ killServerOnPort ( 8000 ) ;
70+ execSync ( 'sleep 5' ) ;
71+
72+ if ( serviceScript . includes ( 'hasura' ) ) {
73+ execSync ( `bash ${ serviceScript } ` , { stdio : 'inherit' } ) ;
74+ } else {
75+ execSync ( `bash ${ serviceScript } &` , { stdio : 'inherit' } ) ;
76+ }
77+
78+ execSync ( 'sleep 15' ) ;
79+
80+ const benchmarks = [ 1 , 2 , 3 ] ;
81+ const benchmarkPromises = benchmarks . map ( bench => runBenchmarkAsync ( serviceScript , bench ) ) ;
82+
83+ await Promise . all ( benchmarkPromises ) ;
84+ }
85+
86+ // Main script
87+ if ( process . argv . length < 3 ) {
88+ console . log ( 'Usage: node script.js <service_name>' ) ;
89+ console . log ( 'Available services: apollo_server, caliban, netflix_dgs, gqlgen, tailcall, async_graphql, hasura, graphql_jit' ) ;
90+ process . exit ( 1 ) ;
91+ }
92+
93+ const service = process . argv [ 2 ] ;
94+ const validServices = [ 'apollo_server' , 'caliban' , 'netflix_dgs' , 'gqlgen' , 'tailcall' , 'async_graphql' , 'hasura' , 'graphql_jit' ] ;
95+
96+ if ( ! validServices . includes ( service ) ) {
97+ console . log ( `Invalid service name. Available services: ${ validServices . join ( ', ' ) } ` ) ;
98+ process . exit ( 1 ) ;
99+ }
100+
101+ if ( fs . existsSync ( 'results.md' ) ) {
102+ fs . unlinkSync ( 'results.md' ) ;
103+ }
104+
105+ async function main ( ) {
106+ await runBenchmark ( `graphql/${ service } /run.sh` ) ;
107+
108+ if ( service === 'apollo_server' ) {
109+ process . chdir ( 'graphql/apollo_server' ) ;
110+ execSync ( 'npm stop' ) ;
111+ process . chdir ( '../../' ) ;
112+ } else if ( service === 'hasura' ) {
113+ execSync ( 'bash graphql/hasura/kill.sh' ) ;
114+ }
115+ }
116+
117+ main ( ) . catch ( error => {
118+ console . error ( "An error occurred:" , error ) ;
119+ process . exit ( 1 ) ;
120+ } ) ;
0 commit comments