-
-
Save opit/5900187 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import redis.clients.jedis.Jedis; | |
import redis.clients.jedis.Transaction; | |
import redis.clients.jedis.Pipeline; | |
import java.util.List; | |
public class JedisBench{ | |
private Jedis jedis; | |
long start, end; | |
public JedisBench(final String host) { | |
jedis = new Jedis(host); | |
jedis.connect(); | |
} | |
public void pipelinedVsSimpleTransactions() { | |
int[] runs = new int[] {1,100,100,100,5000,10000,10000,10000,15000,15000,15000,30000,30000}; | |
long sim, simtx, pl, pltx; | |
for (int i = 0; i < runs.length; i++){ | |
// sim = runSimpleSet(runs[i]); | |
// cleanup(runs[i]); | |
simtx = runTransactionSet(runs[i]); | |
cleanup(runs[i]); | |
pl = runPipelinedSet(runs[i]);; | |
cleanup(runs[i]); | |
pltx = runPipelinedTxSet(runs[i]);;; | |
cleanup(runs[i]); | |
// System.out.println(runs[i] + " sim:\t" + sim + " simtx:\t" + simtx + " pl:\t" + pl + " pltx:\t" + pltx + " (all in ms)"); | |
System.out.println(runs[i] + " simtx:\t" + simtx + " pl:\t" + pl + " pltx:\t" + pltx + " (all in ms)"); | |
} | |
} | |
private void cleanup(int n){ | |
Pipeline pipeline = jedis.pipelined(); | |
for (int i = 0; i < n; i++) { | |
pipeline.del(""+i); | |
} | |
List<Object> pipelineResult = pipeline.syncAndReturnAll(); | |
} | |
private long runSimpleSet(int n) { | |
start = System.currentTimeMillis(); | |
for (int i = 0; i < n; i++) { | |
String result = jedis.set("" + i, "" + i); | |
} | |
end = System.currentTimeMillis(); | |
return (end - start); | |
} | |
private long runTransactionSet(int n) { | |
start = System.currentTimeMillis(); | |
Transaction tx = jedis.multi(); | |
for (int i = 0; i < n; i++) { | |
tx.set("" + i, "" + i); | |
} | |
List<Object> txResult = tx.exec(); | |
end = System.currentTimeMillis(); | |
return (end - start); | |
} | |
private long runPipelinedSet(int n) { | |
start = System.currentTimeMillis(); | |
Pipeline pipeline = jedis.pipelined(); | |
start = System.currentTimeMillis(); | |
for (int i = 0; i < n; i++) { | |
pipeline.set("" + i, "" + i); | |
} | |
List<Object> results = pipeline.syncAndReturnAll(); | |
end = System.currentTimeMillis(); | |
return (end - start); | |
} | |
private long runPipelinedTxSet(int n) { | |
start = System.currentTimeMillis(); | |
Pipeline pipeline = jedis.pipelined(); | |
pipeline.multi(); | |
for (int i = 0; i < n; i++) { | |
pipeline.set("" + i, "" + i); | |
} | |
pipeline.exec(); | |
List<Object> pipelineResult = pipeline.syncAndReturnAll(); | |
end = System.currentTimeMillis(); | |
return (end - start); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment