性能调优 #

一、性能调优概述 #

1.1 性能指标 #

指标 说明 目标值
QPS 每秒请求数 10000+
延迟 响应时间 <10ms
命中率 缓存命中率 >90%
CPU使用率 CPU占用 <70%
内存使用 内存占用 合理范围

1.2 调优方向 #

  • 存储配置优化
  • 线程池优化
  • 内存管理优化
  • 内核参数优化
  • 网络参数优化

二、存储配置 #

2.1 存储类型 #

类型 说明 适用场景
malloc 内存存储 小型缓存
file 文件映射 大型缓存
jemalloc 高效内存分配 高性能场景

2.2 malloc存储 #

bash
# 启动参数
varnishd -s malloc,1G

# 多个存储
varnishd -s malloc,512m -s file,/var/lib/varnish/storage.bin,2G

2.3 file存储 #

bash
# 文件存储
varnishd -s file,/var/lib/varnish/storage.bin,10G

# 预分配文件
varnishd -s file,/var/lib/varnish/storage.bin,10G,granular

2.4 存储选择建议 #

场景 推荐配置
小型站点 malloc,1G
中型站点 malloc,4G
大型站点 file,/path/to/storage,100G
超大型站点 多个file存储

三、线程池配置 #

3.1 线程参数 #

参数 默认值 说明
thread_pools 2 线程池数量
thread_pool_min 100 最小线程数
thread_pool_max 5000 最大线程数
thread_pool_timeout 300 空闲线程超时
thread_pool_destroy_delay 1 销毁延迟
thread_pool_add_delay 2 添加延迟
thread_pool_fail_delay 200ms 失败延迟

3.2 线程配置 #

bash
# 启动参数
varnishd -p thread_pools=4 \
         -p thread_pool_min=200 \
         -p thread_pool_max=5000 \
         -p thread_pool_timeout=300

# 运行时修改
varnishadm param.set thread_pool_min 200
varnishadm param.set thread_pool_max 5000

3.3 线程调优建议 #

bash
# CPU核心数
CPU_CORES=$(nproc)

# 推荐配置
thread_pools = CPU_CORES
thread_pool_min = 100 * thread_pools
thread_pool_max = 1000 * thread_pools

3.4 查看线程状态 #

bash
# 查看线程统计
varnishstat -1 -f MAIN.threads

# 查看线程队列
varnishstat -1 -f MAIN.thread_queue_len

四、内存配置 #

4.1 工作区内存 #

参数 默认值 说明
workspace_client 64k 客户端工作区
workspace_backend 64k 后端工作区
http_req_size 32k HTTP请求大小
http_resp_size 32k HTTP响应大小

4.2 内存配置 #

bash
# 启动参数
varnishd -p workspace_client=128k \
         -p workspace_backend=128k \
         -p http_req_size=64k \
         -p http_resp_size=64k

4.3 大请求处理 #

bash
# 处理大请求头
varnishd -p http_req_size=64k \
         -p http_req_hdr_len=8k

# 处理大响应
varnishd -p http_resp_size=64k \
         -p http_resp_hdr_len=8k

五、超时配置 #

5.1 连接超时 #

参数 默认值 说明
connect_timeout 3.5s 连接超时
first_byte_timeout 60s 首字节超时
between_bytes_timeout 60s 字节间超时
send_timeout 600s 发送超时
idle_send_timeout 60s 空闲发送超时

5.2 超时配置 #

bash
# 启动参数
varnishd -p connect_timeout=5 \
         -p first_byte_timeout=90 \
         -p between_bytes_timeout=30 \
         -p send_timeout=300

# 后端级别配置
backend default {
    .host = "127.0.0.1";
    .port = "8080";
    .connect_timeout = 5s;
    .first_byte_timeout = 90s;
    .between_bytes_timeout = 30s;
}

5.3 客户端超时 #

bash
# 客户端超时
varnishd -p timeout_idle=5 \
         -p timeout_linger=1 \
         -p sess_timeout=5

六、内核参数优化 #

6.1 网络参数 #

bash
# /etc/sysctl.conf

# 最大文件描述符
fs.file-max = 1000000

# TCP连接队列
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535

# TCP缓冲区
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.rmem_default = 1048576
net.core.wmem_default = 1048576
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216

# TCP连接
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_max_tw_buckets = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30

# 保持连接
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3

# 本地端口范围
net.ipv4.ip_local_port_range = 1024 65535

6.2 应用参数 #

bash
# 应用限制
# /etc/security/limits.conf

varnish soft nofile 65535
varnish hard nofile 65535
varnish soft nproc 65535
varnish hard nproc 65535
varnish soft memlock unlimited
varnish hard memlock unlimited

6.3 应用配置 #

bash
# 应用内核参数
sudo sysctl -p

七、缓存优化 #

7.1 TTL优化 #

vcl
sub vcl_backend_response {
    # 静态资源长缓存
    if (bereq.url ~ "\.(css|js|png|gif|jpg|jpeg|ico|svg|woff|woff2)$") {
        set beresp.ttl = 30d;
    }
    
    # HTML中等缓存
    if (beresp.http.Content-Type ~ "text/html") {
        set beresp.ttl = 5m;
        set beresp.grace = 1h;
    }
    
    # API短缓存
    if (bereq.url ~ "^/api/") {
        set beresp.ttl = 10s;
    }
}

7.2 Grace优化 #

vcl
sub vcl_backend_response {
    # 设置Grace
    set beresp.grace = 1h;
}

sub vcl_hit {
    # Grace期间返回过期内容
    if (obj.ttl >= 0s) {
        return (deliver);
    }
    if (obj.ttl + obj.grace > 0s) {
        return (deliver);
    }
    return (restart);
}

7.3 缓存键优化 #

