Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache.kahadb.util » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.kahadb.util;
   18   
   19   import java.io.File;
   20   import java.io.FileNotFoundException;
   21   import java.io.IOException;
   22   import java.io.RandomAccessFile;
   23   import java.util.ArrayList;
   24   import java.util.Arrays;
   25   
   26   /**
   27    * This class is used to get a benchmark the raw disk performance.
   28    */
   29   public class DiskBenchmark {
   30   
   31       boolean verbose;
   32       // reads and writes work with 4k of data at a time.
   33       int bs=1024*4; 
   34       // Work with 100 meg file.
   35       long size=1024*1024*500; 
   36       long sampleInterval = 10*1000; 
   37       
   38       public static void main(String[] args) {
   39   
   40           DiskBenchmark benchmark = new DiskBenchmark();
   41           args = CommandLineSupport.setOptions(benchmark, args);
   42           ArrayList<String> files = new ArrayList<String>();
   43           if (args.length == 0) {
   44               files.add("disk-benchmark.dat");
   45           } else {
   46               files.addAll(Arrays.asList(args));
   47           }
   48   
   49           for (String f : files) {
   50               try {
   51                   File file = new File(f);
   52                   if (file.exists()) {
   53                       System.out.println("File " + file + " allready exists, will not benchmark.");
   54                   } else {
   55                       System.out.println("Benchmarking: " + file.getCanonicalPath());
   56                       Report report = benchmark.benchmark(file);
   57                       file.delete();
   58                       System.out.println(report.toString());
   59                   }
   60               } catch (Throwable e) {
   61                   if (benchmark.verbose) {
   62                       System.out.println("ERROR:");
   63                       e.printStackTrace(System.out);
   64                   } else {
   65                       System.out.println("ERROR: " + e);
   66                   }
   67               }
   68           }
   69   
   70       }
   71       
   72       public static class Report {
   73   
   74           public int size;
   75           
   76           public int writes;
   77           public long writeDuration;
   78           
   79           public int syncWrites;
   80           public long syncWriteDuration;
   81           
   82           public int reads;
   83           public long readDuration;
   84   
   85           @Override
   86           public String toString() {
   87               return 
   88               "Writes: \n" +
   89               "  "+writes+" writes of size "+size+" written in "+(writeDuration/1000.0)+" seconds.\n"+
   90               "  "+getWriteRate()+" writes/second.\n"+
   91               "  "+getWriteSizeRate()+" megs/second.\n"+
   92               "\n"+
   93               "Sync Writes: \n" +
   94               "  "+syncWrites+" writes of size "+size+" written in "+(syncWriteDuration/1000.0)+" seconds.\n"+
   95               "  "+getSyncWriteRate()+" writes/second.\n"+
   96               "  "+getSyncWriteSizeRate()+" megs/second.\n"+
   97               "\n"+
   98               "Reads: \n" +
   99               "  "+reads+" reads of size "+size+" read in "+(readDuration/1000.0)+" seconds.\n"+
  100               "  "+getReadRate()+" writes/second.\n"+
  101               "  "+getReadSizeRate()+" megs/second.\n"+
  102               "\n"+
  103               "";
  104           }
  105   
  106           private float getWriteSizeRate() {
  107               float rc = writes;
  108               rc *= size;
  109               rc /= (1024*1024); // put it in megs
  110               rc /= (writeDuration/1000.0); // get rate. 
  111               return rc;
  112           }
  113   
  114           private float getWriteRate() {
  115               float rc = writes;
  116               rc /= (writeDuration/1000.0); // get rate. 
  117               return rc;
  118           }
  119           
  120           private float getSyncWriteSizeRate() {
  121               float rc = syncWrites;
  122               rc *= size;
  123               rc /= (1024*1024); // put it in megs
  124               rc /= (syncWriteDuration/1000.0); // get rate. 
  125               return rc;
  126           }
  127   
  128           private float getSyncWriteRate() {
  129               float rc = syncWrites;
  130               rc /= (syncWriteDuration/1000.0); // get rate. 
  131               return rc;
  132           }
  133           private float getReadSizeRate() {
  134               float rc = reads;
  135               rc *= size;
  136               rc /= (1024*1024); // put it in megs
  137               rc /= (readDuration/1000.0); // get rate. 
  138               return rc;
  139           }
  140   
  141           private float getReadRate() {
  142               float rc = reads;
  143               rc /= (readDuration/1000.0); // get rate. 
  144               return rc;
  145           }
  146   
  147           public int getSize() {
  148               return size;
  149           }
  150   
  151           public void setSize(int size) {
  152               this.size = size;
  153           }
  154   
  155           public int getWrites() {
  156               return writes;
  157           }
  158   
  159           public void setWrites(int writes) {
  160               this.writes = writes;
  161           }
  162   
  163           public long getWriteDuration() {
  164               return writeDuration;
  165           }
  166   
  167           public void setWriteDuration(long writeDuration) {
  168               this.writeDuration = writeDuration;
  169           }
  170   
  171           public int getSyncWrites() {
  172               return syncWrites;
  173           }
  174   
  175           public void setSyncWrites(int syncWrites) {
  176               this.syncWrites = syncWrites;
  177           }
  178   
  179           public long getSyncWriteDuration() {
  180               return syncWriteDuration;
  181           }
  182   
  183           public void setSyncWriteDuration(long syncWriteDuration) {
  184               this.syncWriteDuration = syncWriteDuration;
  185           }
  186   
  187           public int getReads() {
  188               return reads;
  189           }
  190   
  191           public void setReads(int reads) {
  192               this.reads = reads;
  193           }
  194   
  195           public long getReadDuration() {
  196               return readDuration;
  197           }
  198   
  199           public void setReadDuration(long readDuration) {
  200               this.readDuration = readDuration;
  201           }
  202       }
  203   
  204   
  205       public Report benchmark(File file) throws IOException {
  206           Report rc = new Report();
  207           
  208           // Initialize the block we will be writing to disk.
  209           byte []data = new byte[bs];
  210           for (int i = 0; i < data.length; i++) {
  211               data[i] = (byte)('a'+(i%26));
  212           }
  213           
  214           rc.size = data.length;
  215           RandomAccessFile raf = new RandomAccessFile(file, "rw");
  216           raf.setLength(size);
  217           
  218           // Figure out how many writes we can do in the sample interval.
  219           long start = System.currentTimeMillis();
  220           long now = System.currentTimeMillis();
  221           int ioCount=0;
  222           while( true ) {
  223               if( (now-start)>sampleInterval ) {
  224                   break;
  225               }
  226               raf.seek(0);
  227               for( long i=0; i+data.length < size; i+=data.length) {
  228                   raf.write(data);
  229                   ioCount++;
  230                   now = System.currentTimeMillis();
  231                   if( (now-start)>sampleInterval ) {
  232                       break;
  233                   }
  234               }
  235               // Sync to disk so that the we actually write the data to disk.. otherwise 
  236               // OS buffering might not really do the write.
  237               raf.getFD().sync();
  238           }
  239           raf.getFD().sync();
  240           raf.close();
  241           now = System.currentTimeMillis();
  242           
  243           rc.size = data.length;
  244           rc.writes = ioCount;
  245           rc.writeDuration = (now-start);
  246   
  247           raf = new RandomAccessFile(file, "rw");
  248           start = System.currentTimeMillis();
  249           now = System.currentTimeMillis();
  250           ioCount=0;
  251           while( true ) {
  252               if( (now-start)>sampleInterval ) {
  253                   break;
  254               }
  255               for( long i=0; i+data.length < size; i+=data.length) {
  256                   raf.seek(i);
  257                   raf.write(data);
  258                   raf.getFD().sync();
  259                   ioCount++;
  260                   now = System.currentTimeMillis();
  261                   if( (now-start)>sampleInterval ) {
  262                       break;
  263                   }
  264               }
  265           }
  266           raf.close();
  267           now = System.currentTimeMillis();
  268           rc.syncWrites = ioCount;
  269           rc.syncWriteDuration = (now-start);
  270   
  271           raf = new RandomAccessFile(file, "rw");
  272           start = System.currentTimeMillis();
  273           now = System.currentTimeMillis();
  274           ioCount=0;
  275           while( true ) {
  276               if( (now-start)>sampleInterval ) {
  277                   break;
  278               }
  279               raf.seek(0);
  280               for( long i=0; i+data.length < size; i+=data.length) {
  281                   raf.seek(i);
  282                   raf.readFully(data);
  283                   ioCount++;
  284                   now = System.currentTimeMillis();
  285                   if( (now-start)>sampleInterval ) {
  286                       break;
  287                   }
  288               }
  289           }
  290           raf.close();
  291           
  292           rc.reads = ioCount;
  293           rc.readDuration = (now-start);
  294           return rc;
  295       }
  296   
  297   
  298       public boolean isVerbose() {
  299           return verbose;
  300       }
  301   
  302   
  303       public void setVerbose(boolean verbose) {
  304           this.verbose = verbose;
  305       }
  306   
  307   
  308       public int getBs() {
  309           return bs;
  310       }
  311   
  312   
  313       public void setBs(int bs) {
  314           this.bs = bs;
  315       }
  316   
  317   
  318       public long getSize() {
  319           return size;
  320       }
  321   
  322   
  323       public void setSize(long size) {
  324           this.size = size;
  325       }
  326   
  327   
  328       public long getSampleInterval() {
  329           return sampleInterval;
  330       }
  331   
  332   
  333       public void setSampleInterval(long sampleInterval) {
  334           this.sampleInterval = sampleInterval;
  335       }
  336   
  337   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache.kahadb.util » [javadoc | source]