如何在 Ubuntu 18.04 上对 Redis 服务器的性能进行基准测试

介绍

Benchmarking是分析数据库服务器的整体性能时的一个重要做法,有助于识别瓶颈以及这些系统内改进的机会。

Redis是一个内存数据存储器,可以用作数据库、缓存和消息中介,它支持从简单到复杂的数据结构,包括哈希、串、分类集、比特图、地理空间数据等类型。

前提条件

要遵循这个指南,你需要:

<$>[注] **注:**本教程中展示的命令是在4GB DigitalOcean Droplet上运行的专用Redis服务器上执行的。

使用包含的redis-benchmark工具

Redis 配备了一种名为redis-benchmark的基准工具,该程序可以用来模拟随意数量的客户端同时连接并在服务器上执行操作,测量请求完成所需的时间。

下列列表详细介绍了redis-benchmark中使用的一些常见命令选项:

  • h: Redis 主机. 默认值为 127.0.0.1.
  • -p: Redis 端口. 默认值为 6379.
  • -a: 如果您的服务器需要身份验证,您可以使用此选项来提供密码。
  • -c: 模拟的客户端数(平行连接)。 默认值为
  • n: 要发送多少请求。 默认值为 100000。
  • -d: 数据大小为 SETGET值,以字节计量。 默认值为 3.
  • -t: 只运行一组测试

例如,如果您想检查本地 Redis 服务器每秒可处理的平均请求数,则可以使用:

1redis-benchmark -q

你会得到类似的输出,但有不同的数字:

 1[secondary_label Output]
 2PING_INLINE: 85178.88 requests per second
 3PING_BULK: 83056.48 requests per second
 4SET: 72202.16 requests per second
 5GET: 94607.38 requests per second
 6INCR: 84961.77 requests per second
 7LPUSH: 78988.94 requests per second
 8RPUSH: 88652.48 requests per second
 9LPOP: 87950.75 requests per second
10RPOP: 80971.66 requests per second
11SADD: 80192.46 requests per second
12HSET: 84317.03 requests per second
13SPOP: 78125.00 requests per second
14LPUSH (needed to benchmark LRANGE): 84175.09 requests per second
15LRANGE_100 (first 100 elements): 52383.45 requests per second
16LRANGE_300 (first 300 elements): 21547.08 requests per second
17LRANGE_500 (first 450 elements): 14471.78 requests per second
18LRANGE_600 (first 600 elements): 9383.50 requests per second
19MSET (10 keys): 71225.07 requests per second

您还可以将测试限制在您选择的命令子集中,使用t参数. 以下命令仅显示GETSET命令的平均值:

1redis-benchmark -t set,get -q
1[secondary_label Output]
2SET: 76687.12 requests per second
3GET: 82576.38 requests per second

默认选项将使用 50 个并行连接创建 100,000 个请求到 Redis 服务器. 如果您想要增加并行连接的数量以模拟使用峰值,您可以使用 -c 选项:

1redis-benchmark -t set,get -q -c 1000

因为这将使用1000个同时连接,而不是默认的50个连接,您应该预计性能会下降:

1[secondary_label Output]
2SET: 69444.45 requests per second
3GET: 70821.53 requests per second

如果您想要输出中的详细信息,您可以删除 -q 选项. 以下命令将使用 100 个并行连接在服务器上运行 1000000 个 SET 请求:

1redis-benchmark -t set -c 100 -n 1000000

你会得到类似于此的输出:

 1[secondary_label Output]
 2====== SET ======
 3  1000000 requests completed in 11.29 seconds
 4  100 parallel clients
 5  3 bytes payload
 6  keep alive: 1
 7
 895.22% <= 1 milliseconds
 998.97% <= 2 milliseconds
1099.86% <= 3 milliseconds
1199.95% <= 4 milliseconds
1299.99% <= 5 milliseconds
1399.99% <= 6 milliseconds
14100.00% <= 7 milliseconds
15100.00% <= 8 milliseconds
16100.00% <= 8 milliseconds
1788605.35 requests per second

