Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timing Queries in Redis-Cli?

Tags:

redis

This seems like it ought to be trivial, but I want to run a query through redis-cli, and then just get back how long it took on the server, along with the results. This is just for debugging purposes, to factor out problems with my client library or latency. Is there a way to do this?

like image 355
Eli Avatar asked Aug 08 '13 19:08

Eli


People also ask

Does Redis have timestamp?

The TIME command returns the current server time as a two items lists: a Unix timestamp and the amount of microseconds already elapsed in the current second.

What is Redis connection timeout?

The maximum length of time to wait while establishing a connection to a Redis server.

What does Redis-CLI command do?

The Redis command line interface ( redis-cli ) is a terminal program used to send commands to and read replies from the Redis server.

How do I run a Redis query?

To start Redis client, open the terminal and type the command redis-cli. This will connect to your local server and now you can run any command. In the above example, we connect to Redis server running on the local machine and execute a command PING, that checks whether the server is running or not.


1 Answers

You can do this by wrapping the redis command you're investigating in a MULTI/EXEC block, where the TIME command is used right before and right after your command. For example:

Using the node library:

var multi = redis.multi();
multi.time(); // start time
multi.sunionstore(['fast_food_joints', 'pizza_hut', 'taco_bell']); // this is the command I'm investigating
multi.time(); // end time
multi.exec(function(err, responses){
    var response = responses[1]; // the response i care about
    var start = (parseInt(responses[0][0]) * 1000) + (parseInt(responses[0][1]) / 1000);
    var end   = (parseInt(responses[2][0]) * 1000) + (parseInt(responses[2][1]) / 1000);            
    var execution_time = end - start; // in milliseconds
});

Or... using the command line (which is what you asked for in your question):

192.168.1.1:6379> MULTI
OK
192.168.1.1:6379> TIME
QUEUED
192.168.1.1:6379> SUNIONSTORE fast_food_joints pizza_hut taco_bell
QUEUED
192.168.1.1:6379> TIME
QUEUED
192.168.1.1:6379> EXEC
1) 1) "1450818240"
   2) "666636"
2) (integer) 48886
3) 1) "1450818240"
   2) "666639"

And then do the math yourself. The above example took 3 microseconds.

like image 178
Adam V. Avatar answered Oct 22 '22 17:10

Adam V.