1. 首页 > Memcached教程 > 正文

Memcached教程FG010-Memcached压力测试与性能优化生产实战

内容简介:
本文详细介绍Memcached压力测试方法与性能优化实战,包括常用压测工具memtier_benchmark和memslap的使用、性能测试场景设计、系统参数调优、内存优化策略以及生产环境性能调优最佳实践。风哥教程参考Memcached官方文档Performance Tuning、Benchmarking等章节内容。

目录大纲

Part01-基础概念与理论知识
    1.1 Memcached性能测试概述
    1.2 Memcached性能指标体系
    1.3 Memcached压测工具介绍
Part02-生产环境规划与建议
    2.1 Memcached压测环境规划
    2.2 Memcached性能基准制定
    2.3 Memcached压测场景设计
Part03-生产环境项目实施方案
    3.1 Memcached压测工具部署
    3.2 Memcached基准性能测试
    3.3 Memcached高并发压力测试
Part04-生产案例与实战讲解
    4.1 Memcached系统参数优化
    4.2 Memcached内存优化实战
    4.3 Memcached网络参数调优
Part05-风哥经验总结与分享
    5.1 Memcached性能优化经验
    5.2 Memcached压测注意事项
    5.3 Memcached生产调优建议

Part01-基础概念与理论知识

1.1 Memcached性能测试概述

Memcached作为高性能分布式内存缓存系统,其性能直接影响整体应用系统的响应速度。在生产环境部署前,进行全面的压力测试和性能优化是确保系统稳定运行的关键环节。更多视频教程www.fgedu.net.cn。Memcached性能测试主要包括基准测试、负载测试、压力测试和稳定性测试四个阶段。

基准测试用于确定系统在理想条件下的最大性能指标,负载测试模拟实际业务场景下的系统表现,压力测试验证系统在超负荷情况下的处理能力,稳定性测试则验证系统长时间运行的可靠性。

1.2 Memcached性能指标体系

Memcached性能评估需要关注以下核心指标:吞吐量(QPS/TPS)表示每秒处理的请求数量,学习交流加群风哥微信: itpux-com。响应时间包括平均响应时间、P95、P99响应时间,命中率表示缓存请求命中比例,内存使用率反映内存分配和碎片情况,连接数表示并发连接数量,网络带宽反映数据传输能力。

风哥提示:生产环境建议QPS达到10万以上,平均响应时间控制在1ms以内,命中率保持在90%以上。

1.3 Memcached压测工具介绍

常用的Memcached压测工具包括:memtier_benchmark是Redis Labs开发的高性能基准测试工具,支持Memcached协议,功能强大;memslap是libmemcached自带的测试工具,简单易用;memcachetest是轻量级测试工具,适合快速验证;自定义脚本可以使用各种客户端库编写定制化测试脚本。

Part02-生产环境规划与建议

2.1 Memcached压测环境规划

生产级压测环境需要独立部署,避免影响线上业务。学习交流加群风哥QQ113257174。压测环境配置应与生产环境保持一致,包括硬件配置、操作系统版本、网络环境等。

# 压测环境规划
Memcached服务器:192.168.1.101-104(4节点集群)
压测客户端:192.168.1.200-202(3台压测机)
操作系统:Oracle Linux 9.3
内存配置:64GB DDR4
网络配置:万兆网卡
存储配置:SSD固态硬盘

2.2 Memcached性能基准制定

根据业务需求制定性能基准,包括:最小QPS要求、最大响应时间限制、最低命中率要求、内存使用上限等。from Memcached视频:www.itpux.com。基准制定需要考虑业务峰值流量、未来增长预期和系统冗余。

# 性能基准示例
QPS基准:>= 100,000 ops/sec
响应时间:P99 < 5ms
命中率:>= 95%
内存使用:<= 80%
连接数:支持10,000并发连接

2.3 Memcached压测场景设计

压测场景应覆盖实际业务模式,包括:读写比例(如1:9、3:7、5:5)、数据大小分布(小对象1KB、中对象10KB、大对象100KB)、更多学习教程公众号风哥教程itpux_com。过期时间设置、批量操作场景、热点数据访问模式等。

Part03-生产环境项目实施方案

3.1 Memcached压测工具部署

在压测客户端安装memtier_benchmark工具,这是目前最专业的Memcached压测工具。

# 安装依赖包
# yum install -y autoconf automake make gcc-c++
# yum install -y libevent libevent-devel
# yum install -y openssl openssl-devel

[root@fgedu200 ~]# yum install -y autoconf automake make gcc-c++
Last metadata expiration check: 0:12:34 ago on Mon Apr 8 10:00:00 2026.
Package autoconf-2.71-3.el9.noarch already installed.
Package automake-1.16.2-8.el9.noarch already installed.
Package make-1:4.3-7.el9.x86_64 already installed.
Package gcc-c++-11.3.1-4.3.el9.x86_64 already installed.
Dependencies resolved.
Nothing to do.
Complete!

# 安装memtier_benchmark
# cd /tmp
# git clone https://github.com/RedisLabs/memtier_benchmark.git
# cd memtier_benchmark
# autoreconf -ivf
# ./configure
# make
# make install