默认设置使用 3 字节为关键值. 您可以通过选项 -d 来更改此设置. 以下命令将使用 1MB 关键值对比 GETSET 命令:

1redis-benchmark -t set,get -d 1000000 -n 1000 -q

由于服务器这次工作负载要大得多,预计性能将大幅下降:

1[secondary_label Output]
2SET: 1642.04 requests per second
3GET: 822.37 requests per second

重要的是要认识到,虽然这些数字作为评估 Redis 实例性能的快速方法是有用的,但它们并不代表 Redis 实例能够维持的最大输出量。 通过使用 pipelining,应用程序可以同时发送多个命令,以提高服务器每秒可以处理的请求数量。

要比较差异,首先运行redis-benchmark命令以默认值而无管道,用于GETSET测试:

1redis-benchmark -t get,set -q
1[secondary_label Output]
2SET: 86281.27 requests per second
3GET: 89847.26 requests per second

下一个命令将运行相同的测试,但将共同管道8个命令:

1redis-benchmark -t get,set -q -P 8
1[secondary_label Output]
2SET: 653594.81 requests per second
3GET: 793650.75 requests per second

正如您从输出中可以看到的那样,使用管道的性能有了显著的提高。

redis-cli来检查延迟

如果您想简单地衡量请求收到响应所需的时间,您可以使用 Redis 客户端来检查平均服务器延迟时间。

以下命令将显示您的 Redis 服务器的实时延迟统计数据:

1redis-cli --latency

您将获得类似于此的输出,显示越来越多的样本和可变平均延迟:

1[secondary_label Output]
2min: 0, max: 1, avg: 0.18 (970 samples)

这个命令将继续运行无限期,您可以用CTRL+C来停止。

为了在一定时间内监控延迟,您可以使用:

1redis-cli --latency-history

这将跟踪延迟平均时间,默认设置为 15 秒的可配置间隔,您将获得类似的输出:

 1[secondary_label Output]
 2min: 0, max: 1, avg: 0.18 (1449 samples) -- 15.01 seconds range
 3min: 0, max: 1, avg: 0.16 (1449 samples) -- 15.00 seconds range
 4min: 0, max: 1, avg: 0.17 (1449 samples) -- 15.00 seconds range
 5min: 0, max: 1, avg: 0.17 (1444 samples) -- 15.01 seconds range
 6min: 0, max: 1, avg: 0.17 (1446 samples) -- 15.01 seconds range
 7min: 0, max: 1, avg: 0.17 (1449 samples) -- 15.00 seconds range
 8min: 0, max: 1, avg: 0.16 (1444 samples) -- 15.00 seconds range
 9min: 0, max: 1, avg: 0.17 (1445 samples) -- 15.01 seconds range
10min: 0, max: 1, avg: 0.16 (1445 samples) -- 15.01 seconds range
11...

由于我们示例中的 Redis 服务器是空闲的,所以延迟样本之间没有太大的差异,但是如果您有使用峰值,则这应该反映在结果中延迟的增加中。

