因为您不仅花费时间在 Redis 执行命令上,还花费时间传输数据(发送命令、接收结果)。在单线程模式下,当您传输时,Redis 不起作用。当 Redis 工作时,不会发生任何传输。多个连接或一个管道连接可帮助您使带宽和 CPU 周期饱和。
luttece 不仅仅关乎速度。它还可以帮助您使用异步和反应式 API 更好地组织代码。
回到性能主题,here是一个简单的基准测试,可让您大致了解线程和池的影响。请注意,虽然池化速度有点慢(您在池操作上花费一些时间),但它允许您隔离操作(因此错误不会影响其他线程)并使用MULTI
和阻止命令。
这是我的结果(本地系统有 4 个核心,远程系统 CPU 大约慢 2 倍):
线程=1
Benchmark (address) Mode Cnt Score Error Units
LettuceThreads.pooled socket thrpt 25 35389.995 ± 1325.198 ops/s
LettuceThreads.pooled localhost thrpt 25 32075.870 ± 416.220 ops/s
LettuceThreads.pooled remote thrpt 25 3883.193 ± 67.622 ops/s
LettuceThreads.shared socket thrpt 25 39419.772 ± 1966.023 ops/s
LettuceThreads.shared localhost thrpt 25 34293.245 ± 1737.349 ops/s
LettuceThreads.shared remote thrpt 25 3919.251 ± 49.897 ops/s
线程=2
Benchmark (address) Mode Cnt Score Error Units
LettuceThreads.pooled socket thrpt 25 56938.187 ± 2727.772 ops/s
LettuceThreads.pooled localhost thrpt 25 49420.748 ± 2091.631 ops/s
LettuceThreads.pooled remote thrpt 25 7791.706 ± 133.507 ops/s
LettuceThreads.shared socket thrpt 25 81195.900 ± 1593.424 ops/s
LettuceThreads.shared localhost thrpt 25 78404.688 ± 3878.044 ops/s
LettuceThreads.shared remote thrpt 25 3992.023 ± 39.092 ops/s
线程=4
Benchmark (address) Mode Cnt Score Error Units
LettuceThreads.pooled socket thrpt 25 87345.126 ± 8149.009 ops/s
LettuceThreads.pooled localhost thrpt 25 75003.031 ± 4481.289 ops/s
LettuceThreads.pooled remote thrpt 25 15807.410 ± 225.376 ops/s
LettuceThreads.shared socket thrpt 25 169112.489 ± 3749.897 ops/s
LettuceThreads.shared localhost thrpt 25 115464.778 ± 5099.728 ops/s
LettuceThreads.shared remote thrpt 25 7952.492 ± 133.521 ops/s
您可以在这里看到,性能随着线程数量的增加而变化得非常好,因此生菜并非毫无用处。