vendredi, janvier 22, 2010

Fixing performance issues in your application

I was lucky enough to assist to a presentation done by Kirk Pepperdine last Wednesday. I won't present Kirk (you can check his résumé here ), it's enough to say that he is well known as a java performance Guru.

The presentation was conducted in two parts, the first one was a Q & A session, the second part was about debugging live an application that was carefully slowed down by introducing bugs in it.

Preamble
Let me talk about the application first : the guys who invited Kirk (AFAIK, it was an 'extra', provided as a bonus following an internal presentation they paid for. Thank to Xebia for having shared this presentation with external people) have prepared the application (the well known and useless Pet Clinic) by adding some of the anti-pattern they have met when doing consulting for many of their clients. Kirk had no clue about the bugs that have been injected.


Q&A
It was asked us to provide some questions when we registered, and Kirk answered them extensively. Here are some of the Q and A I remember of :

Q : Which GC should we use ?
A : The one which works. Usually, just focus on your application, you'll not need to pick a specific GC .

Q : What do you think about other languages like Groovy, Scala, wrt performance ?
A : It's irrelevant. Picking a language to develop your application should not be a matter of performance only. 'Whatever works' is the way to go. If you want to build an application fast, and if it's not expected to be heavily loaded, then even php is a good choice.

Q : What tools to you use to check for performance bottlenecks
A : A few : a system monitor, HP-JMeter, an VisualVM

Q : How can you best write an application which depends heavily on concurrent code ?
A : Don't use any synchronization. There are ways to avoid synchronization, based on state machine theory. (pointers needed here ...)

Q : What is the ratio of GC problems you have to deal with when working for a client ?
A : Around 40%. Assuming that I'm the last hope for many of my clients, it's may be an irrelevant number. Usually, people successfully fix easier issues themselves.

Q : Do you check the code when you start tracking some performance issue ?
A : Never. I'm not a coder, I don't have time to go through thousands of line of code. I just spot the place in the code which has problem.

