No results for

Powered byAlgolia
⚠️ This is archived documentation for v0.40. Go to the latest version

Client is a Redis client to interact with a Redis server or cluster. It exposes a promise-based API, which users can interact with in an asynchronous manner.

Though the API intends to be thorough and extensive, it does not expose the whole Redis API. Instead, the intent is to expose Redis for use cases most appropriate to k6.

Note that the Client is configured through the Options object.

Example

import { check } from 'k6';
import http from 'k6/http';
import redis from 'k6/experimental/redis';
import exec from 'k6/execution';
import { textSummary } from 'https://jslib.k6.io/k6-summary/0.0.2/index.js';
export const options = {
scenarios: {
redisPerformance: {
executor: 'shared-iterations',
vus: 10,
iterations: 200,
exec: 'measureRedisPerformance',
},
usingRedisData: {
executor: 'shared-iterations',
vus: 10,
iterations: 200,
exec: 'measureUsingRedisData',
},
},
};
// Get the redis instance(s) address and password from the environment
const redis_addrs = __ENV.REDIS_ADDRS || '';
const redis_password = __ENV.REDIS_PASSWORD || '';
// Instantiate a new redis client
const redisClient = new redis.Client({
addrs: redis_addrs.split(',') || new Array('localhost:6379'), // in the form of 'host:port', separated by commas
password: redis_password,
});
// Prepare an array of crocodile ids for later use
// in the context of the measureUsingRedisData function.
const crocodileIDs = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
export function measureRedisPerformance() {
// VUs are executed in a parallel fashion,
// thus, to ensure that parallel VUs are not
// modifying the same key at the same time,
// we use keys indexed by the VU id.
const key = `foo-${exec.vu.idInTest}`;
redisClient
.set(`foo-${exec.vu.idInTest}`, 1)
.then(() => redisClient.get(`foo-${exec.vu.idInTest}`))
.then((value) => redisClient.incrBy(`foo-${exec.vu.idInTest}`, value))
.then((_) => redisClient.del(`foo-${exec.vu.idInTest}`))
.then((_) => redisClient.exists(`foo-${exec.vu.idInTest}`))
.then((exists) => {
if (exists !== 0) {
throw new Error('foo should have been deleted');
}
});
}
export function setup() {
redisClient.sadd('crocodile_ids', ...crocodileIDs);
}
export function measureUsingRedisData() {
// Pick a random crocodile id from the dedicated redis set,
// we have filled in setup().
redisClient
.srandmember('crocodile_ids')
.then((randomID) => {
const url = `https://test-api.k6.io/public/crocodiles/${randomID}`;
const res = http.get(url);
check(res, {
'status is 200': (r) => r.status === 200,
'content-type is application/json': (r) => r.headers['content-type'] === 'application/json',
});
return url;
})
.then((url) => redisClient.hincrby('k6_crocodile_fetched', url, 1));
}
export function teardown() {
redisClient.del('crocodile_ids');
}
export function handleSummary(data) {
redisClient
.hgetall('k6_crocodile_fetched')
.then((fetched) => Object.assign(data, { k6_crocodile_fetched: fetched }))
.then((data) => redisClient.set(`k6_report_${Date.now()}`, JSON.stringify(data)))
.then(() => redisClient.del('k6_crocodile_fetched'));
return {
stdout: textSummary(data, { indent: ' ', enableColors: true }),
};
}

key/value methods

MethodRedis commandDescription
Client.set(key, value, expiration)SETSet key to hold value, with a time to live equal to expiration.
Client.get(key)GETGet the value of key.
Client.getSet(key, value)GETSETAtomically sets key to value and returns the old value stored at key.
Client.del(keys)DELRemoves the specified keys.
Client.getDel(key)GETDELGet the value of key and delete the key.
Client.exists(keys)EXISTSReturns the number of key arguments that exist.
Client.incr(key)INCRIncrements the number stored at key by one.
Client.incrBy(key, increment)INCRBYIncrements the number stored at key by increment.
Client.decr(key)DECRDecrements the number stored at key by one.
Client.decrBy(key, decrement)DECRBYDecrements the number stored at key by decrement.
Client.randomKey()RANDOMKEYReturns a random key's value.
Client.mget(keys)MGETReturns the values of all specified keys.
Client.expire(key, seconds)EXPIRESets a timeout on key, after which the key will automatically be deleted.
Client.ttl(key)TTLReturns the remaining time to live of a key that has a timeout.
Client.persist(key)PERSISTRemoves the existing timeout on key.

List methods

MethodRedis commandDescription
Client.lpush(key, values)LPSUHInserts all the specified values at the head of the list stored at key.
Client.rpush(key, values)RPUSHInserts all the specified values at the tail of the list stored at key.
Client.lpop(key)LPOPRemoves and returns the first element of the list stored at key.
Client.rpop(key)RPOPRemoves and returns the last element of the list stored at key.
Client.lrange(key, start, stop)LRANGEReturns the specified elements of the list stored at key.
Client.lindex(key, start, stop)LINDEXReturns the specified element of the list stored at key.
Client.lset(key, index, element)LSETSets the list element at index to element.
Client.lrem(key, count, value)LREMRemoves the first count occurrences of value from the list stored at key.
Client.llen(key)LLENReturns the length of the list stored at key.

Hash methods

MethodRedis commandDescription
Client.hset(key, field, value)HSETSets the specified field in the hash stored at key to value.
Client.hsetnx(key, field, value)HSETNXSets the specified field in the hash stored at key to value, only if field does not yet exist.
Client.hget(key, field)HGETReturns the value associated with field in the hash stored at key.
Client.hdel(key, fields)HDELDeletes the specified fields from the hash stored at key.
Client.hgetall(key)HGETALLReturns all fields and values of the hash stored at key.
Client.hkeys(key)HKEYSReturns all fields of the hash stored at key.
Client.hvals(key)HVALSReturns all values of the hash stored at key.
Client.hlen(key)HLENReturns the number of fields in the hash stored at key.
Client.hincrby(key, field, increment)HINCRBYIncrements the integer value of field in the hash stored at key by increment.

Set methods

MethodRedis commandDescription
Client.sadd(key, members)SADDAdds the specified members to the set stored at key.
Client.srem(key, members)SREMRemoves the specified members from the set stored at key.
Client.sismember(key, member)SISMEMBERReturns if member is a member of the set stored at key.
Client.smembers(key)SMEMBERSReturns all the members of the set values stored at keys.
Client.srandmember(key)SRANDMEMBERReturns a random element from the set value stored at key.
Client.spop(key)SPOPRemoves and returns a random element from the set value stored at key.

miscellaneous

MethodDescription
Client.sendCommand(command, args)Send a command to the Redis server.