Monday, September 30, 2013

Liferay + Sonar + JMeter Configuration



Introduction:

This blog provides some useful information about the importance of the effects of coding style and practices on performance, maintainability and scalability on the software products. There is often elaborate discussions or debates over the “Effective” coding v/s “Efficient” coding.

“Efficient” often means code that is correct from the point of view of adhering to standards, using a widely accepted patterns/approaches for structures, regardless of project size, budget etc. “Effective” coding is not being “right”, but about getting things done. This often results in code that falls outside the bounds of commonly accepted “correct” standards, usage etc.

Developers often refuse to compromise their favored approach for the other. In my own experience I have found that people having formal education in software development tend towards efficient camp. Those who picked up software development more or less as a tool to get things done tend towards Effective Camp. When managing team of developers where not all are in one camp things become really challenging.

From this post I try how one can do self-improvement or help other team members improvise their code quality. Usually developers considers that the code completed by them is perfect but when this code gets integrated with the whole application may create issues. So, one has to do manual code review to understand the code and try to check if there is any issue with code affecting performance or scalability of application.

Manual Code review takes lot of time and effort and in this age of budget driven projects it becomes difficult to invest so much in manual code review.

If we can automate this process where code is continuously monitored to check against performance and scalability then things becomes easy to manage and monitor.

So I am going to provide details about how one can easily integrate Liferay with Sonar and Jmeter to help developers improvise their code and be a part of “Efficient” Camp.

 Pre-requisites:
Before we start with the tutorial make sure that your system is ready with the following:

1.       Java 1.5 or later
2.       Apache Ant (Download latest from here.)
3.       Liferay Portal Server (Download link.)
4.       Liferay Plugins SDK (Download link.)
5.       Sonar Server(Download Latest from here.)
6.       Apache JMeter (Download latest release from here.)
Download the latest version of the tools mentioned above in a folder named “Downloads”.

Tools Installation:

In this section we will learn the installation process of tools mentioned above:

Installing Liferay SDK:
Assumption: For this article I am going to work on Tomcat bundled Liferay 6.1.20.


  1.    After you have downloaded the tomcat bundled Liferay unzip the file which will create a folder named “liferay-portal-6.1.1-ce-ga2-ja”, rename this folder to “server” 
  2.  Once you have downloaded the Liferay plugins SDK from the link provided. Before you download the SDK check the version of Liferay you are working on and download the respective SDK. 
  3.    After downloading the SDK Zip file unzip the file into a folder called “plugins”.
  4. Open the plugins folder and make a copy of build.properties file under plugins folder and rename it to build.COMPUTER_NAME.properties.
  5. Keep the following properties and delete all the others:
# Specify the paths to an unzipped Tomcat bundle.
    #
    app.server.type=tomcat
    app.server.dir=${project.dir}/../bundles/tomcat-7.0.23
    app.server.deploy.dir=${app.server.dir}/webapps
    app.server.lib.global.dir=${app.server.dir}/lib/ext
    app.server.portal.dir=${app.server.dir}/webapps/ROOT
6. Change the property “app.server.dir” to PATH_TO_TOMCAT folder. for example if your server is installed under C Drive then the property will change to:

app.server.dir=C:/server/tomcat-7.0.23

7.       Do not change any other properties if you are using default setup of Liferay.


Installation of Apache ANT:
  1.  Once you have downloaded the apache ant zip file. Unzip the file and rename the folder to “apache-ant”.
  2. After unzipping the package we need to set the path for Ant. Open Control Panel and then open Advanced System Settings.
  3.  Click on the “Environment Variable” , Under System Variables click on “New…” button.
  4. Provide the variable name as “ANT_HOME” and provide the path of apache ant installation path. For example if you have unzipped the Ant zip file under Program Files(x86) then you path should be like “C:\Program Files (x86)\apache-ant-1.8.0\”.
  5. After setting ant home, we need to set path for the apache ant. Open the system variable with name “Path” in the edit mode and add the following path “C:\Program Files (x86)\apache-ant-1.8.0\bin\” and save the settings.
  6. Verify installation: to verify whether ant installation was a success open command prompt and type “ant --version” command if you see the version number in the next line then it means installation was successful.

