当前位置:Gxlcms > mysql > 多种方式测试redis入库性能

多种方式测试redis入库性能

时间:2021-07-01 10:21:17 帮助过:40人阅读

? ? Transaction Pipeline 耗时(s) 测试一: N N 311 测试二: N Y 10 测试三: Y Y 7 测试四: Y N 10 ? 结论:Pipeline + Transaction方式是几种插入方式中性能最好的。虽然有点意外,但测试多次仍然是这个结论。 ? 测试代码如下: ? import org.apache.

?

? Transaction Pipeline 耗时(s)
测试一: N N 311
测试二: N Y 10
测试三: Y Y 7
测试四: Y N 10

?

结论:Pipeline + Transaction方式是几种插入方式中性能最好的。虽然有点意外,但测试多次仍然是这个结论。

?

测试代码如下:

?

import org.apache.commons.lang.RandomStringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Transaction;
public class RedisTransactionTest {
	private static String host = "xx.xx.xx.xx";
	private static JedisPool pool = new JedisPool(new JedisPoolConfig(), host);
	private long rowCount = 1000000; // 100万
	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		new RedisTransactionTest().noTransactionNoPipeline();
		System.out.println("noTransactionNoPipeline use " + (System.currentTimeMillis() - start) / 1000);
		pool = new JedisPool(new JedisPoolConfig(), host);
		start = System.currentTimeMillis();
		new RedisTransactionTest().pipelineWithoutTransaction();
		System.out.println("pipelineWithoutTransaction use " + (System.currentTimeMillis() - start) / 1000);
		pool = new JedisPool(new JedisPoolConfig(), host);
		start = System.currentTimeMillis();
		new RedisTransactionTest().pipelineWithTransaction();
		System.out.println("pipelineWithTransaction use " + (System.currentTimeMillis() - start) / 1000);
		pool = new JedisPool(new JedisPoolConfig(), host);
		start = System.currentTimeMillis();
		new RedisTransactionTest().transactionNoPipeline();
		System.out.println("transactionNoPipeline use " + (System.currentTimeMillis() - start) / 1000);
	}
	public void pipelineWithoutTransaction() {
		Jedis jedis = pool.getResource();
		try {
			Pipeline p = jedis.pipelined();
			for (int i = 0; i < rowCount; i++) {
				String key = RandomStringUtils.randomAlphabetic(8);
				p.set(key, RandomStringUtils.randomNumeric(5));
				p.expire(key, 5 * 60);
			}
			p.sync();
		} catch (Exception e) {
			pool.returnResource(jedis);
		}
	}
	public void pipelineWithTransaction() {
		Jedis jedis = pool.getResource();
		try {
			Pipeline p = jedis.pipelined();
			p.multi();
			for (int i = 0; i < rowCount; i++) {
				String key = RandomStringUtils.randomAlphabetic(8);
				p.set(key, RandomStringUtils.randomNumeric(5));
				p.expire(key, 5 * 60);
			}
			p.exec();
			p.sync();
		} catch (Exception e) {
			pool.returnResource(jedis);
		}
	}
	public void noTransactionNoPipeline() {
		Jedis jedis = pool.getResource();
		try {
			for (int i = 0; i < rowCount; i++) {
				String key = RandomStringUtils.randomAlphabetic(8);
				jedis.set(key, RandomStringUtils.randomNumeric(5));
				jedis.expire(key, 5 * 60);
			}
		} catch (Exception e) {
			pool.returnResource(jedis);
		}
	}
	public void transactionNoPipeline() {
		Jedis jedis = pool.getResource();
		try {
			Transaction tx = jedis.multi();
			for (int i = 0; i < rowCount; i++) {
				String key = RandomStringUtils.randomAlphabetic(8);
				tx.set(key, RandomStringUtils.randomNumeric(5));
				tx.expire(key, 5 * 60);
			}
			tx.exec();
		} catch (Exception e) {
			pool.returnResource(jedis);
		}
	}
}

?--end

?

?



已有 0 人发表留言,猛击->> 这里<<-参与讨论


ITeye推荐
  • —软件人才免语言低担保 赴美带薪读研!—



人气教程排行