Friday, May 16, 2014

Java EE 7 Performance Tuning and Optimization Book


The book covers performance tuning and optimization in Java EE 7, it takes months from me to complete this book :) and i did my best to cover most of the important topics in this area.

What this book covers
Chapter 1, Getting Started with Performance Tuning, takes you through the art of performance tuning with its different components and shows you how to think when we face any performance issue. It focuses on preparing you to deal with the world of performance tuning and defining the handling tactics.
Chapter 2, Understanding Java Fundamentals, lays the foundation of required knowledge of the new features in Java Enterprise Edition 7 and different important Java concepts, including the JVM memory structure and Java concurrency. It also focuses on the different Java Enterprise Edition concurrency capabilities.
Chapter 3, Getting Familiar with Performance Testing, discusses performance testing with its different components, defines useful terminologies that you need to be aware of, and then gives hands-on information about using Apache JMeter to create your performance test plans for different components and get the results.
Chapter 4, Monitoring Java Applications, dissects the different monitoring tools that will be used in performance tuning, starting from the operating system tools, different IDE tools, JDK tools, and standalone tools. It covers JProfiler as an advanced profiling tool with its offline profiling capabilities.
Chapter 5, Recognizing Common Performance Issues, discusses the most common performance issues, classifies them, describes the symptoms, and analyzes the possible root causes.
Chapter 6, CPU Time Profiling, focuses on the details of getting the CPU and time profiling results, ways to interpret the results, and ways to handle such issues. It discusses the application logic performance and ways to evaluate different application logics. It provides the initial performance fixing strategy.
Chapter 7, Thread Profiling, discusses thread profiling with details on how to read and interpret thread profiling results and how to handle threading issues. It also highlights the ways to get, use, and read the thread dumps.
Chapter 8, Memory Profiling, discusses how to perform memory profiling, how to read and interpret the results, and how to identify and handle possible issues. It also shows how to read and query memory heap dumps and analyze the different out of memory root causes. The chapter finishes your draft performance fixing strategy.
Chapter 9, Tuning an Application's Environment, focuses on tuning the application environment, starting from the JVM and passing through other elements such as the application servers, web servers, and OS. We will focus on selected examples for each layer and discuss the best practices for tuning them.
Chapter 10, Designing High-performance Enterprise Applications, discusses design and architecture decisions and the performance impact. This includes SOA, REST, cloud, and data caching. It also discusses the performance anti-patterns.
Chapter 11, Performance Tuning Tips, highlights the performance considerations when using the Agile or Test-driven Development methodologies. This chapter also discusses some performance tuning tips that are essential during the designing and development stages of the Java EE applications, including database interaction, logging, exception handling, dealing with Java collections, and others. The chapter also discusses the javap tool that will help you to understand the compiled code in a better way.
Chapter 12, Tuning a Sample Application, includes hands-on, step-by-step tuning of a sample application that has some performance issues. We will measure the application performance and tune the application issues, and re-evaluate the application performance.

The book is published by Packt Publishing:
You can find the covered topics in Table of content in the Packt Publishing web site:
http://www.packtpub.com/java-ee-7-performance-tuning-and-optimization/book

Packt Publishing offers free shipping to UK, US, Europe and selected countries in Asia.


I hope everyone found this book useful and get the maximum value from the book topics.






Thursday, April 3, 2014

BRB

Apologize for being busy in the past period, will try to give more time to my blog in the near future once I finished all my existing commitments :)


Friday, March 15, 2013

Java Performance Tuning

Last week I conduct a session on JDC 2013 about Profiler-Guided Java Performance Tuning..

It was focusing on how to use profile tools results to guide you through tuning your Java application, After the introduction , basic concepts in Java : Concurrency and Memory management discussed..
The hands-on section done using NetBeans , Eclipse and JProfiler (commercial)

Here is the slides of that presentation , but presentation doesn't contain except the high level points of the session ...




For More information, refer to my Java EE 7 performance tuning and optimization book: The book is published by Packt Publishing: http://www.packtpub.com/java-ee-7-performance-tuning-and-optimization/book

Saturday, October 27, 2012

Add Native Support to JS in Android HTML5 Apps

Running HTML 5 apps in Android , is not effective as you loss a lot of native power , Android provides the ability to expose native power to JS so you can use the powerful native support in your app.

Here is the simple steps:


1. Create any class and name it any thing like JavaScriptInterface
Example:


public class JavaScriptInterface {
        Context mContext;

        JavaScriptInterface(Context c) {
            mContext = c;
        }

        public void doSomething() {
         System.err.println("inside do something");
        }
}

2. The class should have constructor that take Context as parameter

3. During instatiation send the activity object in the constructor and add nick(prefix) name to be used inside JS to call this class methods:

     webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");


Example:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crazy);
    WebView webView=(WebView)findViewById(R.id.webView);
    webView.setWebChromeClient(new WebChromeClient());
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.getSettings().setDatabaseEnabled(true);
    webView.getSettings().setDatabasePath("/data/data/osa.ora.test/databases/");
    webView.getSettings().setDomStorageEnabled(true);
    //remove this line for emulator bug in Android 2.3
    webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
       //the following line to prevent the backlight from going during application running.
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    webView.loadUrl("file:///android_asset/www/index.html");    
    }


4. In the JS call this methods

Example:
Android.doSomething();

You can see in the console the outcome of the sys err statement.
(Android is the prefix we have specified during creating instance of this JS interface)

5. Of course we can have many interfaces each for specific purpose.

Simple and straightforward but there is a bug in android emulator in 2.3 so better to test it on mobile devices direct in case you need to test it on such platform.

Monday, October 22, 2012

"Hit The Ball" .. Android Game

I have published 1st version of my game "Hit The Ball" over Android platform.


Links:

Here is the link for the demo version :
https://play.google.com/store/apps/details?id=hit.ball.game.demo

And this is the link to commercial version :
https://play.google.com/store/apps/details?id=hit.ball.game

Description:
The game is based on HTML 5 technology and use Java Script Interface for some native work.
It uses Box2D implementation as physics engine for the game.

The idea behind this game is you play with a ball and you need to hit the other ball in the scene.
If you hit the other ball , you go to next level, where it get complicated little bit.
You can select the destination of the shoot by your click/finger.
If you didn't shoot the ball , the ball will be automatically shoot to a default location.
If you pick small ball you get extra trial.
Pay attention to the speed bar during shooting.
You can resume the game from the last successful level which is auto-saved.

Game screen shots:




Saturday, October 6, 2012

Java Fork Join & Parallel Programming

After emerging of multi-cores programs , a major shift to the application development to utilize these cores by forking many threads/process for the applications esp. in the intensive processing procedure.

Java came lately to the paradigm shift with concurrency APIs in Java 7  (JSR 166) (which was initially planned in Java 5), the framework will go under another improvement to facilitate the current complex way in Java 8.

Best fit algorithms for paralization is divide-and-conquer algorithms.
In this post we will go through example of using the fork-join to develop quick sort enhancement..

1) Framework important parts:
-ForkJoinTask : this is a task to be forked and joined after processing.
It has lifecycle methods as doJoin , doInvoke , doExecute plus status.

- RecursiveAction extends ForkJoinTask: represent a recursive action..
It has important method which is compute..
We will extend this class and override compute method to do what we need.

-ForkJoinPool : represents the pool for fork and join framework , you can initialize it by the number of cores or by fixed magic number ...
Note: To get the number of processors/cores:
int processors = Runtime.getRuntime().availableProcessors();

2) Non-parallel Quick sort:
Static method that process the numbers[] to sort them...


//non-parallellll...
private static int[] numbers;
private static void quicksort(int low, int high) {
int i = low, j = high;
// Get the pivot element from the middle of the list
int pivot = numbers[low + (high-low)/2];

// Divide into two lists
while (i <= j) {
// If the current value from the left list is smaller then the pivot
// element then get the next element from the left list
while (numbers[i] < pivot) {
i++;
}
// If the current value from the right list is larger then the pivot
// element then get the next element from the right list
while (numbers[j] > pivot) {
j--;
}

// If we have found a values in the left list which is larger then
// the pivot element and if we have found a value in the right list
// which is smaller then the pivot element then we exchange the
// values.
// As we are done we can increase i and j
if (i <= j) {
exchange(i, j);
i++;
j--;
}
}
// Recursion
if (low < j)
quicksort(low, j);
if (i < high)
quicksort(i, high);
}

private static void exchange(int i, int j) {
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}



3) Using Fork-Join:


public class ParallelQuickSort extends RecursiveAction {
    Phaser phaser;
    int[] arr = null;
    int left;
    int right;

    ParallelQuickSort(Phaser phaser, int[] arr) {
        this(phaser, arr, 0, arr.length - 1);
    }

    ParallelQuickSort(Phaser phaser, int[] arr, int left, int right) {
        this.phaser = phaser;
        this.arr = arr;
        this.left = left;
        this.right = right;
        phaser.register();  //important
    }


    private ParallelQuickSort leftSorter(int pivotI) {
        return new ParallelQuickSort(phaser, arr, left, --pivotI);
    }

    private ParallelQuickSort rightSorter(int pivotI) {
        return new ParallelQuickSort(phaser, arr, pivotI, right);
    }

    private void recurSort(int leftI, int rightI) {
        if (rightI - leftI > 7) {
            int pIdx = partition(leftI, rightI, getPivot(arr, leftI, rightI));
            recurSort(leftI, pIdx - 1);
            recurSort(pIdx, rightI);
        } else if (rightI - leftI > 0) {
            insertionSort(leftI, rightI);
        }
    }


    @Override
    protected void compute() {
        if (right - left > 1000) {   // if more than 1000 (totally arbitrary number i chose) try doing it parallelly
            int pIdx = partition(left, right, getPivot(arr, left, right));
            leftSorter(pIdx).fork();
            rightSorter(pIdx).fork();

        } else if (right - left > 7) {  // less than 1000 sort recursively in this thread
            recurSort(left, right);

        } else if (right - left > 0) {  //if less than 7 try simple insertion sort
            insertionSort(left, right);
        }

        if (isRoot()) { //if this instance is the root one (the one that started the sort process), wait for others
                        // to complete.
            phaser.arriveAndAwaitAdvance();
        } else {  // all not root one just arrive and de register not waiting for others.
            phaser.arriveAndDeregister();
        }
    }

    /** Patition the array segment based on the pivot   **/
    private int partition(int startI, int endI, int pivot) {
        for (int si = startI - 1, ei = endI + 1; ; ) {
            for (; arr[++si] < pivot;) ;
            for (; ei > startI && arr[--ei] > pivot ; ) ;
            if (si >= ei) {
                return si;
            }
            swap(si, ei);
        }
    }

    private void insertionSort(int leftI, int rightI) {
        for (int i = leftI; i < rightI + 1; i++)
            for (int j = i; j > leftI && arr[j - 1] > arr[j]; j--)
                swap(j, j - 1);

    }

    private void swap(int startI, int endI) {
        int temp = arr[startI];
        arr[startI] = arr[endI];
        arr[endI] = temp;
    }

    /**
     * Check to see if this instance is the root, i.e the first one used to sort the array.
     * @return
     */
    private boolean isRoot() {
        return arr.length == (right - left) + 1;
    }

    /**
     * copied from java.util.Arrays
     */
    private int getPivot(int[] arr, int startI, int endI) {
        int len = (endI - startI) + 1;
        // Choose a partition element, v
        int m = startI + (len >> 1);       // Small arrays, middle element
        if (len > 7) {
            int l = startI;
            int n = startI + len - 1;
            if (len > 40) {        // Big arrays, pseudomedian of 9
                int s = len / 8;
                l = med3(arr, l, l + s, l + 2 * s);
                m = med3(arr, m - s, m, m + s);
                n = med3(arr, n - 2 * s, n - s, n);
            }
            m = med3(arr, l, m, n); // Mid-size, med of 3
        }
        int v = arr[m];
        return v;
    }

    /**
     * copied from java.util.Arrays
     */
    private static int med3(int x[], int a, int b, int c) {
        return (x[a] < x[b] ?
                (x[b] < x[c] ? b : x[a] < x[c] ? c : a) :
                (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
    }


4) Testing :
Generate big random array then sort it using both and see the results:

       private static int[] getRandom(int i) {
        Random randomGenerator = new Random(i);
        int[] array = new int[i];
        for (int n = 0; n < i; n++) {
            array[n] = randomGenerator.nextInt();
        }
        return array;
    }

    public static void main(String[] args) throws InterruptedException {
        int[] arr = getRandom(1000000);
        numbers=Arrays.copyOf(arr, arr.length);
        int[] arr2=Arrays.copyOf(arr, arr.length);
        System.out.println("show: " + arr.length+" "+numbers.length);
        System.out.println("show: " + arr[0]+" "+arr[arr.length-1]);
        System.out.println("show: " + numbers[0]+" "+numbers[numbers.length-1]);
        ForkJoinPool pool = new ForkJoinPool();
        StopWatch  stopWatch=new StopWatch();
        Phaser phaser = new Phaser();
        pool.invoke(new ParallelQuickSort(phaser, arr));
        stopWatch.stop();
        System.out.println("Elapsed Time: " + stopWatch.getElapsedTime());
        System.out.println("show: " + arr[0]+" "+arr[arr.length-1]);
        System.out.println("show: " + numbers[0]+" "+numbers[numbers.length-1]);
        numbers=getRandom(1000000);
        stopWatch=new StopWatch();        
        quicksort(0,numbers.length-1);
        stopWatch.stop();
        System.out.println("Elapsed Time: " + stopWatch.getElapsedTime());
        System.out.println("show: " + arr[0]+" "+arr[arr.length-1]);
        System.out.println("show: " + numbers[0]+" "+numbers[numbers.length-1]);     
        stopWatch=new StopWatch();        
        Arrays.sort(arr2);
        stopWatch.stop();
        System.out.println("Elapsed Time: " + stopWatch.getElapsedTime());
    }



5) Output :

Example of output of this code:


show: 1000000 1000000
show: 1608240105 -356486679
show: 1608240105 -356486679
Elapsed Time: 68
show: -2147481329 2147476538
show: 1608240105 -356486679
Elapsed Time: 114
show: -2147481329 2147476538
show: -2147481329 2147476538
Elapsed Time: 92



So sorting 1 million element in the array toke 65 milliseconds using fork-join and 113 milliseconds using single threaded mode, and using java optimized quick sort it takes 92 milliseconds.