为什么Jedis比Spring RedisTemplate快?

时间:2018-07-31 10:27:59

标签: java spring optimization jedis

config:

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxIdle" value="300"/>  
    <property name="maxTotal" value="60000"/>  
    <property name="testOnBorrow" value="true"/>  
</bean>



<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <property name="poolConfig" ref="jedisPoolConfig"></property>
    <property name="hostName" value="${redis.host}"></property>
    <property name="port" value="${redis.port}"></property>
    <property name="timeout" value="${redis.timeout}"></property>
    <property name="password" value="${redis.password}"></property>
    <property name="database" value="${redis.database.business}"></property>
</bean>

<bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
    <property name="keySerializer" ref="keySerializer"></property>
    <property name="connectionFactory" ref="jedisConnectionFactory"></property>
 </bean>

测试用例

    @Benchmark
public void jedis(TestBenchmark state) {

    for (int i = 0; i < 1000; i++) {
        Jedis jedis = jedisPool.getResource();
        jedis.hset("HA", "FA" + i, "VV" + i);
        jedis.close();
    }
    for (int i = 0; i < 1000; i++) {
        Jedis jedis = jedisPool.getResource();

        jedis.hget("HA", "FA" + i);
        jedis.close();
    }

}



 @Benchmark
public void springTemplate(TestBenchmark state) {



    for (int i = 0; i < 1000; i++) {
        template.opsForHash().put("HA", "FA" + i, "VV" + i);
    }
    for (int i = 0; i < 1000; i++) {
        template.opsForHash().get("HA", "FA" + i);
    }

}

结果:

# Run complete. Total time: 00:04:45

Benchmark                     Mode  Cnt        Score        Error  Units
TestBenchmark.jedis           avgt   20  4063712.941 ± 774497.669  us/op
TestBenchmark.springTemplate  avgt   20  8127876.567 ± 689718.613  us/op

是否有可能通过jedis来创建更多java对象,或者还有其他原因?

填充 jedis:4063712.941±(99.9%)774497.669 us / op [平均](最小,平均,最大)=(3392560.609,4063712.941,7306286.674),stdev = 891912.667 CI(99.9%) :[3289215.273,4838210.610](假设正态分布)弹簧模板8127876.567±(99.9%)689718.613 us / op [平均值](最小,平均,最大值)=(7080667.847,8127876.567,9678719.917),stdev = 794280.981 CI(99.9% ):[7438157.954,8817595.181](假设正态分布)

0 个答案:

没有答案