Advertisement
Guest User

CpuCacheTest

a guest
Jun 4th, 2011
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1.  
  2.  
  3. import java.util.Random;
  4.  
  5.  
  6. public class CpuCacheTest {
  7.  
  8.     static final int
  9.         POOL_MIN_BYTES = 1<<3,
  10.         POOL_MAX_BYTES = 1<<28,
  11.        
  12.         ACCESS_ORDER_LIST_LENGTH = 10 * 1000 * 1000,
  13.         NUM_REPEATS = 10;
  14.    
  15.     int numBytes;
  16.     byte[] bytes;//the pool of bytes out in main memory
  17.     int[] accessOrder;//will always be accessed sequentially
  18.    
  19.    
  20.     CpuCacheTest(int sizeInBytes, boolean randomAccess){
  21.         Random random = new Random();
  22.        
  23.         //populate a byte pool of the specified size
  24.         this.numBytes = sizeInBytes;
  25.         this.bytes = new byte[numBytes];
  26.         for(int i=0; i < numBytes; ++i){//fill the array with something
  27.             bytes[i] = (byte)(random.nextInt(127));
  28.         }
  29.        
  30.         //generate an array of either sequential or random indexes of the pool
  31.         accessOrder = new int[ACCESS_ORDER_LIST_LENGTH];
  32.         for(int i=0; i < ACCESS_ORDER_LIST_LENGTH; ++i){
  33.             if(randomAccess){
  34.                 accessOrder[i] = random.nextInt(numBytes);
  35.             }else{
  36.                 accessOrder[i] = i % numBytes;
  37.             }
  38.         }
  39.        
  40.         //minimize chances of interruption
  41.         System.gc();
  42. //      System.runFinalization();//doesn't seem to matter
  43.     }
  44.    
  45.    
  46.     void iterateBytes(){
  47.         long startNs = System.nanoTime();
  48.         byte val = -1;
  49.         for(int repetition=0; repetition < NUM_REPEATS; ++repetition){
  50.             for(int accessIdx=0; accessIdx < ACCESS_ORDER_LIST_LENGTH; ++accessIdx){
  51.                 //do something so the compiler doesn't optimize the array access away
  52.                 val += bytes[accessOrder[accessIdx]];
  53.             }
  54.         }
  55.         long endNs = System.nanoTime();
  56.        
  57.         long durationNs = Math.max(1, endNs - startNs);
  58. //      System.out.print(pad(""+durationNs,' ',12));
  59.         System.out.print(pad(""+megabytesPerSecond(durationNs),' ',12));
  60.     }
  61.    
  62.    
  63.     /**************************** main **************************************/
  64.    
  65.     public static void main(String... args){
  66.         System.out.println("bytes fetched per microsecond (megabytes per second)");
  67.         System.out.println("pool size      sorted      random");
  68.         int stepFactor = 2;
  69.         for(int sizeInBytes=POOL_MIN_BYTES; sizeInBytes <= POOL_MAX_BYTES; sizeInBytes *= stepFactor){
  70.             System.out.print(pad(getAbbreviatedBytes(sizeInBytes)+"", ' ', 9));
  71.            
  72.             //streaming bytes sequentially from memory
  73.             new CpuCacheTest(sizeInBytes, false).iterateBytes();
  74.            
  75.             //grabbing bytes randomly from memory
  76.             new CpuCacheTest(sizeInBytes, true).iterateBytes();
  77.            
  78.             System.out.println();
  79.         }
  80.     }
  81.    
  82.    
  83.     /************************** helpers **********************************/
  84.  
  85.     long megabytesPerSecond(long durationNs){
  86.         double numOps = (double)(ACCESS_ORDER_LIST_LENGTH * NUM_REPEATS);
  87.         double opsPerNs = numOps / (double)durationNs;
  88.         return (long)(1000 * opsPerNs);
  89.     }
  90.    
  91.     static String getAbbreviatedBytes(long bytes){
  92.         if(bytes < 1<<10){ return bytes + "B"; }
  93.         if(bytes < 1<<20){ return (bytes>>10) + "K"; }
  94.         if(bytes < 1<<30){ return (bytes>>20) + "M"; }
  95.         return (bytes>>30) + "G";
  96.     }
  97.    
  98.     static String pad(String input, char padding, int length){
  99.         if(input.length() > length){
  100.             throw new IllegalArgumentException("input \""+input+"\" longer than maxLength="+length);
  101.         }
  102.         int charsToGo = length - input.length();
  103.         return repeat(padding, charsToGo) + input;
  104.     }
  105.    
  106.     static String repeat(char chr, int number) {
  107.         StringBuilder sb = new StringBuilder();
  108.         for(int i=0; i < number; ++i){
  109.             sb.append(chr);
  110.         }
  111.         return sb.toString();
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement