首页 文章

如何使用Spring Data Redis和本地Redis服务器找到性能瓶颈

提问于
浏览
2

我正在尝试优化从Redis获取数据的性能 . 服务器当前在我的2015 Macbook Pro上本地运行 .

First: Problem explanation 目前我只有32个密钥存储为哈希 . 其中16个在每个哈希值中存储了相当长的JSON字符串,每个哈希值中包含<300个字段 . 剩下的很小,所以我对它们没有任何问题 .

在Spring Boot应用程序中,使用Spring Data Redis模板和Jedis连接,通过将4个HGETALL命令4次流水线操作,检索16个大哈希值的总时间约为1700ms .

My question: 如何找到真正的瓶颈?我已经检查了SLOWLOG,它告诉我在服务器上执行的操作非常快,每个HGETALL命令<1ms,这是预期的 . 这意味着瓶颈必须在Java应用程序和Redis服务器之间 . 是否有可能延迟是其他〜1650ms的原因? (延迟似乎不是其他较小哈希的问题 . )或者它可能是我的JSON字符串的反序列化?我不知道如何测试这个,因为我无法在RedisTemplate代码中加入计时器 .

下面是我用来管道HGETALL命令的代码:

private Map<Date, Map<Integer, Departure>> pipelineMethod(List<String> keys, String keyspace) {

    long pipeTime = System.currentTimeMillis();
    List<Object> results = redisTemplate.executePipelined(
            (RedisCallback) (connection) -> {
                for (String key : keys) {
                    long actionTime = System.currentTimeMillis();
                    connection.hGetAll((keyspace + key).getBytes());
                    System.out.println("HGETALL finished in " + (System.currentTimeMillis()-actionTime) + "ms");
                }

                return null;
            }
    );
    System.out.println("Pipeline finished in " + (System.currentTimeMillis()-pipeTime) + "ms");

    Map<Date, Map<Integer, Departure>> resultMap = new ConcurrentHashMap<>();
    for (int i = 0; i < keys.size(); i++) {
        if (results.get(i) == null) {
            resultMap.put(new Date(Long.parseLong(keys.get(i))), null);
            log.debug("Hash map from redis on " + new Date(Long.parseLong(keys.get(i))) + " was null on retrieval");
        }
        else
            resultMap.put(new Date(Long.parseLong(keys.get(i))), (Map<Integer, Departure>) results.get(i));
    }

    return resultMap;
}

任何建议将不胜感激 .

1 回答

  • 0

    尝试使用连接池的生菜客户端

    private GenericObjectPoolConfig getPoolConfig() {
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        //All below should use the propertysources;
        poolConfig.setMaxTotal(20);
        poolConfig.setMaxIdle(20);
        poolConfig.setMinIdle(0);
        return poolConfig;
      }      
         @Bean
          @Primary
          public RedisConnectionFactory redisConnectionFactory() {
            DefaultLettucePool lettucePool = new DefaultLettucePool(redisHost, Integer.valueOf(redisPort).intValue(), getPoolConfig());
            lettucePool.setPassword(redisPass);
            lettucePool.afterPropertiesSet();
            LettuceConnectionFactory clientConfig = new LettuceConnectionFactory(lettucePool);
            clientConfig.afterPropertiesSet();
            return clientConfig;
          }
    
          @Bean
          public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory connectionFactory) {
            RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
            template.setConnectionFactory(connectionFactory);
            return template;
          }
    

相关问题