WIN 10:i5 4440, 8GB
Test the transaction speed of Redis
The Redis watch mechanism implementation transaction is an optimistic locking form (which should be a spin lock). For scenarios where concurrency is not high, you can conserve CPU resources (lightweight locks). However, when the competition is intense, the probability of failure is greatly increased, and the retry after a large number of failures consumes CPU resources.
The logic of the test
1) Read counter
2) Java-side self-increment
3) set back to Redis
The Redis incr function is not available here, the Redis server side is single-threaded execution, so the server side only executes a incr instruction, in fact, can be regarded as atomic operation, the server side directly executes 1-2 seconds, of course, depending on the computer.
The counter is calculated until 100,000. (The number of threads increases, so the load calculation per thread is reduced)
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[CONTENTION_LEVEL]; contentionclient[] clients = new contentionclient[contention _level]; 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"); &nbsP; client.setjedis (Pool.getresource ()); client.setlatch (latch); clients[i] = client; threads[i] = new thread (client); } long start = system.currenttimemillis (); for (Int i = 0; i < contention_level ; i++) { threads[i].start (); } latch.await () ; long end = system.currenttimemillis (); &nbSp; system.out.println ("Elapse:" + (End - start) + " ms"); for (int i = 0; i < contention_level; i++) { double failrate = (Double) clients[i].getfailcount () / TOTAL; system.out.println (i + " Fail Rate:" + nf.format (failrate)); Clients[i].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 (); } }}
Test multiple sets of data separately
Number of threads
|
1
|
2
|
4
|
8
|
16
|
Retry rate
|
0
|
67% |
185%
|
435%
|
900%
|
Take
|
21055ms
|
19312
|
21136 |
34231
|
65964
|
With optimistic locking mechanism concurrency is slightly higher, the resource consumption is very serious.
After all, the use of Windows and home machines, testing data, no reference value.
This article from "Tech Doc" blog, declined reprint!
Redis stand-alone transaction test under WINDOWS