Installation if Apache JMeter:
  1.  Once you have downloaded the Apache JMeter, unzip and rename the folder as “apache-jmeter”.
  2.  Open the Control Panel and open Advance System Settings , click on “Environment Variables” button.
  3. Under system variable click on “New…” button. Provide Variable name as “JMETER_HOME” and provide the path of apache JMeter installation path. For example if you have unzipped the JMeter zip file under Program Files(x86) then you path should be like “C:\Program Files (x86)\apache-jmeter\”.
  4. After setting jmeter  home, we need to set path for the apache jmeter. Open the system variable with name “Path” in the edit mode and add the following path “C:\Program Files (x86)\apache-jmeter\bin\” and save the settings.
  5. Verify installation: to verify whether jmeter installation was a success open command prompt and type “jmeter --version” command if you see the version number in the next line then it means installation was successful.

Installation of SONAR server:
  1. After downloading the latest version of Sonar from the link provided, unzip the file and rename the folder to “sonar”.
  2. Create a database called “sonar” in the mysql database.
  3. Under the sonar directory open the conf folder and then open sonar.properties file. sonar.jdbc.username: rootsonar.jdbc.password: root
  4. Uncomment the following line by removing ‘#’ symbol for configuring the Sonar with MySql database sonar.jdbc.url: jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8 sonar.jdbc.driverClassName: com.mysql.jdbc.Driver
  5. Start the sonar server by running the file “\bin\windows-x86-32\StartSonar.bat”
  6. Once the server starts up with no errors you will see the following log :                                    "Started SelectChannelConnector@0.0.0.0:9000 
  7. Open the browser and hit the URL : http://localhost:9000/
  8. Login using the following default credentials:                                                                                    username: admin                                                                                                                   password: admin
  9. If you are able to login then it means that you have completed the setup of Sonar successfully.

Configuring Liferay with Sonar:
  1. Download the sonar ant task jar file from this link and place it inside lib folder of directory.
  2. Open build.xml file under the “plugins” directory and add the following code:
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">



</taskdef>

<!-- Add the target -->

<target name="sonar">

<property name="sonar.jdbc.url" value="jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8" />

<property name="sonar.jdbc.driverClassName" value="com.mysql.jdbc.Driver" />

<!-- the username and password of your database -->

<property name="sonar.jdbc.username" value="root" />

<property name="sonar.jdbc.password" value="root" />

<!-- list of mandatories Sonar properties -->

<property name="sonar.sources" value="<relative-path-to-src-folders> " />

<property name="sonar.projectName" value="<project-name> " />

<property name="sonar.binaries" value="<relative-path-to-classes>" />

<property name="sonar.libraries" value="<relative-path-to-lib>" />

<sonar:sonar key="<project-name>" version="0.1-SNAPSHOT" xmlns:sonar="antlib:org.sonar.ant"/>

</target>



3.       Build the hooks/portlets using ant command using either “ant deploy” command or “ant compile” command. 
4.       Once the build for portlets is successful run ant task “ant sonar” to generate sonar reports. Make sure sonar task doesn’t throw any errors. 
5.       Login to the Sonar server to see the results.
6.       If you face any heap size while running sonar task use the following command:

for windows: set ANT_OPTS=-Xmx1g
for linux : export ANT_OPTS=-Xmx1g

Configuring Sonar with Jmeter:

  1.  Before you configure Sonar with JMeter you need to configure JMeter with Liferay. Please follow this link and once you have completed a test case with Liferay export the result as .jtl file.
  2. To configure JMeter with Sonar download the JMeter Reporting Server from this link.
  3. Once you have downloaded unzip the file and add configuration related to project as the following: testconfig.project_name.desc=project_name        testconfig.project_name.prject=PATH_TO_JTL_FIL  
  4. Here "testconfig.project_name.desc" is the name of Project. and "testconfig.project_name.jtlpath" is the path of Jmeter report file.
  5. Once you are done with changes in jmeter-report-server.properties file, then start Jmeter Report server by running jmeter-report-server.bat on Windows platform.\
  6.  Select Project in which you want to show output of Jmeter Load test result. Go to project Settings -> Jmeter.
  7.  Add below configuration and click on "Save Jmeter Settings" button:                                        Remote config name=project_name                                                                                         Remote server host=                                                     Remote server port=5555
  8. Now when next build will run, Sonar-Jmeter will show out of Jmeter Result file.