pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Java pastebin - collaborative debugging tool View Help


Posted by Colin Howe on Mon 27 Apr 18:09
report abuse | download | new post

  1. import java.util.Random;
  2.  
  3. import org.jredis.JRedis;
  4. import org.jredis.RedisException;
  5. import org.jredis.ri.alphazero.JRedisClient;
  6.  
  7. public class Redis {
  8.   private static final int THREAD_COUNT = 2;
  9.   private static final int SETS_PER_THREAD = 100;
  10.   private static final int RANDOM_GETS_PER_THREAD = 500;
  11.  
  12.     benchmarkSets();
  13.     benchmarkGets();
  14.   }
  15.  
  16.  
  17.   /**
  18.    * Benchmark the desired number of sets across the specified number of threads.
  19.    *
  20.    * @throws InterruptedException
  21.    */
  22.   private static void benchmarkSets() throws InterruptedException {
  23.  
  24.     // -- Create the threads to do the writing...
  25.     //
  26.     Thread[] writeThreads = new WriteThread[THREAD_COUNT];
  27.     for (int i = 0; i < THREAD_COUNT; i++) {
  28.       writeThreads[i] = new WriteThread(Integer.toString(i));
  29.     }
  30.    
  31.     // -- Start all the threads...
  32.     //
  33.     for (int i = 0; i < THREAD_COUNT; i++) {
  34.       writeThreads[i].start();
  35.     }
  36.    
  37.     // -- Wait for all the threads to finish...
  38.     //
  39.     long writeStartTime = System.currentTimeMillis();
  40.     for (int i = 0; i < THREAD_COUNT; i++) {
  41.       writeThreads[i].join();
  42.     }
  43.     long writeEndTime = System.currentTimeMillis();
  44.    
  45.     System.out.println("Writes finished after " + (writeEndTime - writeStartTime) + " ms");
  46.    
  47.   }
  48.  
  49.  
  50.   /**
  51.    * Benchmark the desired number of gets across the specified number of threads.
  52.    *
  53.    * @throws InterruptedException
  54.    */
  55.   private static void benchmarkGets() throws InterruptedException {
  56.     // -- Create the threads to do the GETTING...
  57.     //
  58.     Thread[] getThreads = new GetThread[THREAD_COUNT];
  59.     for (int i = 0; i < THREAD_COUNT; i++) {
  60.       getThreads[i] = new GetThread(Integer.toString(i));
  61.     }
  62.    
  63.     // -- Start all the threads...
  64.     //
  65.     for (int i = 0; i < THREAD_COUNT; i++) {
  66.       getThreads[i].start();
  67.     }
  68.    
  69.     // -- Wait for all the threads to finish...
  70.     //
  71.     long getStartTime = System.currentTimeMillis();
  72.     for (int i = 0; i < THREAD_COUNT; i++) {
  73.       getThreads[i].join();
  74.     }
  75.     long getEndTime = System.currentTimeMillis();
  76.    
  77.     System.out.println("Gets finished after " + (getEndTime - getStartTime) + " ms");
  78.   }
  79.  
  80.  
  81.   /**
  82.    * Thread that handles the writing of X items to the database.
  83.    */
  84.   private static class WriteThread extends Thread {
  85.     private JRedis jredis = null;
  86.     private final String keyPrefix;
  87.    
  88.     public WriteThread(String keyPrefix) {
  89.       this.keyPrefix = keyPrefix;
  90.      
  91.       // -- Connect to the Redis server...
  92.       //
  93.       try {
  94.         jredis = new JRedisClient("localhost", 6379);
  95.         System.out.println("Redis connection established");
  96.       } catch (Exception e) {
  97.         e.printStackTrace();
  98.       }
  99.     }
  100.    
  101.    
  102.     /**
  103.      * @see java.lang.Thread#run()
  104.      */
  105.     @Override
  106.     public void run() {
  107.       try {
  108.         // Perform the desired number of sets
  109.         for (int i = 0; i < SETS_PER_THREAD; i++) {
  110.           jredis.set(keyPrefix + i, Integer.toString(i));
  111.         }
  112.        
  113.         // Close the connection
  114.         jredis.quit();
  115.       } catch (RedisException e) {
  116.         e.printStackTrace();
  117.       }
  118.     }
  119.   }
  120.  
  121.  
  122.   /**
  123.    * Thread that handles the getting of X random items from Redis.
  124.    */
  125.   private static class GetThread extends Thread {
  126.     private JRedis jredis = null;
  127.     private final String keyPrefix;
  128.    
  129.     public GetThread(String keyPrefix) {
  130.       this.keyPrefix = keyPrefix;
  131.      
  132.       // -- Connect to the Redis server...
  133.       //
  134.       try {
  135.         jredis = new JRedisClient("localhost", 6379);
  136.         System.out.println("Redis connection established");
  137.       } catch (Exception e) {
  138.         e.printStackTrace();
  139.       }
  140.     }
  141.    
  142.    
  143.     /**
  144.      * @see java.lang.Thread#run()
  145.      */
  146.     @Override
  147.     public void run() {
  148.       try {
  149.         Random rand = new Random();
  150.        
  151.         // Perform the desired number of gets and verify the value is correct
  152.         for (int i = 0; i < RANDOM_GETS_PER_THREAD; i++) {
  153.           int keyNum = rand.nextInt(SETS_PER_THREAD);
  154.           String value = new String(jredis.get(keyPrefix + keyNum));
  155.           assert(value.equals(Integer.toString(keyNum)));
  156.         }
  157.        
  158.         // Close the connection
  159.         jredis.quit();
  160.       } catch (RedisException e) {
  161.         e.printStackTrace();
  162.       }
  163.     }
  164.   }
  165. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post