Created
December 19, 2009 18:22
-
-
Save pcdinh/260182 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 java.util.Random; | |
import org.jredis.JRedis; | |
import org.jredis.RedisException; | |
import org.jredis.ri.alphazero.JRedisClient; | |
public class Redis { | |
private static final int THREAD_COUNT = 2; | |
private static final int SETS_PER_THREAD = 100; | |
private static final int RANDOM_GETS_PER_THREAD = 500; | |
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, InterruptedException { | |
benchmarkSets(); | |
benchmarkGets(); | |
} | |
/** | |
* Benchmark the desired number of sets across the specified number of threads. | |
* | |
* @throws InterruptedException | |
*/ | |
private static void benchmarkSets() throws InterruptedException { | |
// -- Create the threads to do the writing... | |
// | |
Thread[] writeThreads = new WriteThread[THREAD_COUNT]; | |
for (int i = 0; i < THREAD_COUNT; i++) { | |
writeThreads[i] = new WriteThread(Integer.toString(i)); | |
} | |
// -- Start all the threads... | |
// | |
for (int i = 0; i < THREAD_COUNT; i++) { | |
writeThreads[i].start(); | |
} | |
// -- Wait for all the threads to finish... | |
// | |
long writeStartTime = System.currentTimeMillis(); | |
for (int i = 0; i < THREAD_COUNT; i++) { | |
writeThreads[i].join(); | |
} | |
long writeEndTime = System.currentTimeMillis(); | |
System.out.println("Writes finished after " + (writeEndTime - writeStartTime) + " ms"); | |
} | |
/** | |
* Benchmark the desired number of gets across the specified number of threads. | |
* | |
* @throws InterruptedException | |
*/ | |
private static void benchmarkGets() throws InterruptedException { | |
// -- Create the threads to do the GETTING... | |
// | |
Thread[] getThreads = new GetThread[THREAD_COUNT]; | |
for (int i = 0; i < THREAD_COUNT; i++) { | |
getThreads[i] = new GetThread(Integer.toString(i)); | |
} | |
// -- Start all the threads... | |
// | |
for (int i = 0; i < THREAD_COUNT; i++) { | |
getThreads[i].start(); | |
} | |
// -- Wait for all the threads to finish... | |
// | |
long getStartTime = System.currentTimeMillis(); | |
for (int i = 0; i < THREAD_COUNT; i++) { | |
getThreads[i].join(); | |
} | |
long getEndTime = System.currentTimeMillis(); | |
System.out.println("Gets finished after " + (getEndTime - getStartTime) + " ms"); | |
} | |
/** | |
* Thread that handles the writing of X items to the database. | |
*/ | |
private static class WriteThread extends Thread { | |
private JRedis jredis = null; | |
private final String keyPrefix; | |
public WriteThread(String keyPrefix) { | |
this.keyPrefix = keyPrefix; | |
// -- Connect to the Redis server... | |
// | |
try { | |
jredis = new JRedisClient("localhost", 6379); | |
System.out.println("Redis connection established"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* @see java.lang.Thread#run() | |
*/ | |
@Override | |
public void run() { | |
try { | |
// Perform the desired number of sets | |
for (int i = 0; i < SETS_PER_THREAD; i++) { | |
jredis.set(keyPrefix + i, Integer.toString(i)); | |
} | |
// Close the connection | |
jredis.quit(); | |
} catch (RedisException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
/** | |
* Thread that handles the getting of X random items from Redis. | |
*/ | |
private static class GetThread extends Thread { | |
private JRedis jredis = null; | |
private final String keyPrefix; | |
public GetThread(String keyPrefix) { | |
this.keyPrefix = keyPrefix; | |
// -- Connect to the Redis server... | |
// | |
try { | |
jredis = new JRedisClient("localhost", 6379); | |
System.out.println("Redis connection established"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* @see java.lang.Thread#run() | |
*/ | |
@Override | |
public void run() { | |
try { | |
Random rand = new Random(); | |
// Perform the desired number of gets and verify the value is correct | |
for (int i = 0; i < RANDOM_GETS_PER_THREAD; i++) { | |
int keyNum = rand.nextInt(SETS_PER_THREAD); | |
String value = new String(jredis.get(keyPrefix + keyNum)); | |
assert(value.equals(Integer.toString(keyNum))); | |
} | |
// Close the connection | |
jredis.quit(); | |
} catch (RedisException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment