Tuesday, July 24, 2012

JMeter is not good tool for finding Request per second

I played around with performance tools a lot in my last project and this was one blog which I never got to finish. My goal was to come with a tool which is best for benchmarking an application on Tomcat. While working with JMeters all the numbers compare to AB tool were low, I suspected Tomcat and started reading the tomcat performance tuning book and found below lines.

(Page 136 - Chapter 4 - Tomcat Performance Tuning ) - JMeter's HTTP client is slower than that of AB or siege. You can use JMeter to  find out if a change to your webapp, your tomcat installation or your JVM, accelerated or slows the response times of the webpage however you cannot use JMeter to determine the server's maximum number of requests per second that it can successfully serve because of JMeter's HTTP client appears to be slower than Tomcat's server code.


http://books.google.com/books?id=vJttHyVF0SUC&pg=PA136&lpg=PA136&dq=%22maximum+number+of+requests+per+second%22++jmeter&source=bl&ots=i-5yv-tLh0&sig=0JnmfPtq2PwKWdEDkfSamUQMBEg&hl=en&sa=X&ei=6L0QT-3DGc6lsQKmio2LBA&ved=0CDIQ6AEwAg#v=onepage&q=%22maximum%20number%20of%20requests%20per%20second%22%20%20jmeter&f=false

I thought it was really valuable find :)
Manisha 

Get rid of ^M in VI editor

^M magically appears on scripts when we copy a file from windows to Linux OS after modification.

To remove the ^M characters at the end of all lines in VI use below command:

:%s/^V^M//g

The ^V is a CONTROL-V character and ^M is a CONTROL-M. When you type this, it will look like below command. In UNIX, you can escape a control character by proceeding it with a CONTROL-V.

:%s/^M//g

The :%s is a basic search and replace command in VI. Above command tells VI to replace all the ^M character globally with nothing and to escape ^M we need to type ^V.

Enjoy
Manisha

Thursday, July 19, 2012

Creates the appropriate HSSFWorkbook / XSSFWorkbook from the given InputStream

I waited enough for Sandip to write this blog since it was his find :).

Here is the problem we had, we have an application where user can upload the excels and our application process them. Our users were uploading 2007 formatted excel file in .xls format or vices-verse. Microsoft excel 2007 is backward compatible but our POI libraries sometime gets confuse. DOM parser is smart enough to handle but when it comes to SAX we need to know the exact format of the file loaded. i.e. just checking the file extension might not be enough. So we have to look into input stream to find the exact loaded excel format of the file.

As usual I looked over the internet and could not find anything post how to do it. Since Sandip is lot more savvy with Apache POI he started looking into the Apace POI source code and found WorkbookFactory class (Factory for creating the appropriate kind of Workbook) has a help method which takes the inputStream and based on that creates an appropriate object. Look at the code below.

============================
/**
 * Creates the appropriate HSSFWorkbook / XSSFWorkbook from
 *  the given InputStream.
 * Your input stream MUST either support mark/reset, or
 *  be wrapped as a {@link PushbackInputStream}!
 */
public static Workbook create(InputStream inp) throws IOException, InvalidFormatException {
    // If clearly doesn't do mark/reset, wrap up
    if(! inp.markSupported()) {
        inp = new PushbackInputStream(inp, 8);
    }
   
    if(POIFSFileSystem.hasPOIFSHeader(inp)) {
        return new HSSFWorkbook(inp);
    }
    if(POIXMLDocument.hasOOXMLHeader(inp)) {
        return new XSSFWorkbook(OPCPackage.open(inp));
    }
    throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
}

============================

Good find Sandip
Manisha

JSP/Tomcat 6 "org.apache.jasper.JasperException: java.lang.ArrayIndexOutOfBoundsException"

We noticed "org.apache.jasper.JasperException: java.lang.ArrayIndexOutOfBoundsException" on our JSP pages in the morning. I have copied the entire exception stack trace below for reference.

My first guess was the jsp pages have some issue where we are trying to access some index which has not been assigned. But we realized it's the server which ran our of space. so the first exception was "
java.io.IOException: There is not enough space in the file system.".

