Monday, September 19, 2016

How to use VisualVM

      VisualVM can be very helpful to discover the performance lags in Java application.
 It is one of the easiest profiling tools for Java.


Download VisualVM
https://visualvm.github.io/


Run VisualVM and check local running java apps: 


Remote Profiling.
Run your java application with following JVM arguments:
-Djavax.management.builder.initial=
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=9010
-Dcom.sun.management.jmxremote.local.only=false
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
Above parameters, makes your remote java application to listen to port 9010.
Then, you can connect to it from VisualVM by Menu->File->Add JMX connection
Type your hostname and port. Example: 192.168.10.10:9010
(IP address of remote machine and port)

Performance Profiling
      After you connect to your app from VisualVM, go to "Sampler" tab and press "CPU" button.
It is important to sort by "Total Time(CPU)" to see high CPU consumers on the top of the list.
This gives you some idea, but it is not detail. So, to get detail information,
press "Snapshot" button, this opens you following view:





VisualVM allows you to real-time monitoring which functions are taking up high CPU usage.

This window is very important. From this, you can find which functions, classes,
or packages are causing your Java application to be slow.
It is the key approach to resolve performance issues in your java application.
You can play with sorting options, and navigate through callers,
and check other tabs "Host Spots" etc.

Memory Profiling
      Memory usage analysing is also similar to above. Press "Memory" button in Sampler window.
Sort by "Bytes" to see data types (or classes) which are consuming much memory on the top.
You can also take "Snapshot" to see more details about the monitoring status.

Conclusion
   VisualVM can be very helpful to monitor, analyse, tune Java Application Performance.
This is essential task while developing scalabale, distributed, high-performance applications.














Performance tuning for Web engine


Install Tsung on CentOS

Pre-requisites:
1. Install Erlang:

sudo yum -y update && sudo yum -y upgrade
sudo yum install epel-release

sudo yum -y install erlang perl perl-RRD-Simple.noarch perl-Log-Log4perl-RRDs.noarch gnuplot perl-Template-Toolkit


2. Get Tsung
wget http://tsung.erlang-projects.org/dist/tsung-1.6.0.tar.gz

3. Extract and Install
tar zxvf tsung-1.6.0.tar.gz
cd tsung-1.6.0
./configure && make && sudo make install

Note: Sample XML configurations are located in /usr/share/doc/tsung/examples/http_simple.xml

Setup up Cluster Testing with Tsung

1. Add cluster nodes info in each node's "/etc/hosts"
sudo vi /etc/hosts
# cluster nodes
192.168.10.10       n1
192.168.10.11       n2
192.168.10.12       n3
192.168.10.13       n4

2. Setup ~/.ssh/config file
vi ~/.ssh/config
Host n1
  Hostname n1
  User tsung
  Port 722
  IdentityFile /home/tsung/.ssh/my_key_rsa7
Host n2
  Hostname n2
  User tsung
  Port 722
  IdentityFile /home/tsung/.ssh/my_key_rsa7
.....

Test and Visualize Results
1. Start tsung on master server
tsung -f /home/tsung/test/selected_scenario.xml start

2. Plot graphs with Perl script
/usr/lib/tsung/bin/tsung_stats.pl --stats /home/tsung/.tsung/log/$tsung_path/tsung.log

Change Kernel params

vi /etc/sysctl.conf

net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 1024 65000
fs.file-max = 65000

Source: http://tsung.erlang-projects.org/user_manual/faq.html#why-do-i-have-error-connect-emfile-errors

References:
 https://gist.github.com/huberflores/2827890
  https://github.com/ngocdaothanh/tsart
 https://gist.github.com/clasense4/47438a884cabca9e66c8 

 http://www.jeramysingleton.com/install-erlang-and-elixir-on-centos-7-minimal/

 https://jackiechen.org/2015/12/04/use-tsung-to-test-https-site/