shaoqin 发表于 2018-6-20 08:52:32

WINDOWS 下, Redis 单机事务测试

package one;  

  
import java.text.NumberFormat;
  
import java.util.concurrent.CountDownLatch;
  

  
import redis.clients.jedis.Jedis;
  
import redis.clients.jedis.Transaction;
  

  
public class TransactionContentionTest extends ConnectionBuilder {
  
    private static final int CONTENTION_LEVEL = 5;
  
    private static final int TOTAL = 20000;
  
    private static final NumberFormat nf = NumberFormat.getPercentInstance();
  

  
    public static void main(String[] args) throws Exception {
  
      nf.setMinimumFractionDigits(2);
  
      buildPool();
  
      CountDownLatch latch = new CountDownLatch(CONTENTION_LEVEL);
  
      Thread[] threads = new Thread;
  
      ContentionClient[] clients = new ContentionClient;
  
      Jedis jedis = pool.getResource();
  
      jedis.set("TestCounter", "0");
  
      jedis.close();
  
      for (int i = 0; i < CONTENTION_LEVEL; i++) {
  
            ContentionClient client = new ContentionClient();
  
            client.setTotal(TOTAL);
  
            client.setCounterName("TestCounter");
  
            client.setJedis(pool.getResource());
  
            client.setLatch(latch);
  
            clients = client;
  
            threads = new Thread(client);
  
      }
  
      long start = System.currentTimeMillis();
  
      for (int i = 0; i < CONTENTION_LEVEL; i++) {
  
            threads.start();
  
      }
  
      latch.await();
  
      long end = System.currentTimeMillis();
  
      System.out.println("Elapse:" + (end - start) + " ms");
  
      for (int i = 0; i < CONTENTION_LEVEL; i++) {
  
            Double failRate = (double) clients.getFailCount() / TOTAL;
  
            System.out.println(i + " Fail Rate:" + nf.format(failRate));
  
            clients.getJedis().close();
  
      }
  
      close();
  
    }
  

  
    static class ContentionClient implements Runnable {
  
      private Jedis jedis;
  
      private String counterName;
  
      private int total;
  
      private long failCount = 0;
  

  
      public CountDownLatch getLatch() {
  
            return latch;
  
      }
  

  
      public void setLatch(CountDownLatch latch) {
  
            this.latch = latch;
  
      }
  

  
      private CountDownLatch latch;
  

  
      public Jedis getJedis() {
  
            return jedis;
  
      }
  

  
      public void setJedis(Jedis jedis) {
  
            this.jedis = jedis;
  
      }
  

  
      public String getCounterName() {
  
            return counterName;
  
      }
  

  
      public void setCounterName(String counterName) {
  
            this.counterName = counterName;
  
      }
  

  
      public int getTotal() {
  
            return total;
  
      }
  

  
      public void setTotal(int total) {
  
            this.total = total;
  
      }
  

  
      public long getFailCount() {
  
            return failCount;
  
      }
  

  
      public void setFailCount(long failCount) {
  
            this.failCount = failCount;
  
      }
  

  
      @Override
  
      public void run() {
  
            while (total > 0) {
  
                jedis.watch(counterName);
  
                Integer counter = Integer.parseInt(jedis.get(counterName));
  
                Transaction tx = jedis.multi();
  
                counter++;
  
                tx.set(counterName, counter.toString());
  
                if (tx.exec() == null) {
  
                  jedis.unwatch();
  
                  failCount++;
  
                } else {
  
                  total--;
  
                }
  
            }
  
            latch.countDown();
  
      }
  
    }
  
}

zhangxiajun 发表于 2018-6-20 08:57:13

谢谢分享
页: [1]
查看完整版本: WINDOWS 下, Redis 单机事务测试