Q : Managers don't let me adding some traces in the application on production… What should I tell them ?
A : Managers know the difference between a slow application and a dead application. Do what you have to do, or find another client. (in other words : you don't cure cancer with aspirin...)

Q : Which profiler do you use, or prefer ?
A : YourKit: it's simple and efficient.

The most interesting presentation I have seen in years. I actually learn things in an area I thought I was efficient…

Live demo

What was the crux about this part was the processes Kirk adopted to point out the problems in the code.

Step 1

First, he asked for a baseline to work on. Namely, you should have a scenario which demonstrates the kind of real performances issues a real client perceives. Improving some application which is already perceived as working is a waste of time, energy and money. Without a base line, you also have no way to check that you have improved the application. Last, not least, define your expectations, otherwise, you won't meet them ! So here, the team has defined a JMeter test, and defined the expected response time for each page.

Step 2

Second, run the baseline scenario, and measure the response time, plus a few other counters :
- CPU (users and system)

That's it, nothing more. Here, the code has not been even checked. The only thing Kirk did was to remove all the tuning for the JVM, like the memory min and max size, and every other premature configurations.

The rational is that you have no idea at this point if those parameters have any effect, but they for sure have an impact, probably polluting the results.

Looking at the CPU consumption and response time (90%CPU, around 5% system), with an average of 10s per page, it was clear the application has a performance issue, but there was no clue about what's going on yet.

Step 3

Then he checked the way the GC was running. He added some instruction on the JVM setting to generate some GC traces, run the application for a few minutes, then checked the logs ("You have to be patient ! Memory leaks may take a while to be noticed.")

A quick look at the metrics shown that the GC was eating 13% of the whole CPU. Way too much.

Step 4

Kirk now decided to connect to the running application, using VisualVM. The idea was to check the way objects were allocated. After a few minutes of tests, the allocated objects graph shown that we have a linear increase over time, which means a memory leak.

Finding the memory leak was a matter of minutes : find an application object (no need to check a Java object like byte[] or String : "Java collection objects don't leak…"). What is the key for Kirk is the number of generations an object survived : the higher this number, the more likely this object is leaking. Very new to me.

As a side note, he also said that many of the existing tools don't provide this generation number. They base the detection of leaking object on delta between snapshots. Not convenient.

Then you can check where the object was allocated checking the stack trace, an now, look at the code.

At this point, the important lesson is : just look at the code when you know in which method you have a problem.

(the application had another memory leak he found too, using the very same approach)

Another lesson : he asked to remove the caches in the code, instead of blind-guessing what was wrong with those caches (they were leaking). His moto was : "Why would you optimize your code by adding cache when you have no idea about what's going wrong in your code ?"

Step 5

Once this initial problem was fixed, he re-runs the test, and he saw that the CPU was not going any upper than 50%. Very wrong when the response time was still awful. In this case, the System CPU was high (the ration between user and system should be around 5-10%/90-95%).

What does it mean ? Contention. How to find where we have contention ? Easy : generating a thread-dump.

No fancy profiler, no long source reading, just a thread-dump.

It immediately shown that only two threads were used to deal with 50 concurrent clients requesting the application.

A quick tuning on Tomcat (number of threads accepting requests), and we moved to the next step.

Step 6

One last measure shown now that we had much better performances, but with a very high CPU system usage : around 20%.

Same action here : thread dump, look at the blocking threads, go to the portion of code where the thread was waiting. A bad thread.sleep( 100 ) was found in the code.

And it was over for the demonstration : 2 hours to fix bugs that would have took days and days for most of us!

Conclusion

In two hours, he made the application running way faster, simply by using a couple of tools, and without reading the code.

Impressive.

Thanks to Kirk Pepperdine, Cyrille Le Clerc and Xebia !

Follow up
I have forgotten a few things :
  • at some point after stet 4, GC went up to 65%. Kirk suspected that some part of the code was calling the GC. You bet !
  • after the presentation, Kirk said that the very first step is really to catch all the GC problems first, as they will probably hide other problems.

samedi, janvier 09, 2010

vendredi, décembre 18, 2009

Apache Directory WebSite statistics

Here are some stats we gathered yesterday on http://directory.apache.org site. I just checked the OS of our visitors (thanks to Google analytics), and the browsers.













































OS
2007 2008 2009 Last month
Windows 79,45% 76,10% 72,16% 68,92%
Linux 13,61% 15,11% 16,89% 18,74%
Mac OS 6,27% 8,13% 10,21% 11,61%
SunOS 0,23% 0,28% 0,35% 0,33%
iPhone 0,01% 0,07% 0,09% 0,10%


W$ is losing some ground every year. It's even more obvious on the Browser category. Last month, the stats are :

  • FF (all versions) : 59,19%

  • IE (all versions) : 21,31%

  • Chrome : 7,92%

  • Safari : 6,14%

  • Opera : 2,58%





















IE stats
2007 36,42%
2008 30,15%
2009 23.99%
Last month 21,31%



Of course, it's a bit biased, because we are addressing very technical people, who are most certainly on the bleeding hedge. My mom does not browse Directory.a.o, that's for sure ! However, this is relevant, considering that they are the ones who create tomorrow's IT !

mardi, décembre 08, 2009

Come on Eclipse, you can do it !!!

It's now years Eclipse has introduced the Working Set feature. It's really helpful, but sadly, it's still a damn bad hack : you can't have more than one project with the same name.

For instance, I want to work on MINA 2.0 *and* MINA 3.0. Both of them will have the same project names, as I see no reason to change them. I have a mina-core java project in both sets, and no way to have them both in their associated working set in eclipse.

This is not only useless, at some point, it's ridiculous. I can't imagine that in 8 years, no one in eclipse understood that it's a major problem. It seems to me a very basic concept...

It's just like, hmmm, I have a new born baby, and I want to name him/her John/Dorothy : WRONG ! Some one in another family has already used those names... Get it ?

How stupid ...

Hopefully, it might be possible in Eclipse 4.0 : http://wiki.eclipse.org/Eclipse_4.0/Wishlist (bullet #6)

jeudi, novembre 26, 2009

DELL D620, R.I.P...

Last week, I installed the latest Ubuntu 9.10 on my Dell laptop. I had to revert to 9.04, due to some huge instabilities (I was using Ubuntu 8.10 for more than a year before).

So far, Ub9.04 was just fine, I didn't had any problem, beside the problematic support for Flash on FF3.0, and a painfull process eating 45% of my CPU : something supposely helping disabled people to use the computer. Once this process has been killed, everything went fine.

Too bad. Yesterday evening, I had some issues with my power cord (it was said not to be recognized by the system), with the consequence of the battery not being on charge, and my processor crawling at half speed. It lasted until this morning, then it worked again. I already experimented this harsh in Oakland last month...

Tonite, I left the office after having suspended my laptop, and when I arrived at home, it was dead. Switch on, some leds were blinking, I heard the disk spinning a few seconds, and that was it. Nothing on the screen... Hopefully, I did a full backup last week before switching my system !

So I bought a Mac book pro, a 15", 2.8Ghz, 320Gb 7200rpms, 4Gb mem. 1400 €, refurbished. It is supposed to arrive either tomorrow or monday.

My Dell was 3 years old, I didn't paid for it (insurance paid for it, my previous one has been stolen), so it's the first time in 5 years I buy a new laptop. Not too bad ...

If some one is interested in spare pieces, I have 1 Gb of mem, a 160Gb 2.5" disk, a battery. Just tell me !

jeudi, novembre 19, 2009

A F**** disgrace !

So we did it. For the forth time in a raw, France will be present in South Africa. But what a shame this match was !

I must say that I never saw the french soccer "team" (at this point, I don't know if we can call it a team) played so bad. Ever. We have talented players, probably worth half a billion euros if you cumulate their value, and we can't make them play a decent game ?

Last, not least, this ends with an insult to fair play. Really, Irish players deserved a better end...

My apologize for all the irish, this was unfair. Shame on Henry, shame on the referees, shame on the FIFA presidents who keep thinking that video is just good for watching pr0n movies, probably... And shame on Domenech, who admitted to sometime pick players based on astrology studies... ("all parameters have to be considered and I have added one by saying there is astrology involved"...)

This is not soccer. Just a pathetic parody...

mercredi, novembre 18, 2009

Upgraded to Ubuntu 9.10, updated

So after the ApacheCon event, I decided it was a good timing for an upgrade of my 8.10 Ubuntu driven laptop. I was quite pleased with the previous version, except that wifi support was crappy at best, and decided not to upgrade to 9.04,

After a full backup (took 4 hours), installation took only 20 minutes, most of the time was spent to carefully define the partitions so that I keep my /home and /opt safe. It seems that the installer is not happy with a 500 Mb partition for / (it asks for 2Gb), but I went through, without any problem.

I have 9 partitions defined, and even if it sounds a bit too much, I'm quite pleased with this setup. At least, /home and /opt are separate partitions, just because I don't want to update, but prefer to go through a complete re-installation instead (I have been burnt once with an update which ended with a full re-installation , so better spare this time by doing a direct re-installation). Another benefit is that I was able to move from ext3 to ext4 for the formatted partitions.

Then came the reboot, and bang, all was pretty much ok !

I just had to let Ubuntu to update many of its packages, and to add a few missing icons in my menu (xchat, thunderbird). My eclipse shortcut weren't working anymore, too, but that was easy to fix.

Now, the bad : suspend wasn't working anymore. I spent 3 hours on the net checking for a solution (it seems that almost everyone have experienced the very same problem), and at the end, I just found an interesting information. With an NVidia device, once the new driver (185) has been installed, adding this line in xorg.conf did the trick (in red) :


Section "Device"
Identifier "Default Device"
Driver "nvidia"
Option "NoLogo" "True"
Option "NvAGP" "1"
EndSection


Don't ask me why, but it worked (Tm).

FYI, I'm using a Dell D620, a machine probably built and designed by former soviet union engineers : works well, looks ugly !

Update :
This version is probably the worst I ever used. CPU scaling does not work (I'm stuck at 1Ghz), Eclipse is crashing every now and then, many widgets remain stuck and can't be closed, sound does not work correctly, plus many little problems...

Release often, release fast, ok, but at least : release only when it's READY !!!