vcl
sub vcl_hash {
    hash_data(req.url);
    
    # 只在必要时加入Host
    if (req.http.Host) {
        hash_data(req.http.Host);
    }
    
    # 避免不必要的区分
    # 不要加入变化的值如时间戳
    
    return (lookup);
}

八、监控与分析 #

8.1 性能统计 #

bash
# 查看关键指标
varnishstat -1 -f MAIN.cache_hit -f MAIN.cache_miss -f MAIN.client_req

# 查看线程状态
varnishstat -1 -f MAIN.threads -f MAIN.threads_limited -f MAIN.thread_queue_len

# 查看内存使用
varnishstat -1 -f MAIN.s0.*

8.2 性能分析脚本 #

bash
#!/bin/bash
# varnish_perf.sh

echo "=== Varnish Performance Report ==="
echo "Time: $(date)"
echo ""

# 缓存命中率
HITS=$(varnishstat -1 -f MAIN.cache_hit | awk '{print $2}')
MISSES=$(varnishstat -1 -f MAIN.cache_miss | awk '{print $2}')
TOTAL=$((HITS + MISSES))
if [ $TOTAL -gt 0 ]; then
    RATE=$(echo "scale=2; $HITS * 100 / $TOTAL" | bc)
    echo "Cache Hit Rate: ${RATE}%"
fi
echo ""

# 请求统计
echo "Request Statistics:"
varnishstat -1 -f MAIN.client_req -f MAIN.cache_hit -f MAIN.cache_miss
echo ""

# 线程状态
echo "Thread Statistics:"
varnishstat -1 -f MAIN.threads -f MAIN.threads_limited -f MAIN.threads_created
echo ""

# 后端统计
echo "Backend Statistics:"
varnishstat -1 -f MAIN.backend_conn -f MAIN.backend_unhealthy -f MAIN.backend_fail
echo ""

# 内存统计
echo "Memory Statistics:"
varnishstat -1 -f MAIN.s0.g_bytes -f MAIN.s0.g_space
echo ""

# 对象统计
echo "Object Statistics:"
varnishstat -1 -f MAIN.n_object -f MAIN.n_expired -f MAIN.n_lru_nuked

8.3 性能瓶颈分析 #

bash
# 查看慢请求
varnishlog -q "Timestamp:Process[2] > 1.0"

# 查看后端延迟
varnishlog -i Timestamp -i BereqURL | grep -A1 "BereqURL"

# 查看错误请求
varnishlog -q "RespStatus >= 500"

九、性能测试 #

9.1 使用http_load #

bash
# 安装
sudo apt install http_load

# 测试
http_load -parallel 100 -seconds 60 urls.txt

9.2 使用wrk #

bash
# 安装
sudo apt install wrk

# 测试
wrk -t12 -c400 -d30s http://localhost:6081/

9.3 使用ab #

bash
# 安装
sudo apt install apache2-utils

# 测试
ab -n 100000 -c 100 http://localhost:6081/

十、完整配置示例 #

10.1 启动参数 #

bash
# /etc/default/varnish 或 systemd service

DAEMON_OPTS="-a :6081 \
    -T localhost:6082 \
    -f /etc/varnish/default.vcl \
    -S /etc/varnish/secret \
    -s malloc,4G \
    -p thread_pools=4 \
    -p thread_pool_min=200 \
    -p thread_pool_max=5000 \
    -p thread_pool_timeout=300 \
    -p workspace_client=128k \
    -p workspace_backend=128k \
    -p http_req_size=64k \
    -p http_resp_size=64k \
    -p connect_timeout=5 \
    -p first_byte_timeout=90 \
    -p between_bytes_timeout=30 \
    -p timeout_idle=5 \
    -p default_ttl=300 \
    -p default_grace=3600"

10.2 VCL配置 #

vcl
vcl 4.1;

import std;

backend default {
    .host = "127.0.0.1";
    .port = "8080";
    .connect_timeout = 5s;
    .first_byte_timeout = 90s;
    .between_bytes_timeout = 30s;
    .max_connections = 500;
}

sub vcl_recv {
    # 静态资源
    if (req.url ~ "\.(css|js|png|gif|jpg|jpeg|ico|svg|woff|woff2)$") {
        unset req.http.Cookie;
        return (hash);
    }
    
    return (hash);
}

sub vcl_backend_response {
    # 静态资源
    if (bereq.url ~ "\.(css|js|png|gif|jpg|jpeg|ico|svg|woff|woff2)$") {
        set beresp.ttl = 30d;
        unset beresp.http.Set-Cookie;
    }
    # HTML
    elseif (beresp.http.Content-Type ~ "text/html") {
        set beresp.ttl = 5m;
    }
    # 默认
    else {
        set beresp.ttl = 5m;
    }
    
    # Grace
    set beresp.grace = 1h;
}

sub vcl_hit {
    if (obj.ttl >= 0s) {
        return (deliver);
    }
    if (obj.ttl + obj.grace > 0s) {
        return (deliver);
    }
    return (restart);
}

sub vcl_deliver {
    if (obj.hits > 0) {
        set resp.http.X-Cache = "HIT";
    } else {
        set resp.http.X-Cache = "MISS";
    }
}

十一、总结 #

本章我们学习了:

  1. 性能调优概述:指标、方向
  2. 存储配置:malloc、file、选择建议
  3. 线程池配置:参数、调优建议
  4. 内存配置:工作区、大请求处理
  5. 超时配置:连接超时、客户端超时
  6. 内核参数优化:网络参数、应用参数
  7. 缓存优化:TTL、Grace、缓存键
  8. 监控与分析:统计、脚本、瓶颈分析
  9. 性能测试:http_load、wrk、ab

掌握性能调优后,让我们进入下一章,学习VMOD扩展!

最后更新:2026-03-28