如果您只想测量 system 延迟,您可以使用 `-- intrinsic-latency' 来测量它,而内在延迟是环境中固有的,这取决于硬件、内核、服务器邻居和其他不受 Redis 控制的因素。

您可以将内在延迟视为您的 Redis 总体性能的基准。下列命令将检查内在系统延迟,运行测试30秒:

1redis-cli --intrinsic-latency 30

你应该得到类似于此的输出:

1[secondary_label Output]
23
4498723744 total runs (avg latency: 0.0602 microseconds / 60.15 nanoseconds per run).
5Worst run took 22975x longer than the average latency.

比较两种延迟测试可以帮助识别可能影响 Redis 服务器性能的硬件或系统瓶颈,考虑到请求到我们的示例服务器的总延迟平均需要 0.18 微秒完成,内在延迟为 0.06 微秒意味着系统在不受 Redis 控制的流程中花费了总请求时间的三分之一。

使用Memtier Benchmark工具

Memtier是由Redis Labs创建的Redis和Memcached(https://memcached.org/)的高输出量基准工具,虽然在各种方面非常类似于redis基准,但Memtier有几种配置选项,可以调整以更好地模拟您可以在您的Redis服务器上期望的负载类型,除了提供集群支持之外。

要在您的服务器上安装Memtier,您需要从源头编译软件,首先安装编译代码所需的依赖:

1sudo apt-get install build-essential autoconf automake libpcre3-dev libevent-dev pkg-config zlib1g-dev

接下来,进入您的主目录并从其 Github 存储库克隆 memtier_benchmark 项目:

1cd
2git clone https://github.com/RedisLabs/memtier_benchmark.git

导航到项目目录并运行autoreconf命令来生成应用程序配置脚本:

1cd memtier_benchmark
2autoreconf -ivf

运行配置脚本以生成编译所需的应用文物:

1./configure

现在运行Make来编译应用程序:

1make

一旦构建完成,您可以使用以下方法测试可执行:

1./memtier_benchmark --version

这将为您带来以下结果:

1[secondary_label Output]
2memtier_benchmark 1.2.17
3Copyright (C) 2011-2017 Redis Labs Ltd.
4This is free software. You may redistribute copies of it under the terms of
5the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
6There is NO WARRANTY, to the extent permitted by law.

下列列表包含一些最常见的选项,用于memtier_benchmark命令:

  • s:服务器主机. 默认为 localhost.
  • p:服务器端口. 默认为 6379.
  • a: 使用提供密码验证请求.
  • n: 客户端请求数(默认为 10000)。
  • c: 客户端数(默认为 50)。
  • t: 线程数(默认为 4)。
  • --pipeline: 允许管道。
  • --比率: SET 和 GET` 命令之间的比例,默认为 1:10.
  • `--hide-histogram:

这些选项大多与redis-benchmark中的选项非常相似,但Memtier以不同的方式测试性能. 为了更好地模拟常见的现实环境,由memtier_benchmark执行的默认基准将仅对GETSET请求进行测试,为比例为1至10。

以下命令在默认设置下运行memtier_benchmark,同时只提供高级输出信息:

1./memtier_benchmark --hide-histogram

<$>[注意] 注意:如果您已经配置了您的 Redis 服务器以要求身份验证,您应该在memtier_benchmark命令中提供-a选项以及您的 Redis 密码:

1./memtier_benchmark --hide-histogram -a your_redis_password

美元

你会看到类似于此的输出:

 1[secondary_label Output]
 2...
 3
 44 Threads
 550 Connections per thread
 610000 Requests per client
 7
 8ALL STATS
 9=========================================================================
10Type Ops/sec Hits/sec Misses/sec Latency KB/sec 
11-------------------------------------------------------------------------
12Sets 8258.50          ---          ---      2.19800 636.05 
13Gets 82494.28 41483.10 41011.18 2.19800 4590.88 
14Waits 0.00          ---          ---      0.00000          --- 
15Totals 90752.78 41483.10 41011.18 2.19800 5226.93

根據這個「memtier_benchmark」的執行,我們的Redis伺服器可以每秒執行大約90萬個操作,在1 : 10的「SET」/「GET」比率。

重要的是要注意,每个基准工具都有自己的性能测试和数据呈现算法,因此,即使在使用相似设置时,在同一服务器上有略有不同的结果也是正常的。

结论

在本指南中,我们展示了如何使用两个不同的工具在Redis服务器上进行基准测试:包含的redis基准和由Redis Labs开发的memtier_benchmark工具,我们还看到如何使用redis-cli来检查服务器延迟。

Published At
Categories with 技术
comments powered by Disqus