Lettuce和Jedis的基準測試

原文連接:https://www.dubby.cn/detail.html?id=9108html

1.準備工做

本地須要安裝Redis,使用JMH作基準測試的框架:java

<dependency>
    <groupId>org.openjdk.jmh</groupId>
    <artifactId>jmh-core</artifactId>
    <version>1.21</version>
</dependency>
<dependency>
    <groupId>org.openjdk.jmh</groupId>
    <artifactId>jmh-generator-annprocess</artifactId>
    <version>1.21</version>
    <scope>provided</scope>
</dependency>
複製代碼

項目添加Jedis和Lettuce的依賴:redis

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>
<dependency>
    <groupId>io.lettuce</groupId>
    <artifactId>lettuce-core</artifactId>
    <version>5.0.3.RELEASE</version>
</dependency>
複製代碼

若是你對JMH不是很熟悉,建議你去看看Code Tools: jmh,和samples 安全

2.編寫測試代碼

2.1 Jedis

@BenchmarkMode(Mode.Throughput)
@Warmup(iterations = 1)
@Threads(100)
@State(Scope.Thread)
@Measurement(iterations = 2, time = 600, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class JedisStudy {
    private static final int LOOP = 1;
    private Jedis jedis;
    @Setup
    public void setup() {
        jedis = new Jedis("127.0.0.1", 6379);
    }
    @Benchmark
    public void get() {
        for (int i = 0; i < LOOP; ++i) {
            jedis.get("a");
        }
    }
}
複製代碼
public static void main(String[] args) throws RunnerException {
    Options options = new OptionsBuilder().include(JedisStudy.class.getSimpleName())
            .output("benchmark/jedis-Throughput.log").forks(1).build();
    new Runner(options).run();
}
複製代碼

這裏用到的註解,其中@OutputTimeUnit(TimeUnit.MILLISECONDS)很容易理解,就是測試結果的單位,@Threads(100)是開啓多少個線程測試;@Warmup(iterations = 1)是預熱的循環次數;@BenchmarkMode(Mode.Throughput)是測試的模式,能夠測試吞吐,延時等;@Measurement(iterations = 2, time = 600, timeUnit = TimeUnit.MILLISECONDS)是測試的循環次數,以及時長;其中比較難以理解的就是@State(Scope.Thread),這裏我簡單描述下吧,@State(Scope.Thread)和下面的@Setup配合使用,意思是每一個測試線程,都會使用獨立的一個變量,這個變量就是Jedis jedis,使用@Setup所修飾的方法來作初始化。bash

參考JMHSample_03_States JMHSample_04_DefaultState 併發

其中由於Jedis是線程不安全的,因此每一個線程使用的都是一個單獨的Jedis對象,這裏能夠使用Pool來優化,讀者若是感興趣,能夠嘗試。框架

2.2 Lettuce

@BenchmarkMode(Mode.Throughput)
@Warmup(iterations = 1)
@Threads(100)
@State(Scope.Benchmark)
@Measurement(iterations = 2, time = 600, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class LettuceAsyncStudy {
    private static final int LOOP = 1;
    private StatefulRedisConnection<String, String> connection;
    @Setup
    public void setup() {
        RedisClient client = RedisClient.create("redis://localhost");
        connection = client.connect();
    }
    @Benchmark
    public void get() throws ExecutionException, InterruptedException {
        RedisAsyncCommands<String, String> commands = connection.async();
        List<RedisFuture<String>> redisFutureList = new ArrayList<>();
        for (int i = 0; i < LOOP; ++i) {
            RedisFuture<String> future = commands.get("a");
            redisFutureList.add(future);
            future.get();
        }
        redisFutureList.forEach(f -> {
            try {
                f.get();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}
複製代碼
public static void main(String[] args) throws RunnerException {
    Options options = new OptionsBuilder().include(LettuceAsyncStudy.class.getSimpleName())
            .output("benchmark/lettuceAsync-Throughput.log").forks(1).build();
    new Runner(options).run();
}
複製代碼

這裏和上面Jedis的區別就是@State(Scope.Benchmark),其實就是StatefulRedisConnection<String, String> connection這個對象是全部測試線程共享的,由於Lettuce的StatefulRedisConnection是線程的安全的,因此能夠這麼用。async

3.測試結果

Client 線程數(併發數) 每次測試方法裏循環執行get的次數 Throughput(ops/ms)
Jedis 100 1 46.628
Lettuce 100 1 106.589
-- -- -- --
Jedis 100 10 5.307
Lettuce 100 10 14.802
-- -- -- --
Jedis 100 100 0.483
Lettuce 100 100 1.599
相關文章
相關標籤/搜索