1- import { createExampleSetup , createTenv } from '@internal/e2e' ;
1+ import { setTimeout } from 'node:timers/promises' ;
2+ import { createExampleSetup , createTenv , GatewayOptions } from '@internal/e2e' ;
23import { benchConfig } from '@internal/testing' ;
34import { bench , describe , expect } from 'vitest' ;
45
56describe ( 'Response Cache' , async ( ) => {
6- const { gateway, container } = createTenv ( __dirname ) ;
7- const exampleSetup = createExampleSetup ( __dirname , 1000 ) ;
8-
7+ const { runBench, container } = await makeRunner ( ) ;
98 const redis = await container ( {
109 name : 'redis' ,
1110 healthcheck : [ 'CMD' , 'redis-cli' , 'ping' ] ,
@@ -17,58 +16,67 @@ describe('Response Cache', async () => {
1716 containerPort : 6379 ,
1817 } ) ;
1918
20- const supergraph = await exampleSetup . supergraph ( ) ;
21-
22- const { query, operationName, result } = exampleSetup ;
23-
24- const gatewayWithoutCache = await gateway ( {
25- supergraph,
26- args : [ '-c' , 'gateway-without-cache.config.ts' ] ,
27- } ) ;
28- bench (
29- 'Without response cache' ,
30- async ( ) => {
31- const response = await gatewayWithoutCache . execute ( {
32- query,
33- operationName,
34- } ) ;
35- expect ( response ) . toEqual ( result ) ;
36- } ,
37- benchConfig ,
19+ await runBench (
20+ 'With in memory response cache' ,
21+ 'gateway-with-cache.config.ts' ,
3822 ) ;
23+ await runBench ( 'Without response cache' , 'gateway-without-cache.config.ts' ) ;
3924
40- const gatewayWithCache = await gateway ( {
41- supergraph,
42- args : [ '-c' , 'gateway-with-cache.config.ts' ] ,
43- } ) ;
44- bench (
45- 'With in memory response cache' ,
46- async ( ) => {
47- const response = await gatewayWithCache . execute ( {
48- query,
49- operationName,
50- } ) ;
51- expect ( response ) . toEqual ( result ) ;
52- } ,
53- benchConfig ,
25+ await runBench . skip (
26+ 'Without invalidation cache' ,
27+ 'gateway-without-auto-invalidation.config.ts' ,
5428 ) ;
5529
56- const gatewayWithRedisCache = await gateway ( {
57- supergraph,
58- args : [ '-c' , 'gateway-with-redis.config.ts' ] ,
59- env : {
60- REDIS_URL : `redis://localhost:${ redis . port } ` ,
61- } ,
62- } ) ;
63- bench (
30+ await runBench . skip (
6431 'With redis response cache' ,
65- async ( ) => {
66- const response = await gatewayWithRedisCache . execute ( {
67- query,
68- operationName,
69- } ) ;
70- expect ( response ) . toEqual ( result ) ;
32+ 'gateway-with-redis.config.ts' ,
33+ {
34+ env : {
35+ REDIS_URL : `redis://localhost:${ redis . port } ` ,
36+ } ,
7137 } ,
72- benchConfig ,
7338 ) ;
7439} ) ;
40+
41+ const makeRunner = async ( ) => {
42+ const { gateway, container } = createTenv ( __dirname ) ;
43+ const exampleSetup = createExampleSetup ( __dirname , 1000 ) ;
44+
45+ const supergraph = await exampleSetup . supergraph ( ) ;
46+
47+ const { query, operationName, result } = exampleSetup ;
48+
49+ const runBench = async (
50+ name : string ,
51+ configFile : string ,
52+ options ?: Partial < GatewayOptions > ,
53+ ) => {
54+ const { execute } = await gateway ( {
55+ supergraph,
56+ ...options ,
57+ args : [ '-c' , configFile , ...( options ?. args ?? [ ] ) ] ,
58+ } ) ;
59+ return bench (
60+ name ,
61+ async ( ) => {
62+ const response = await execute ( {
63+ query,
64+ operationName,
65+ } ) ;
66+ expect ( response ) . toEqual ( result ) ;
67+ } ,
68+ benchConfig ,
69+ ) ;
70+ } ;
71+
72+ runBench . skip = async (
73+ name : string ,
74+ _configFile : string ,
75+ _options ?: GatewayOptions ,
76+ ) => bench . skip ( name ) ;
77+
78+ return {
79+ runBench,
80+ container,
81+ } ;
82+ } ;
0 commit comments