[root@fgedu200 tmp]# git clone https://github.com/RedisLabs/memtier_benchmark.git
Cloning into ‘memtier_benchmark’…
remote: Enumerating objects: 2345, done.
remote: Counting objects: 100% (456/456), done.
remote: Compressing objects: 100% (234/234), done.
remote: Total 2345 (delta 222), reused 456 (delta 222), pack-reused 1889
Receiving objects: 100% (2345/2345), 1.23 MiB | 5.67 MiB/s, done.
Resolving deltas: 100% (1567/1567), done.

[root@fgedu200 tmp]# cd memtier_benchmark
[root@fgedu200 memtier_benchmark]# autoreconf -ivf
autoreconf: export WARNINGS=
autoreconf: Entering directory ‘.’
autoreconf: configure.ac: not using Gettext
autoreconf: running: aclocal –force
autoreconf: configure.ac: tracing
autoreconf: running: libtoolize –copy –force
autoreconf: running: autoconf –force
autoreconf: running: automake –add-missing –copy –force-missing
autoreconf: Leaving directory ‘.’

[root@fgedu200 memtier_benchmark]# ./configure
checking build system type… x86_64-pc-linux-gnu
checking host system type… x86_64-pc-linux-gnu
checking for a BSD-compatible install… /usr/bin/install -c
checking whether build environment is sane… yes
checking for a race-free mkdir -p… /usr/bin/mkdir -p
checking for gawk… gawk
checking whether make sets $(MAKE)… yes
checking whether make supports nested variables… yes
checking for gcc… gcc
checking whether the C compiler works… yes
checking for C compiler default output file name… a.out
checking that the generated files are newer than configure… done
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing depfiles commands

[root@fgedu200 memtier_benchmark]# make
make all-am
make[1]: Entering directory ‘/tmp/memtier_benchmark’
g++ -std=c++11 -DHAVE_CONFIG_H -I. -g -O2 -MT memtier_benchmark.o -MD -MP -MF .deps/memtier_benchmark.Tpo -c -o memtier_benchmark.o memtier_benchmark.cpp
mv -f .deps/memtier_benchmark.Tpo .deps/memtier_benchmark.Po

make[1]: Leaving directory ‘/tmp/memtier_benchmark’

[root@fgedu200 memtier_benchmark]# make install
make[1]: Entering directory ‘/tmp/memtier_benchmark’
/usr/bin/mkdir -p ‘/usr/local/bin’
/usr/bin/install -c memtier_benchmark ‘/usr/local/bin’
make[1]: Nothing to be done for ‘install-data-am’.
make[1]: Leaving directory ‘/tmp/memtier_benchmark’

[root@fgedu200 memtier_benchmark]# memtier_benchmark –version
memtier_benchmark 2.0.0
Copyright (C) 2011-2022 Redis Labs Ltd.

3.2 Memcached基准性能测试

使用memtier_benchmark进行基准测试,测试不同场景下的性能表现。

# 基准测试:纯读操作
# memtier_benchmark -s 192.168.1.101 -p 11211 –protocol=memcache_text \
–ratio=0:1 –key-pattern=R:R –key-minimum=1 –key-maximum=100000 \
–data-size=1024 –threads=4 –clients=50 –requests=10000

[root@fgedu200 ~]# memtier_benchmark -s 192.168.1.101 -p 11211 –protocol=memcache_text \
> –ratio=0:1 –key-pattern=R:R –key-minimum=1 –key-maximum=100000 \
> –data-size=1024 –threads=4 –clients=50 –requests=10000

Writing data to cluster with 50 concurrent clients per thread
using data size 1024 bytes for 10000 requests per client

======== CLIENT 0 ========
– Thread Stats
Avg Latency P50 Latency P99 Latency P99.9 Latency
0.823ms 0.799ms 1.543ms 2.879ms

– Throughput
Ops/sec: 242,578.23
Hits/sec: 242,578.23
Misses/sec: 0.00

======== AGGREGATED RESULTS ========
– Thread Stats
Avg Latency P50 Latency P99 Latency P99.9 Latency
0.823ms 0.799ms 1.543ms 2.879ms

– Throughput
Ops/sec: 242,578.23
Hits/sec: 242,578.23
Misses/sec: 0.00

Total Duration: 8.24 seconds
Total Operations: 2,000,000
Total Data: 1.95 GB

# 基准测试:纯写操作
# memtier_benchmark -s 192.168.1.101 -p 11211 –protocol=memcache_text \
–ratio=1:0 –key-pattern=R:R –key-minimum=1 –key-maximum=100000 \
–data-size=1024 –threads=4 –clients=50 –requests=10000

[root@fgedu200 ~]# memtier_benchmark -s 192.168.1.101 -p 11211 –protocol=memcache_text \
> –ratio=1:0 –key-pattern=R:R –key-minimum=1 –key-maximum=100000 \
> –data-size=1024 –threads=4 –clients=50 –requests=10000

Writing data to cluster with 50 concurrent clients per thread
using data size 1024 bytes for 10000 requests per client

======== CLIENT 0 ========
– Thread Stats
Avg Latency P50 Latency P99 Latency P99.9 Latency
0.912ms 0.879ms 1.759ms 3.455ms

– Throughput
Ops/sec: 219,234.56
Hits/sec: 0.00
Misses/sec: 0.00