Why tomcat has to throw such an non intuitive error message? Why?  Not sure if it's tomcat's fault or the underneath framework is messing up.

==============================================================
org.apache.jasper.JasperException: java.lang.ArrayIndexOutOfBoundsException
 org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 sun.reflect.GeneratedMethodAccessor209.invoke(Unknown Source)
 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
 java.lang.reflect.Method.invoke(Method.java:600)
 org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:269)
 java.security.AccessController.doPrivileged(AccessController.java:284)
 javax.security.auth.Subject.doAsPrivileged(Subject.java:727)
 org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:301)
 org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:162)

root cause
java.lang.ArrayIndexOutOfBoundsException
 java.io.OutputStreamWriter.write(OutputStreamWriter.java:250)
 java.io.Writer.write(Writer.java:152)
 org.apache.log4j.helpers.CountingQuietWriter.write(CountingQuietWriter.java:44)
 org.apache.log4j.WriterAppender.subAppend(WriterAppender.java:308)
 org.apache.log4j.RollingFileAppender.subAppend(RollingFileAppender.java:236)
 org.apache.log4j.WriterAppender.append(WriterAppender.java:159)
 org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:230)
 org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:65)
 org.apache.log4j.Category.callAppenders(Category.java:203)
 org.apache.log4j.Category.forcedLog(Category.java:388)
 org.apache.log4j.Category.error(Category.java:319)
 com.jpmc.tao.common.logging.Logger.error(Logger.java:167)
 com.jpmc.tao.presentation.controller.FrontController.process(FrontController.java:61)
 org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 sun.reflect.GeneratedMethodAccessor225.invoke(Unknown Source)
 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
 java.lang.reflect.Method.invoke(Method.java:600)
 org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:269)
 java.security.AccessController.doPrivileged(AccessController.java:284)
 javax.security.auth.Subject.doAsPrivileged(Subject.java:727)
 org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:301)
 org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:162)
 java.security.AccessController.doPrivileged(AccessController.java:251)
 java.security.AccessController.doPrivileged(AccessController.java:251)
 org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:706)
 org.apache.jasper.runtime.PageContextImpl.access$1000(PageContextImpl.java:71)
 org.apache.jasper.runtime.PageContextImpl$11.run(PageContextImpl.java:664)
 java.security.AccessController.doPrivileged(AccessController.java:251)
 org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:662)
 org.apache.jsp.default_jsp._jspService(default_jsp.java:54)
 org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 sun.reflect.GeneratedMethodAccessor209.invoke(Unknown Source)
 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
 java.lang.reflect.Method.invoke(Method.java:600)
 org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:269)
 java.security.AccessController.doPrivileged(AccessController.java:284)
 javax.security.auth.Subject.doAsPrivileged(Subject.java:727)
 org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:301)
 org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:162)
==============================================================

Any thoughts?
Manisha

Tuesday, July 10, 2012

How to be a Technical Leader

This is one question which is bothering me from a while. Honestly there are bunch of questions and this is the root of all.

1) What is leadership? Bossing over people?
2) How can I be leader and be technical at the same time?
3) Can I be a leader without becoming like those other people?

To me leadership should create an environment in which people feel empowered. An environment where one is free to talk, free to act and ask anything. Since each person is unique, can one style be application to entire team. Should leader choose different style of leadership for different teams? The biggest question to me is WHAT IS LEADERSHIP? Now a days I hear servant leadership term but what if your team is not capable to solve problems on their own. Will this style of leadership work in the mentioned scenario? I would assume no. I did not have very sweet experience.

To me a leader should be a good motivator. A leader should understand the problem and manage flow of ideas and manage quality too. Above all team should have FAITH in the leader and his leadership. There are lots of other factors which might come in play as a leader. May in next blog :)

Manisha

Saturday, June 30, 2012

Remove all unused imports with one click

I am almost month old in my new company. The project I am working has 5K approx warnings and majority of them are because of unused imports and unused variables (gurrrrr). So I was looking for someway to fix these minor things using some tool and guess what I found eclipse "Organize Imports (Ctrl+Shift+O) in most eclipse" can be executed at package or project level. Bingoooooooooo resolved around half of warning. Feeling better inside :)

