Skip to content

Commit 1394bb3

Browse files
authored
Merge pull request #1061 from grafana/document/async-await-redis
Update redis module's documentation to use `async/await` syntax
2 parents bfb31ac + 8180976 commit 1394bb3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+342
-383
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,5 @@ which operate in a synchronous manner,
2727
the Redis `Client` operates in an asynchronous manner.
2828
In practice, this means that using the Redis `Client`'s methods won't block test execution,
2929
and that the test will continue to run even if the Redis `Client` isn't ready to respond to the request.
30-
31-
This new execution model does introduce a potential caveat: to depend on operations against Redis, all following operations should be made in the context of a [promise chain](https://javascript.info/promise-chaining).
32-
As the [Redis Client example](/javascript-api/k6-experimental/redis/client#example) demonstrates, whenever there is a dependency on a Redis operation result or return value, the operation should be wrapped in a promise itself.
33-
This way, a user can perform asynchronous interactions with Redis in a seemingly synchronous manner.
30+
The `async` and `await` keywords enable asynchronous, promise-based behavior to be written in a cleaner style, avoiding the need to explicitly configure promise chains
31+
(for details, refer to the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)).

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

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -53,51 +53,44 @@ 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+
const value = await redisClient.get(key);
66+
if (value !== '11') {
67+
throw new Error('foo should have been incremented to 11');
68+
}
69+
70+
await redisClient.del(key);
71+
if (await redisClient.exists(key) !== 0) {
72+
throw new Error('foo should have been deleted');
73+
}
7474
}
7575

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