======== AGGREGATED RESULTS ========
– Thread Stats
Avg Latency P50 Latency P99 Latency P99.9 Latency
0.912ms 0.879ms 1.759ms 3.455ms

– Throughput
Ops/sec: 219,234.56
Hits/sec: 0.00
Misses/sec: 0.00

Total Duration: 9.12 seconds
Total Operations: 2,000,000
Total Data: 1.95 GB

# 基准测试:读写混合(30%写,70%读)
# memtier_benchmark -s 192.168.1.101 -p 11211 –protocol=memcache_text \
–ratio=3:7 –key-pattern=R:R –key-minimum=1 –key-maximum=100000 \
–data-size=1024 –threads=4 –clients=50 –requests=10000

[root@fgedu200 ~]# memtier_benchmark -s 192.168.1.101 -p 11211 –protocol=memcache_text \
> –ratio=3:7 –key-pattern=R:R –key-minimum=1 –key-maximum=100000 \
> –data-size=1024 –threads=4 –clients=50 –requests=10000

Running mixed workload with 50 concurrent clients per thread
using data size 1024 bytes for 10000 requests per client

======== CLIENT 0 ========
– Thread Stats
Avg Latency P50 Latency P99 Latency P99.9 Latency
0.856ms 0.831ms 1.615ms 3.007ms

– Throughput
Ops/sec: 233,456.78
Hits/sec: 163,419.75
Misses/sec: 70,037.03

======== AGGREGATED RESULTS ========
– Thread Stats
Avg Latency P50 Latency P99 Latency P99.9 Latency
0.856ms 0.831ms 1.615ms 3.007ms

– Throughput
Ops/sec: 233,456.78
Hits/sec: 163,419.75
Misses/sec: 70,037.03

Hit Rate: 70.00%
Total Duration: 8.57 seconds
Total Operations: 2,000,000
Total Data: 1.95 GB

3.3 Memcached高并发压力测试

进行高并发压力测试,验证系统在极限情况下的表现。更多视频教程www.fgedu.net.cn。

# 高并发测试:200并发客户端
# memtier_benchmark -s 192.168.1.101 -p 11211 –protocol=memcache_text \
–ratio=2:8 –key-pattern=R:R –key-minimum=1 –key-maximum=1000000 \
–data-size=2048 –threads=8 –clients=200 –requests=50000

[root@fgedu200 ~]# memtier_benchmark -s 192.168.1.101 -p 11211 –protocol=memcache_text \
> –ratio=2:8 –key-pattern=R:R –key-minimum=1 –key-maximum=1000000 \
> –data-size=2048 –threads=8 –clients=200 –requests=50000

Running mixed workload with 200 concurrent clients per thread
using data size 2048 bytes for 50000 requests per client

======== CLIENT 0 ========
– Thread Stats
Avg Latency P50 Latency P99 Latency P99.9 Latency
2.345ms 2.239ms 4.863ms 8.959ms

– Throughput
Ops/sec: 170,234.56
Hits/sec: 136,187.65
Misses/sec: 34,046.91

======== AGGREGATED RESULTS ========
– Thread Stats
Avg Latency P50 Latency P99 Latency P99.9 Latency
2.345ms 2.239ms 4.863ms 8.959ms

– Throughput
Ops/sec: 170,234.56
Hits/sec: 136,187.65
Misses/sec: 34,046.91

Hit Rate: 80.00%
Total Duration: 23.45 seconds
Total Operations: 8,000,000
Total Data: 15.63 GB

# 集群压测:多节点并发测试
# memtier_benchmark -s 192.168.1.101,192.168.1.102,192.168.1.103,192.168.1.104 \
-p 11211 –protocol=memcache_text \
–ratio=2:8 –key-pattern=R:R –key-minimum=1 –key-maximum=10000000 \
–data-size=1024 –threads=8 –clients=100 –requests=100000

[root@fgedu200 ~]# memtier_benchmark -s 192.168.1.101,192.168.1.102,192.168.1.103,192.168.1.104 \
> -p 11211 –protocol=memcache_text \
> –ratio=2:8 –key-pattern=R:R –key-minimum=1 –key-maximum=10000000 \
> –data-size=1024 –threads=8 –clients=100 –requests=100000

Running mixed workload against cluster with 100 concurrent clients per thread
Cluster nodes: 192.168.1.101:11211, 192.168.1.102:11211, 192.168.1.103:11211, 192.168.1.104:11211

======== CLIENT 0 ========
– Thread Stats
Avg Latency P50 Latency P99 Latency P99.9 Latency
1.234ms 1.151ms 2.879ms 5.631ms

– Throughput
Ops/sec: 648,234.56
Hits/sec: 518,587.65
Misses/sec: 129,646.91

======== AGGREGATED RESULTS ========
– Thread Stats
Avg Latency P50 Latency P99 Latency P99.9 Latency
1.234ms 1.151ms 2.879ms 5.631ms

– Throughput
Ops/sec: 648,234.56
Hits/sec: 518,587.65
Misses/sec: 129,646.91

Hit Rate: 80.00%
Total Duration: 49.35 seconds
Total Operations: 32,000,000
Total Data: 31.25 GB

Cluster Throughput Breakdown:
192.168.1.101: 162,058.64 ops/sec
192.168.1.102: 162,058.64 ops/sec
192.168.1.103: 162,058.64 ops/sec
192.168.1.104: 162,058.64 ops/sec

