Skip to content

Commit 13f992c

Browse files
committed
Update redis client example with async/await syntax
1 parent bfb31ac commit 13f992c

File tree

1 file changed

+23
-31
lines changed
  • src/data/markdown/docs/02 javascript api/07 k6-experimental/02 redis

1 file changed

+23
-31
lines changed

src/data/markdown/docs/02 javascript api/07 k6-experimental/02 redis/10 Client.md

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -53,51 +53,43 @@ const redisClient = new redis.Client({
5353
// in the context of the measureUsingRedisData function.
5454
const crocodileIDs = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
5555

56-
export function measureRedisPerformance() {
56+
export async function measureRedisPerformance() {
5757
// VUs are executed in a parallel fashion,
5858
// thus, to ensure that parallel VUs are not
5959
// modifying the same key at the same time,
6060
// we use keys indexed by the VU id.
6161
const key = `foo-${exec.vu.idInTest}`;
6262

63-
redisClient
64-
.set(`foo-${exec.vu.idInTest}`, 1)
65-
.then(() => redisClient.get(`foo-${exec.vu.idInTest}`))
66-
.then((value) => redisClient.incrBy(`foo-${exec.vu.idInTest}`, value))
67-
.then((_) => redisClient.del(`foo-${exec.vu.idInTest}`))
68-
.then((_) => redisClient.exists(`foo-${exec.vu.idInTest}`))
69-
.then((exists) => {
70-
if (exists !== 0) {
71-
throw new Error('foo should have been deleted');
72-
}
73-
});
63+
await redisClient.set(key, 1);
64+
await redisClient.incrBy(key, 10);
65+
if (await redisClient.get(key) !== '11') {
66+
throw new Error('foo should have been incremented to 11');
67+
}
68+
69+
await redisClient.del(key);
70+
if (await redisClient.exists(key) !== 0) {
71+
throw new Error('foo should have been deleted');
72+
}
7473
}
7574

76-
export function setup() {
77-
redisClient.sadd('crocodile_ids', ...crocodileIDs);
75+
export async function setup() {
76+
await redisClient.sadd('crocodile_ids', ...crocodileIDs);
7877
}
7978

80-
export function measureUsingRedisData() {
79+
export async function measureUsingRedisData() {
8180
// Pick a random crocodile id from the dedicated redis set,
8281
// we have filled in setup().
83-
redisClient
84-
.srandmember('crocodile_ids')
85-
.then((randomID) => {
86-
const url = `https://test-api.k6.io/public/crocodiles/${randomID}`;
87-
const res = http.get(url);
88-
89-
check(res, {
90-
'status is 200': (r) => r.status === 200,
91-
'content-type is application/json': (r) => r.headers['content-type'] === 'application/json',
92-
});
93-
94-
return url;
95-
})
96-
.then((url) => redisClient.hincrby('k6_crocodile_fetched', url, 1));
82+
const randomID = await redisClient.srandmember('crocodile_ids');
83+
const url = `https://test-api.k6.io/public/crocodiles/${randomID}`;
84+
const res = http.get(url);
85+
86+
check(res, {'status is 200': (r) => r.status === 200});
87+
88+
await redisClient.hincrby('k6_crocodile_fetched', url, 1);
9789
}
9890

99-
export function teardown() {
100-
redisClient.del('crocodile_ids');
91+
export async function teardown() {
92+
await redisClient.del('crocodile_ids');
10193
}
10294

10395
export function handleSummary(data) {

0 commit comments

Comments
 (0)