Choose the package -> Right click -> Source -> Organize Imports.

Now to the next job of finding the tool for unused variables. Wish me luck
Manisha

Friday, June 1, 2012

Java Performance- Cookbook Part II

This is the continuation of my previous blog post. Last post I shared my chapter 2 notes and here we go with chapter 3.

This chapter provides an overview of the "Hotspot Java Virtual Machine" (VM) architecture and it's major component. There are three major components of the HotSpot VM. 
1) VM Runtime
2) JIT Compiler
3) and A Memory Manager (Runtime)

HotSpot VM High Level Architecture

HotSpot VM has a JIT compiler, client or server, is pluggable, as is the choice of garbage collector: Serial GC, Throughput, Concurrent or G1. VM also has a runtime (HotSpot VM Runtime) which provides services and common APIs to the JIT compilers and garbage collector. Runtime also provides basic functionality to the VM such as a launcher, thread management, Java Native Interface and so on.

VM yields high scalability by generating dynamic optimization. In other words it makes optimization decisions while the Java application running and generates high performing native machine instruction targeted for the underlying system architecture.

32-bit VMs are limited to 4 GB memory. It is important to note that the actual Java heap space available for 32-bit VM may be further limited depending on the underlying OS. For instance Windows OS the maximum Java heap available to the VM is around 1.5 GB. For Linux OS it is around 2.5 to 3.0 GB. The actual maximums vary due to memory address space consumed by both a given Java application and JVM version.

64-bit VM allows these systems to utilize more memory. However, with 64-bit VMs come a performance penalty due to an increase in size of the internal HotSpot VM's representation of Java objects, called Ordinary Object Pointers (OOPS), which have an increase in the width from 32-bit to 64-bit. 

Increase in width results in fewer OOPS being available on a CPU cache line. The decrease in CPU cache efficiency results in about 8% to 15 % performance degradation compare to 32-bit JVM. A new feature called compressed oops, which is enabled with the -XX:+UseCompressedOops VM command line option, can yield 32-bit JVM performance with the benefit of larger 64-bit Java Heaps. Java application realize better performance with a 64-bit VM using compressed oops than they achieve with 32-bit VM. Application executes faster with improved CPU cache utilization.

HotSport VM Runtime

VM Runtime encompasses many responsibilities, including parsing of command line arguments, VM life cycle, class loading, byte code interpreter, exception handling, synchronization, thread management etc.

a) Command Line Options
VM Runtime parses command line options and configures the HotSpot VM based on those options. There are 3 main categories of command line options.

1) Standard Options - These options are expected to be accepted by all JVM implementation and stable between releases.
2) Non-standard Options - These options are not required to be supported by all JVM and begin with a -X option.
3) Developer Options - It begins with -XX prefix.

b) VM Life Cycle
The HotSpot VM Runtime is responsible for launching and shutting-down the HotSpot VM. The component that starts the VM is called the "launcher" (java, javaw, javaws (java web start)). Below are the steps which launcher executes to start the VM.


1) Parse command line options, such as -client or -server, determines the JIT compiler to load.
2) Establish the Java Heap size and JIT Compiler Type.
3) Establish environment varibales such as LD_LIBRARY_PATH and CLASSPATH.
4) Look for MAIN-Class name from command line or from JAR's manifest file.
5) Create VM using the standard JNI method (JNI_CreateJavaVM).
6) Once the VM is created and initialized, the Java Main-class is loaded and launcher gets the Java main methods attributes from the java main-class.
7) Java main method is invoked in the VM using JNI method CallStaticVoidMethod passing it the marshaled arguments from command line.

Once the java program completes it's execution both method's and program's exit status must be passed back to their caller. The Java main method is detached from the VM using JNI's method DetachCurrentThread and  thread count decrements so the JNI can safely invoke DestroyJavaVM method.

Sorry could not finish typing my notes.. There are few lot more important stuff which I would love to remember from this chapter. More to come soon.

Manisha