80-
export function measureUsingRedisData() {
80+
export async function measureUsingRedisData() {
8181
// Pick a random crocodile id from the dedicated redis set,
8282
// 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));
83+
const randomID = await redisClient.srandmember('crocodile_ids');
84+
const url = `https://test-api.k6.io/public/crocodiles/${randomID}`;
85+
const res = await http.asyncRequest("GET", url);
86+
87+
check(res, {'status is 200': (r) => r.status === 200});
88+
89+
await redisClient.hincrby('k6_crocodile_fetched', url, 1);
9790
}
9891

99-
export function teardown() {
100-
redisClient.del('crocodile_ids');
92+
export async function teardown() {
93+
await redisClient.del('crocodile_ids');
10194
}
10295

10396
export function handleSummary(data) {

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,18 @@ const redisClient = new redis.Client({
3535
password: redis_password,
3636
});
3737

38-
export default function () {
39-
redisClient
40-
.set('mykey', 10, 0)
41-
.then((_) => redisClient.incr('mykey'))
42-
.then((value) => redisClient.incrBy('mykey', value))
43-
.then((value) => redisClient.decrBy('mykey', value))
44-
.then((_) => redisClient.decr('mykey'));
38+
export default async function () {
39+
await redisClient.set('mykey', 10, 0);
40+
41+
let value;
42+
value = await redisClient.incr('mykey');
43+
value = await redisClient.incrBy('mykey', value);
44+
value = await redisClient.decrBy('mykey', value);
45+
value = await redisClient.decr('mykey');
46+
47+
if (value !== -1) {
48+
throw new Error('mykey should have been -1');
49+
}
4550
}
4651
```
4752

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ const redisClient = new redis.Client({
3636
password: redis_password,
3737
});
3838

39-
export default function () {
40-
redisClient
41-
.set('mykey', 10, 0)
42-
.then((_) => redisClient.incr('mykey'))
43-
.then((value) => redisClient.incrBy('mykey', value))
44-
.then((value) => redisClient.decrBy('mykey', value))
45-
.then((_) => redisClient.decr('mykey'));
39+
export default async function () {
40+
await redisClient.set('mykey', 10, 0);
41+
42+
const value = await redisClient.decrBy('mykey', 2);
43+
if (value !== 8) {
44+
throw new Error('mykey should have been 8');
45+
}
4646
}
4747
```
4848

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,18 @@ const redisClient = new redis.Client({
3535
password: redis_password,
3636
});
3737

38-
export default function () {
39-
redisClient
40-
.set('mykey', 'myvalue', 0)
41-
.then((_) => redisClient.exists('mykey'))
42-
.then((exists) => {
43-
if (exists === false) {
38+
export default async function () {
39+
await redisClient.set('mykey', 'myvalue', 0);
40+
41+
const exists = await redisClient.exists('mykey');
42+
if (exists === false) {
4443
throw new Error('mykey should exist');
45-
}
44+
}
4645

47-
return redisClient.get('mykey');
48-
})
49-
.then((value) => console.log(`set key 'mykey' to value: ${value}`))
50-
.then(() => redisClient.del('mykey'));
46+
const value = await redisClient.get('mykey');
47+
console.log(`set key 'mykey' to value: ${value}`);
48+
49+
await redisClient.del('mykey');
5150
}
5251
```
5352

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,20 @@ const redisClient = new redis.Client({
3535
password: redis_password,
3636
});
3737

38-
export default function () {
39-
redisClient
40-
.set('mykey', 'myvalue', 0)
41-
.then((_) => redisClient.exists('mykey'))
42-
.then((exists) => {
43-
if (exists === false) {
38+
export default async function () {
39+
let exists = await redisClient.exists('mykey');
40+
if (exists === true) {
41+
throw new Error('mykey should not exist');
42+
}
43+
44+
await redisClient.set('mykey', 'myvalue', 0);
45+
46+
exists = await redisClient.exists('mykey');
47+
if (exists === false) {
4448
throw new Error('mykey should exist');
45-
}
46-
47-
return redisClient.get('mykey');
48-
})
49-
.then((value) => console.log(`set key 'mykey' to value: ${value}`))
50-
.then(() => redisClient.del('mykey'));
49+
}
50+
51+
await redisClient.del('mykey');
5152
}
5253
```
5354

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,15 @@ const redisClient = new redis.Client({
3636
password: redis_password,
3737
});
3838

39-
export default function () {
40-
redisClient
41-
.set('mykey', 'myvalue', 10)
42-
.then((_) => redisClient.expire('mykey', 100))
43-
.then((_) => redisClient.ttl('mykey'))
44-
.then((ttl) => {
45-
if (ttl <= 10) {
46-
throw new Error('mykey should have a ttl of 10 <= x < 100');
47-
}
48-
49-
return redisClient.persist('mykey', 100);
50-
});
39+
export default async function () {
40+
await redisClient.set('mykey', 'myvalue', 10);
41+
await redisClient.expire('mykey', 100);
42+
43+
const ttl = await redisClient.ttl('mykey');
44+
if (ttl <= 10 || ttl >= 100) {
45+
throw new Error('mykey should have a ttl of 10 <= x < 100');
46+
}
5147
}
5248
```
5349

54-
</CodeGroup>
50+
</CodeGroup>

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,18 @@ const redisClient = new redis.Client({
3535
password: redis_password,
3636
});
3737

38-
export default function () {
39-
redisClient
40-
.set('mykey', 'myvalue', 0)
41-
.then((_) => redisClient.exists('mykey'))
42-
.then((exists) => {
43-
if (exists === false) {
44-
throw new Error('mykey should exist');
45-
}
46-
47-
return redisClient.get('mykey');
48-
})
49-
.then((value) => console.log(`set key 'mykey' to value: ${value}`))
50-
.then(() => redisClient.del('mykey'));
38+
export default async function () {
39+
await redisClient.set('mykey', 'myvalue', 0)
40+
41+
const exists = await redisClient.exists('mykey');
42+
if (exists === false) {
43+
throw new Error('mykey should exist');
44+
}
45+
46+
const value = await redisClient.get('mykey');
47+
console.log(`set key 'mykey' to value: ${value}`);
48+
49+
await redisClient.del('mykey');
5150
}
5251
```
5352

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ const redisClient = new redis.Client({
3535
password: redis_password,
3636
});
3737

38-
export default function () {
39-
redisClient
40-
.set('mykey', 'oldvalue', 0)
41-
.then((_) => redisClient.getSet('mykey', 'newvalue'))
42-
.then((_) => redisClient.getDel('mykey'));
38+
export default async function () {
39+
await redisClient.set('mykey', 'oldvalue', 0);
40+
let value = await redisClient.getSet('mykey', 'newvalue');
41+
42+
value = await redisClient.getDel('mykey');
43+
if (value !== 'newvalue') {
44+
throw new Error('mykey should have been newvalue');
45+
}
4346
}
4447
```
4548

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@ const redisClient = new redis.Client({
3636
password: redis_password,
3737
});
3838

39-
export default function () {
40-
redisClient
41-
.set('mykey', 'oldvalue', 0)
42-
.then((_) => redisClient.getSet('mykey', 'newvalue'))
43-
.then((_) => redisClient.getDel('mykey'));
39+
export default async function () {
40+
await redisClient.set('mykey', 'oldvalue', 0);
41+
await redisClient.getSet('mykey', 'newvalue');
42+
await redisClient.getDel('mykey');
4443
}
4544
```
4645

0 commit comments

Comments
 (0)