php - 使用 php 脚本的 Redis 延迟测量

标签 php redis latency predis

请原谅我问这个基本问题。

我已经在本地 ubuntu 机器上安装了 redis 和 predis(客户端库)。现在,我正在我的 php 脚本中执行某些 SET/GET 操作。我只想知道如何“准确计算”redis 服务器完成“SET”或“GET”操作所花费的时间。 redis-cli --latency 给你一个平均值,但实际上并没有列出任何操作所花费的个人时间。

我的目标是执行大约 100 条这样的指令,并找出执行每条指令所花费的平均时间。

非常欢迎任何建议。

这是我的 php 客户端脚本:

<html>
<body>
<?php
require __DIR__.'/predis/src/Autoloader.php';
Predis\Autoloader::register();
// since we connect to default setting localhost
// and 6379 port there is no need for extra
// configuration. If not then you can specify the
// scheme, host and port to connect as an array
// to the constructor.
try {
$redis = new Predis\Client();
//$redis = new PredisClient();
/*
$redis = new PredisClient(array(
"scheme" => "tcp",
"host" => "127.0.0.1",
"port" => 6379));
*/

echo "<br />Successfully connected to Redis<br />";
//$redis->set("hello_world", "Hi from php!");
//$value = $redis->get("hello_world");

$sum = 0;
$time_start = microtime(true);
echo "<br />Start Time: $time_start";
$c = $redis->hset("taxi_car1", "brand", "Toyota");
$time_stop = microtime(true);

echo "<br /> Time taken for this instruction:".($time_stop-$time_start)*1000;
$sum += ($time_stop-$time_start)*1000;

$time_start = microtime(true);
$redis->hset("taxi_car1", "model", "Corolla");
$time_stop = microtime(true);
echo "<br /> Time taken for this instruction:".($time_stop-$time_start)*1000;
$sum += ($time_stop-$time_start)*1000;

echo "<br />Stop Time: $time_stop<br />";
echo "Average Time Taken: ".($sum/2);

//var_dump($value);
//echo "<br />$value<br />"; 
echo ($redis->exists("Santa Claus")) ? "true" : "false";
}
catch (Exception $e) {
echo "Couldn't connected to Redis";
}
?>

我知道这不是正确的方法。如果有人可以建议一种计算此平均延迟的正确方法,那就太好了。我最终计划将其扩展到分布式系统。

提前致谢

更新:

我尝试启用慢速日志以查看特定命令的执行时间。但是,当命令集稍大时(例如 100),则计算平均值。手动执行时间,成为一项繁琐的工作。我期待通过 php 脚本来完成。

最佳答案

您可以使用 INFO commandstats .如果要重置统计信息,请运行 CONFIG RESETSTAT .

关于php - 使用 php 脚本的 Redis 延迟测量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25400729/

相关文章:

php - 如何在uuid codeigniter中检索最后插入的id

php - 当包含在 ajax 调用中时,我的 href 链接不起作用

单库Redis SLAVEOF

caching - GPU 的延迟是多少?

delphi - 减少音频文件播放前的延迟

javascript - 将 Angular 表单提交到 php 文件

php - 如何重新生成 Laravel session ID

redis - 如何检查 Redis 中两个列表是否相等

caching - Redis 与硬件缓存

latency - 您如何测量低延迟环境中的延迟?