Fork me on GitHub

Examples

Perfidix offers a very flexible usage based on annotation. The examples contains all the same usecase but are different implemented. A compressed file access is compared to a normal file access. The code in the following examples therefore are doing exactly the same.

Example 1

The setUp() and tear-Down() methods are invoked before each run of each method.

public class SomeAnnoBenchmark { 
    CompressedHandler c; SimpleFileHandler s;

    //setUp , invoked before each run
    @BeforeEachBenchRun
    public void setUp() {
       c = new CompressedHandler ( ) ; s = new SimpleFileHandler ();
    }

    //tearDown , invoked after each run
    @AfterEachBenchRun 
    public void tearDown() {
       c = null; s = null;
    }

    //bench Method 1
    @Bench
    public void benchCWrite() {
       c.write(”hello world”);
    }

    //bench Method 1
    @Bench
    public void benchSRead() {
       s.read();
    } 

    //bench Method 2
    @Bench
    public void benchSWrite () {
       s.write(”hello world”); 
    }

    //bench Method 3
    @Bench
    public void benchCRead() {
       c.read();
    }
}

Example 2

In the following example, you see the usage of specific setUp and tearDown methods. These methods have the same behavior than methods with the BeforeBenchRun annotation.

public class SomeSpecificSetUpTearDownBenchmark { 
    CompressedHandler c; SimpleFileHandler s;

    //setUp for benchCRead/benchCWrite . Invoked via @Bench−params
    public void setUpCompressed () {
       c = new CompressedHandler ( ) ;
    }

    //tearDown for benchCRead/benchCWrite . Invoked via @Bench−params
    public void tearDownCompressed() {
       c = null;
    }

    //setUp for benchSRead/benchSWrite . Invoked via @Bench−params
    public void setUpSimple () {
       s = new SimpleFileHandler ();
    }

    //tearDown for benchSRead/benchSWrite . Invoked via @Bench−params 
    public void tearDownSimple () {
       s = null;
    }

    //bench Method 1
    @Bench(beforeEachBenchRun=”setUpCompressed” ,afterEachBenchRun=”tearDownCompressed”)
    public void benchCWrite() { 
       c.write(”hello world”);
    }

    //bench Method 2
    @Bench(beforeEachBenchRun =”setUpSimple”, afterEachBenchRun =”tearDownSimple”)
    public void benchSWrite () { 
       s.write(”hello world”);
    }

    //bench Method 3
    @Bench(beforeEachBenchRun =”setUpCompressed”, afterEachBenchRun=”tearDownCompressed”)
    public void benchCRead() { 
       c.read();
    }

    //bench Method 4
    @Bench(beforeEachBenchRun =”setUpSimple”, afterEachBenchRun =”tearDownSimple”)
    public void benchSRead() { 
       s.read();
    }
}

Example 3

In the following example, the same Bench is a little bit modified: First of all, the class-annotation BenchClass with the param runs is used. That means that every method which is parameter-free and is not annotated with a setUp / tearDown annotation, is benched 10 times, except the benchSWrite method, which has an extra Bench annotation with a run parameter. This method is benched 60 times.\ Additional to that, every possible setUp and tearDown method is used in this example. A description is given in the code and in Section 3.

@BenchClass(runs=10)
public class ClassAnnoBenchmark {
    CompressedHandler c ; SimpleFileHandler s ;

    String toTest; long testLength;

    //classwide setUp , invoked just one time , just setting the length
    @BeforeBenchClass
    public void beforeClass () {
       Math.abs(testLength = new Random().nextInt(100));
    }

    //methodWide setUp , invoked just one time per method , building a
    @BeforeFirstBenchRun
    public void beforeMethod () {
       for(int i = 0; i<testLength; i++) {
         toTest = toTest + (char)(new Random(). nextInt(Ch
         }
       }

       //normal setUp , invoked one time per method per run, instantiating
       @BeforeEachBenchRun
    public void beforeRun () {
       c = new CompressedHandler ( ) ; 
       s = new SimpleFileHandler ();
    }

    //normal tearDown , invoked one time per method per run , removing
    @AfterEachBenchRun 
    public void afterRun () {
       c = null;
       s = null;
    }

    //methodWide tearDown , invoked just one time
    @AfterLastBenchRun
    public void afterMethod () {
       toTest = null;
    }

    //classwide tearDown , invoked just one time ,
    @AfterBenchClass
    public void afterClass () {
       testLength = −1;
    }

    //bench 1, invoked because of class−annotation
    public void benchCWrite() {
       c.write(”hello world”);
    }

    //bench 2, invoked because of method−annotation 
    @Bench(runs=60)
    public void benchSWrite () {
       s.write(”hello world”);
    }

    //bench 3, invoked because of class−annotation 
    public void benchCRead() {
       c.read();
    }

    //bench 4, invoked because of class−annotation
    public void benchSRead() {
       s.read();
    }
}