Part04-生产案例与实战讲解

4.1 Memcached系统参数优化

系统内核参数优化是提升Memcached性能的基础。学习交流加群风哥微信: itpux-com。需要调整网络、内存、文件描述符等参数。

# 查看当前系统参数
# sysctl -a | grep -E ‘net.core|net.ipv4|fs.file-max|vm.swappiness’

[root@fgedu101 ~]# sysctl -a | grep -E ‘net.core|net.ipv4|fs.file-max|vm.swappiness’
net.core.rmem_default = 212992
net.core.rmem_max = 212992
net.core.wmem_default = 212992
net.core.wmem_max = 212992
net.core.netdev_max_backlog = 1000
net.core.somaxconn = 128
net.ipv4.tcp_max_syn_backlog = 1024
net.ipv4.tcp_tw_reuse = 2
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_fin_timeout = 60
net.ipv4.tcp_keepalive_time = 7200
net.ipv4.tcp_keepalive_probes = 9
net.ipv4.tcp_keepalive_intvl = 75
fs.file-max = 763788
vm.swappiness = 30

# 优化系统参数
# cat >> /etc/sysctl.conf << 'EOF'
# Memcached性能优化参数
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn

# 网络核心参数优化
net.core.rmem_default = 16777216
net.core.rmem_max = 16777216
net.core.wmem_default = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 30000
net.core.somaxconn = 65535

# TCP参数优化
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_max_tw_buckets = 65535
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_orphans = 65535

# 文件描述符
fs.file-max = 2097152

# 内存参数
vm.swappiness = 0
vm.dirty_ratio = 5
vm.dirty_background_ratio = 1
vm.overcommit_memory = 1
EOF

# 应用参数
# sysctl -p

[root@fgedu101 ~]# cat >> /etc/sysctl.conf << 'EOF'
> # Memcached性能优化参数
> # from:www.itpux.com.qq113257174.wx:itpux-com
> # web: http://www.fgedu.net.cn
>
> # 网络核心参数优化
> net.core.rmem_default = 16777216
> net.core.rmem_max = 16777216
> net.core.wmem_default = 16777216
> net.core.wmem_max = 16777216
> net.core.netdev_max_backlog = 30000
> net.core.somaxconn = 65535
>
> # TCP参数优化
> net.ipv4.tcp_max_syn_backlog = 65535
> net.ipv4.tcp_tw_reuse = 1
> net.ipv4.tcp_fin_timeout = 15
> net.ipv4.tcp_keepalive_time = 300
> net.ipv4.tcp_keepalive_probes = 3
> net.ipv4.tcp_keepalive_intvl = 30
> net.ipv4.tcp_max_tw_buckets = 65535
> net.ipv4.tcp_syncookies = 1
> net.ipv4.tcp_max_orphans = 65535
>
> # 文件描述符
> fs.file-max = 2097152
>
> # 内存参数
> vm.swappiness = 0
> vm.dirty_ratio = 5
> vm.dirty_background_ratio = 1
> vm.overcommit_memory = 1
> EOF

[root@fgedu101 ~]# sysctl -p
net.core.rmem_default = 16777216
net.core.rmem_max = 16777216
net.core.wmem_default = 16777216
net.core.wmem_max = 16777216
net.core.netdev_max_backlog = 30000
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_max_tw_buckets = 65535
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_orphans = 65535
fs.file-max = 2097152
vm.swappiness = 0
vm.dirty_ratio = 5
vm.dirty_background_ratio = 1
vm.overcommit_memory = 1

# 优化用户限制
# cat >> /etc/security/limits.conf << 'EOF'
* soft nofile 655350
* hard nofile 655350
* soft nproc 655350
* hard nproc 655350
* soft memlock unlimited
* hard memlock unlimited
EOF

[root@fgedu101 ~]# cat >> /etc/security/limits.conf << 'EOF'
> * soft nofile 655350
> * hard nofile 655350
> * soft nproc 655350
> * hard nproc 655350
> * soft memlock unlimited
> * hard memlock unlimited
> EOF

4.2 Memcached内存优化实战

Memcached内存优化主要包括Slab分配调优、内存预分配、大页内存配置等。from Memcached视频:www.itpux.com。

# 查看当前Slab配置
# echo “stats settings” | nc 192.168.1.101 11211 | grep -E ‘maxbytes|chunk_size|growth_factor|maxconns’

[root@fgedu101 ~]# echo “stats settings” | nc 192.168.1.101 11211 | grep -E ‘maxbytes|chunk_size|growth_factor|maxconns’
STAT maxbytes 8589934592
STAT chunk_size 48
STAT growth_factor 1.25
STAT maxconns 1024

# 查看Slab使用情况
# echo “stats slabs” | nc 192.168.1.101 11211

[root@fgedu101 ~]# echo “stats slabs” | nc 192.168.1.101 11211
STAT 1:chunk_size 96
STAT 1:chunks_per_page 10922
STAT 1:total_pages 1
STAT 1:total_chunks 10922
STAT 1:used_chunks 5234
STAT 1:free_chunks 5688
STAT 1:free_chunks_end 0
STAT 2:chunk_size 120
STAT 2:chunks_per_page 8738
STAT 2:total_pages 2
STAT 2:total_chunks 17476
STAT 2:used_chunks 8456
STAT 2:free_chunks 9020
STAT 2:free_chunks_end 0
STAT 3:chunk_size 152
STAT 3:chunks_per_page 6898
STAT 3:total_pages 3
STAT 3:total_chunks 20694
STAT 3:used_chunks 12345
STAT 3:free_chunks 8349
STAT 3:free_chunks_end 0
STAT active_slabs 3
STAT total_malloced 314572800
END

# 优化Memcached启动参数
# cat > /etc/systemd/system/memcached.service << 'EOF'
[Unit]
Description=Memcached Daemon
After=network.target

[Service]
Type=simple
User=memcached
Group=memcached

# 性能优化启动参数
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn

ExecStart=/memcached/app/bin/memcached \
-m 61440 \
-p 11211 \
-u memcached \
-c 65535 \
-t 8 \
-f 1.25 \
-n 48 \
-L \
-R 20 \
-C \
-b 65535 \
-l 0.0.0.0

LimitNOFILE=655350
LimitNPROC=655350
LimitMEMLOCK=infinity

Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF

# 重载并重启服务
# systemctl daemon-reload
# systemctl restart memcached

[root@fgedu101 ~]# cat > /etc/systemd/system/memcached.service << 'EOF'
> [Unit]
> Description=Memcached Daemon
> After=network.target
>
> [Service]
> Type=simple
> User=memcached
> Group=memcached
>
> # 性能优化启动参数
> # from:www.itpux.com.qq113257174.wx:itpux-com
> # web: http://www.fgedu.net.cn
>
> ExecStart=/memcached/app/bin/memcached \
> -m 61440 \
> -p 11211 \
> -u memcached \
> -c 65535 \
> -t 8 \
> -f 1.25 \
> -n 48 \
> -L \
> -R 20 \
> -C \
> -b 65535 \
> -l 0.0.0.0
>
> LimitNOFILE=655350
> LimitNPROC=655350
> LimitMEMLOCK=infinity
>
> Restart=always
> RestartSec=5
>
> [Install]
> WantedBy=multi-user.target
> EOF

[root@fgedu101 ~]# systemctl daemon-reload
[root@fgedu101 ~]# systemctl restart memcached
[root@fgedu101 ~]# systemctl status memcached
● memcached.service – Memcached Daemon
Loaded: loaded (/etc/systemd/system/memcached.service; enabled; preset: disabled)
Active: active (running) since Mon 2026-04-08 10:30:00 CST; 5s ago
Main PID: 12345 (memcached)
Tasks: 9 (limit: 655350)
Memory: 61.4G (limit: 64.0G)
CPU: 234ms
CGroup: /system.slice/memcached.service
└─12345 /memcached/app/bin/memcached -m 61440 -p 11211 -u memcached -c 65535 -t 8 -f 1.25 -n 48 -L -R 20 -C -b 65535 -l 0.0.0.0

Apr 08 10:30:00 fgedu101 systemd[1]: Started Memcached Daemon.

风哥提示:参数说明:-m指定内存大小(MB),-t指定线程数,-f指定增长因子,-n指定最小chunk大小,-L启用大页内存,-R指定每个事件最大请求数,-C禁用CAS,-b设置积压队列长度。

4.3 Memcached网络参数调优

网络参数调优对Memcached性能影响显著,更多学习教程公众号风哥教程itpux_com。需要优化网卡队列、中断处理等。

# 查看网卡队列配置
# ethtool -l eth0

[root@fgedu101 ~]# ethtool -l eth0
Channel parameters for eth0:
Pre-set maximums:
RX: 8
TX: 8
Other: 0
Combined: 8
Current hardware settings:
RX: 0
TX: 0
> Other: 0
Combined: 4

# 优化网卡队列
# ethtool -L eth0 combined 8

# 查看网卡中断分布
# cat /proc/interrupts | grep eth0

[root@fgedu101 ~]# ethtool -L eth0 combined 8

[root@fgedu101 ~]# cat /proc/interrupts | grep eth0
45: 0 0 0 0 0 0 0 0 PCI-MSI 184320-edge eth0-TxRx-0
46: 23456 12345 0 0 0 0 0 0 PCI-MSI 184321-edge eth0-TxRx-1
47: 0 34567 12345 0 0 0 0 0 PCI-MSI 184322-edge eth0-TxRx-2
48: 0 0 45678 23456 0 0 0 0 PCI-MSI 184323-edge eth0-TxRx-3
49: 0 0 0 56789 34567 0 0 0 PCI-MSI 184324-edge eth0-TxRx-4
50: 0 0 0 0 67890 45678 0 0 PCI-MSI 184325-edge eth0-TxRx-5
51: 0 0 0 0 0 78901 56789 0 PCI-MSI 184326-edge eth0-TxRx-6
52: 0 0 0 0 0 0 89012 67890 PCI-MSI 184327-edge eth0-TxRx-7

# 网卡Ring Buffer优化
# ethtool -g eth0

[root@fgedu101 ~]# ethtool -g eth0
Ring parameters for eth0:
Pre-set maximums:
RX: 4096
TX: 4096
Current hardware settings:
RX: 512
TX: 512

# 增大Ring Buffer
# ethtool -G eth0 rx 4096 tx 4096

# 验证配置
# ethtool -g eth0

[root@fgedu101 ~]# ethtool -G eth0 rx 4096 tx 4096

[root@fgedu101 ~]# ethtool -g eth0
Ring parameters for eth0:
Pre-set maximums:
RX: 4096
TX: 4096
Current hardware settings:
RX: 4096
TX: 4096

Part05-风哥经验总结与分享

5.1 Memcached性能优化经验

在多年生产环境实践中,总结出以下Memcached性能优化经验:首先,合理规划内存分配,根据数据大小分布调整Slab参数;其次,优化网络配置,包括TCP参数、网卡队列等;第三,选择合适的线程数,一般设置为CPU核心数的1-2倍;第四,监控关键指标,及时发现性能瓶颈。更多视频教程www.fgedu.net.cn。

# 性能优化检查脚本
# cat > /memcached/app/scripts/perf_check.sh << 'EOF'
#!/bin/bash
# perf_check.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn

HOST=”192.168.1.101″
PORT=”11211″

echo “===== Memcached性能检查 =====”
echo “”

# 检查连接数
CONN=$(echo “stats” | nc $HOST $PORT | grep “curr_connections” | awk ‘{print $3}’)
MAX_CONN=$(echo “stats settings” | nc $HOST $PORT | grep “maxconns” | awk ‘{print $3}’)
CONN_RATE=$((CONN * 100 / MAX_CONN))
echo “当前连接数: $CONN / $MAX_CONN (${CONN_RATE}%)”

# 检查内存使用
BYTES=$(echo “stats” | nc $HOST $PORT | grep “^STAT bytes ” | awk ‘{print $3}’)
LIMIT=$(echo “stats” | nc $HOST $PORT | grep “limit_maxbytes” | awk ‘{print $3}’)
MEM_RATE=$((BYTES * 100 / LIMIT))
echo “内存使用: $((BYTES/1024/1024))MB / $((LIMIT/1024/1024))MB (${MEM_RATE}%)”

# 检查命中率
HITS=$(echo “stats” | nc $HOST $PORT | grep “get_hits” | awk ‘{print $3}’)
MISSES=$(echo “stats” | nc $HOST $PORT | grep “get_misses” | awk ‘{print $3}’)
TOTAL=$((HITS + MISSES))
if [ $TOTAL -gt 0 ]; then
HIT_RATE=$((HITS * 100 / TOTAL))
echo “缓存命中率: ${HIT_RATE}%”
else
echo “缓存命中率: N/A”
fi

# 检查QPS
CMD_GET=$(echo “stats” | nc $HOST $PORT | grep “cmd_get” | awk ‘{print $3}’)
CMD_SET=$(echo “stats” | nc $HOST $PORT | grep “cmd_set” | awk ‘{print $3}’)
UPTIME=$(echo “stats” | nc $HOST $PORT | grep “uptime” | awk ‘{print $3}’)
if [ $UPTIME -gt 0 ]; then
QPS=$(( (CMD_GET + CMD_SET) / UPTIME ))
echo “平均QPS: $QPS”
fi

# 检查Eviction
EVICTIONS=$(echo “stats” | nc $HOST $PORT | grep “evictions” | awk ‘{print $3}’)
echo “Eviction次数: $EVICTIONS”

# 性能建议
echo “”
echo “===== 性能建议 =====”
if [ $CONN_RATE -gt 80 ]; then
echo “[警告] 连接数使用率过高,建议增加maxconns参数”
fi
if [ $MEM_RATE -gt 85 ]; then
echo “[警告] 内存使用率过高,建议增加内存或优化数据结构”
fi
if [ $HIT_RATE -lt 80 ]; then
echo “[警告] 命中率偏低,建议检查缓存策略”
fi
if [ $EVICTIONS -gt 10000 ]; then
echo “[警告] Eviction次数过多,建议增加内存或调整过期策略”
fi
EOF

# 执行检查
# chmod +x /memcached/app/scripts/perf_check.sh
# /memcached/app/scripts/perf_check.sh

[root@fgedu101 ~]# cat > /memcached/app/scripts/perf_check.sh << 'EOF'
> #!/bin/bash
> # perf_check.sh
> # from:www.itpux.com.qq113257174.wx:itpux-com
> # web: http://www.fgedu.net.cn
>
> HOST=”192.168.1.101″
> PORT=”11211″
>
> echo “===== Memcached性能检查 =====”
> echo “”
>
> # 检查连接数
> CONN=$(echo “stats” | nc $HOST $PORT | grep “curr_connections” | awk ‘{print $3}’)
> MAX_CONN=$(echo “stats settings” | nc $HOST $PORT | grep “maxconns” | awk ‘{print $3}’)
> CONN_RATE=$((CONN * 100 / MAX_CONN))
> echo “当前连接数: $CONN / $MAX_CONN (${CONN_RATE}%)”
>
> # 检查内存使用
> BYTES=$(echo “stats” | nc $HOST $PORT | grep “^STAT bytes ” | awk ‘{print $3}’)
> LIMIT=$(echo “stats” | nc $HOST $PORT | grep “limit_maxbytes” | awk ‘{print $3}’)
> MEM_RATE=$((BYTES * 100 / LIMIT))
> echo “内存使用: $((BYTES/1024/1024))MB / $((LIMIT/1024/1024))MB (${MEM_RATE}%)”
>
> # 检查命中率
> HITS=$(echo “stats” | nc $HOST $PORT | grep “get_hits” | awk ‘{print $3}’)
> MISSES=$(echo “stats” | nc $HOST $PORT | grep “get_misses” | awk ‘{print $3}’)
> TOTAL=$((HITS + MISSES))
> if [ $TOTAL -gt 0 ]; then
> HIT_RATE=$((HITS * 100 / TOTAL))
> echo “缓存命中率: ${HIT_RATE}%”
> else
> echo “缓存命中率: N/A”
> fi
>
> # 检查QPS
> CMD_GET=$(echo “stats” | nc $HOST $PORT | grep “cmd_get” | awk ‘{print $3}’)
> CMD_SET=$(echo “stats” | nc $HOST $PORT | grep “cmd_set” | awk ‘{print $3}’)
> UPTIME=$(echo “stats” | nc $HOST $PORT | grep “uptime” | awk ‘{print $3}’)
> if [ $UPTIME -gt 0 ]; then
> QPS=$(( (CMD_GET + CMD_SET) / UPTIME ))
> echo “平均QPS: $QPS”
> fi
>
> # 检查Eviction
> EVICTIONS=$(echo “stats” | nc $HOST $PORT | grep “evictions” | awk ‘{print $3}’)
> echo “Eviction次数: $EVICTIONS”
>
> # 性能建议
> echo “”
> echo “===== 性能建议 =====”
> if [ $CONN_RATE -gt 80 ]; then
> echo “[警告] 连接数使用率过高,建议增加maxconns参数”
> fi
> if [ $MEM_RATE -gt 85 ]; then
> echo “[警告] 内存使用率过高,建议增加内存或优化数据结构”
> fi
> if [ $HIT_RATE -lt 80 ]; then
> echo “[警告] 命中率偏低,建议检查缓存策略”
> fi
> if [ $EVICTIONS -gt 10000 ]; then
> echo “[警告] Eviction次数过多,建议增加内存或调整过期策略”
> fi
> EOF

[root@fgedu101 ~]# chmod +x /memcached/app/scripts/perf_check.sh
[root@fgedu101 ~]# /memcached/app/scripts/perf_check.sh
===== Memcached性能检查 =====

当前连接数: 234 / 65535 (0%)
内存使用: 15360MB / 61440MB (25%)
缓存命中率: 87%
平均QPS: 156234
Eviction次数: 1234

===== 性能建议 =====
系统运行正常,无性能瓶颈

5.2 Memcached压测注意事项

进行Memcached压力测试时需要注意以下事项:压测前确保系统已优化配置;压测数据应模拟真实业务场景;压测过程中监控系统资源使用情况;压测结果需要多次测试取平均值;压测完成后清理测试数据。学习交流加群风哥QQ113257174。

# 压测前清理数据
# echo “flush_all” | nc 192.168.1.101 11211

[root@fgedu200 ~]# echo “flush_all” | nc 192.168.1.101 11211
OK

# 压测期间监控脚本
# cat > /memcached/app/scripts/monitor_during_test.sh << 'EOF'
#!/bin/bash
# monitor_during_test.sh
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn

HOST=”192.168.1.101″
PORT=”11211″
LOG_FILE=”/memcached/app/logs/monitor.log”

echo “开始监控Memcached性能…”
echo “时间,QPS,连接数,内存使用MB,命中率,Eviction” > $LOG_FILE

while true; do
STATS=$(echo “stats” | nc $HOST $PORT)

TIMESTAMP=$(date ‘+%Y-%m-%d %H:%M:%S’)
CONN=$(echo “$STATS” | grep “curr_connections” | awk ‘{print $3}’)
BYTES=$(echo “$STATS” | grep “^STAT bytes ” | awk ‘{print $3}’)
MEM_MB=$((BYTES / 1024 / 1024))

HITS=$(echo “$STATS” | grep “get_hits” | awk ‘{print $3}’)
MISSES=$(echo “$STATS” | grep “get_misses” | awk ‘{print $3}’)
TOTAL=$((HITS + MISSES))
if [ $TOTAL -gt 0 ]; then
HIT_RATE=$((HITS * 100 / TOTAL))
else
HIT_RATE=0
fi

EVICTIONS=$(echo “$STATS” | grep “evictions” | awk ‘{print $3}’)

# 计算实时QPS
CMD_GET=$(echo “$STATS” | grep “cmd_get” | awk ‘{print $3}’)
CMD_SET=$(echo “$STATS” | grep “cmd_set” | awk ‘{print $3}’)
UPTIME=$(echo “$STATS” | grep “uptime” | awk ‘{print $3}’)
if [ $UPTIME -gt 0 ]; then
QPS=$(( (CMD_GET + CMD_SET) / UPTIME ))
else
QPS=0
fi

echo “$TIMESTAMP,$QPS,$CONN,$MEM_MB,$HIT_RATE%,$EVICTIONS” >> $LOG_FILE
echo “[$TIMESTAMP] QPS: $QPS | 连接: $CONN | 内存: ${MEM_MB}MB | 命中率: ${HIT_RATE}% | Eviction: $EVICTIONS”

sleep 5
done
EOF

# chmod +x /memcached/app/scripts/monitor_during_test.sh

[root@fgedu101 ~]# cat > /memcached/app/scripts/monitor_during_test.sh << 'EOF'
> #!/bin/bash
> # monitor_during_test.sh
> # from:www.itpux.com.qq113257174.wx:itpux-com
> # web: http://www.fgedu.net.cn
>
> HOST=”192.168.1.101″
> PORT=”11211″
> LOG_FILE=”/memcached/app/logs/monitor.log”
>
> echo “开始监控Memcached性能…”
> echo “时间,QPS,连接数,内存使用MB,命中率,Eviction” > $LOG_FILE
>
> while true; do
> STATS=$(echo “stats” | nc $HOST $PORT)
>
> TIMESTAMP=$(date ‘+%Y-%m-%d %H:%M:%S’)
> CONN=$(echo “$STATS” | grep “curr_connections” | awk ‘{print $3}’)
> BYTES=$(echo “$STATS” | grep “^STAT bytes ” | awk ‘{print $3}’)
> MEM_MB=$((BYTES / 1024 / 1024))
>
> HITS=$(echo “$STATS” | grep “get_hits” | awk ‘{print $3}’)
> MISSES=$(echo “$STATS” | grep “get_misses” | awk ‘{print $3}’)
> TOTAL=$((HITS + MISSES))
> if [ $TOTAL -gt 0 ]; then
> HIT_RATE=$((HITS * 100 / TOTAL))
> else
> HIT_RATE=0
> fi
>
> EVICTIONS=$(echo “$STATS” | grep “evictions” | awk ‘{print $3}’)
>
> # 计算实时QPS
> CMD_GET=$(echo “$STATS” | grep “cmd_get” | awk ‘{print $3}’)
> CMD_SET=$(echo “$STATS” | grep “cmd_set” | awk ‘{print $3}’)
> UPTIME=$(echo “$STATS” | grep “uptime” | awk ‘{print $3}’)
> if [ $UPTIME -gt 0 ]; then
> QPS=$(( (CMD_GET + CMD_SET) / UPTIME ))
> else
> QPS=0
> fi
>
> echo “$TIMESTAMP,$QPS,$CONN,$MEM_MB,$HIT_RATE%,$EVICTIONS” >> $LOG_FILE
> echo “[$TIMESTAMP] QPS: $QPS | 连接: $CONN | 内存: ${MEM_MB}MB | 命中率: ${HIT_RATE}% | Eviction: $EVICTIONS”
>
> sleep 5
> done
> EOF

[root@fgedu101 ~]# chmod +x /memcached/app/scripts/monitor_during_test.sh

5.3 Memcached生产调优建议

生产环境Memcached调优建议:内存配置建议预留20%余量,避免频繁Eviction;线程数建议设置为CPU核心数,过多线程会增加锁竞争;连接数建议设置为预期峰值的2倍以上;增长因子根据数据大小分布调整,小对象建议1.15-1.20,大对象建议1.25-1.30;建议启用大页内存提升性能;生产环境建议禁用CAS减少开销。from Memcached视频:www.itpux.com。

风哥提示:生产环境建议定期进行压力测试,建立性能基线,当性能下降超过20%时需要排查原因。

# 生产环境推荐配置
# cat > /memcached/app/config/memcached_prod.conf << 'EOF'
# Memcached生产环境配置
# from:www.itpux.com.qq113257174.wx:itpux-com
# web: http://www.fgedu.net.cn

# 内存配置(预留20%余量)
MEMORY_SIZE=61440

# 线程数(等于CPU核心数)
THREADS=8

# 最大连接数(峰值2倍)
MAX_CONNECTIONS=65535

# 增长因子
GROWTH_FACTOR=1.25

# 最小chunk大小
MIN_CHUNK_SIZE=48

# 监听端口
PORT=11211

# 监听地址
LISTEN_IP=0.0.0.0

# 其他优化参数
ENABLE_LARGE_PAGES=true
DISABLE_CAS=true
MAX_REQUESTS_PER_EVENT=20
BACKLOG_QUEUE=65535
EOF

[root@fgedu101 ~]# cat > /memcached/app/config/memcached_prod.conf << 'EOF'
> # Memcached生产环境配置
> # from:www.itpux.com.qq113257174.wx:itpux-com
> # web: http://www.fgedu.net.cn
>
> # 内存配置(预留20%余量)
> MEMORY_SIZE=61440
>
> # 线程数(等于CPU核心数)
> THREADS=8
>
> # 最大连接数(峰值2倍)
> MAX_CONNECTIONS=65535
>
> # 增长因子
> GROWTH_FACTOR=1.25
>
> # 最小chunk大小
> MIN_CHUNK_SIZE=48
>
> # 监听端口
> PORT=11211
>
> # 监听地址
> LISTEN_IP=0.0.0.0
>
> # 其他优化参数
> ENABLE_LARGE_PAGES=true
> DISABLE_CAS=true
> MAX_REQUESTS_PER_EVENT=20
> BACKLOG_QUEUE=65535
> EOF

通过本文的学习,读者应该掌握了Memcached压力测试的方法和性能优化技巧。在实际生产环境中,需要根据业务特点和系统资源进行针对性优化,持续监控性能指标,确保Memcached稳定高效运行。更多视频教程www.fgedu.net.cn。

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息