Wednesday, 8 February 2017

gzip -x

Test subject is a linux LVM volume containing a usual minimal Linux root filesystem, I want to move it to another host. LVM does not give any special tool for moving volumes, so one has to use the "standard" unix commands, this one may be a good candidate:

dd if=/dev/vg/lv-1 | ssh other-server "dd of=/dev/vg/lv-1"

Now of course this is simple, but unfortunately not that good, because if this volume is 2 GB, then we generate 2 GB network traffic, we can try with compression:

dd if=/dev/vg/lv-1 | gzip -N | ssh other-server " gzip -d | dd of=/dev/vg/lv-1"


And now the question is: what should that N be? I have always used 9, but is it that much better than 8?

Centos 7 root filesystem

















A blank ext4 filesystem

Now this is the case, where the compression is quick yet I am not satisfied. The problem is that dd does not have a way to skip empty blocks, those too will be transferred. Can gzip help?






That nice drop between 3 and four is interesting. But is it worth waiting 3 seconds to save 7MB transfer? If it does, then your network sucks. Most of the time gzip -1 will do just enough.



Conclusion

More than 5 does not improve significantly on the compression, but comes with a great cost.

gzip -1 may be the best option when we know that the filesystem is mostly empty.

But in any case, an optimization should look for something better than gzip :)

Sunday, 26 April 2015

Just a little VM tuning: Memory and CPU saving with KVM + KSM

This topic will be somewhat unusal from a java junkie like me, but hopefully interesting for those who are interested in cloud computing and virtualization. To make it easier to understand for everyone, I will start from far-far away, please just skip ahead if you feel like this is nothing new for you, there may be some interesting pieces of information later on.

The basics

This may not be something new for you, feel free to skip ahead to the hypothesis if you know Linux and virtual memory handling.

Virtual Memory


Modern computers break up the memory into pages. When your program reads or writes a memory address, that translates to a page and through the paging table to a physical address.

This is the so called virtual memory and allows swapping, the OS can just swap out some pages from memory to a larger and cheaper storage (typically a disk). When a page is referenced that is not in the memory, the hardware generates an interrupt and the OS takes over, loads the memory page and gives back control to the program. But not only that is possible...

Linux have a small module built in called Kernel Samepage Merging or shortly KSM This module was actually written by the same guy who wrote KVM, and very likely with KVM in mind, but any other system can benefit.
I'd recommend to read the doc in the kernel documentation, but this is what it does in a nutshell:
  1. Periodically checks memory pages
  2. If two identical pages are found, then they are merged and marked with COW (copy on write) -this is because KSM has no idea what the page is used for, it just merges whatever it finds
So if you have two VM's, both running the same OS, then most pages of the kernel and programs can be shared between the two VM's and they will never know. This can save quite some memory and allows big memory overcommit in virtualized environments, if you accept the price:
  1. KSM takes CPU time. If you have a lot of memory, then it will take a lot of CPU time.
  2. Basically it just does not know when to stop, it just keeps running, so additional software is used to manage it. Like ksmtuned.

Cache


While CPU has become faster and faster until the second half of the 2000's, the memory speed did not really keep up with it and CPU's started to use ever growing cache. The cache is in the CPU, it is very quick, but it's size is still limited, even Xeon CPUs have 10 MB of cache, typical desktop CPU's have 1-2 MB.

The hypothesis

Since the cache is small, switching to another VM in a virtualized environment should cause a little performance loss, since the cached pages of the kernel in the VM1 need to be replaced with the actually identical pages of VM2.
The second part of the idea is that KSM could help here by eliminating that performance loss. When pages are shared between the operating systems of the VM's, then a cache miss is less likely after another VM takes the CPU time.
Therefore once pages are merged with KSM and KSMD is turned off, switching between different VM's will be less expensive and respone times improve.

KSM could not only be a memory-saver, but also a CPU-saver.

Test

To test the idea, I prepared 12 web server VM's and one load-balancer. All of them run fedora 20 operating system. The web servers run apache httpd, the load-ballancer runs HAProxy, with more or less default settings. Each VM have 256 MB RAM and a single CPU.

The test host is an Intel NUC D34010WYK with Core i3 CPU (important factors for the test: hyperthreading is enabled, cache size is 3MB) and 2x 8 GB DDR3-1600 RAM.

Nice little box, they could have called it Intel NUKE :)

To generate load, I use the simple apache benchmark (ab) command line utility from my laptop. It is not really relevant, my laptop is a wreck, perfect motivation to speed-optimize software.
Load command:
ab -n 100000 -c 8 http://192.168.1.104/icons/poweredby.png


(This is the small "Powered by Fedora" banner)

Results





Comments, conclusions

The results with VM numbers > 4 seem to prove the theory, but I was surprised to see the performance loss when the number of VM's was less than 4. I do not have an explaination for that yet.
I suspect that the fall of the no-ksm (blue) curve shows the increase of cache misses, it flats out after 10 VM's, basically by then cache misses are becoming so frequent that they can not be a lot more frequent.

Basically on each CPU and memory you will get different values, also different OS'es and programs will generate different values, the intersection may be somewhere else, but the form of the curves should be similar.

TODO

I think it would be interesting to repeat the test with hyperthreading turned off and see how the curve changes.

Monday, 29 December 2014

Performance comparison of JAX-RS 2.0 implementations

I was looking for a short comparison of JAX-RS 2.0 implementations and I could not find one that compared their response times, so I had to do it myself.

I'm sure some will argue that speed is not the most important characteristic of a REST framework, and things like stability, maturity, advanced features matter more. Well, they may or may not be right, but in any case performance is an important aspect.

Tested frameworks


While surely I missed someone's pet JAX-RS framework, I think I have selected the three mainstream ones:
  • CXF - I use it for several years, I have always been a happy user
  • Jersey - The reference implementation from Sun/Oracle
  • Resteasy - The JBoss implementation of JAX-RS

Test method

Project layout
  • I have implemented a single Hello service annotated with JAX-RS annotations, this is in a separate maven module and used by framework-specific webapps.
  • All web modules are configured with Jackson 2
  • For simplicity, the services are registered in spring, however Jersey does not allow it the way Resteasy and CXF do
  • All tests are performed using Jetty 9.2.2.
  • For warm-up, I gave each test 1M requests on a single thread.
  • The test tool was the Apache httpd benchmark utility 'ab'. Each result was performed separately with 100.000 requests and 1,2,4,8,16 concurrent threads.
  • Source code of the tests available on github.
  • All tests performed on the same laptop. Fedora 20, Oracle Java 1.7.0_45, AMD E2-1800 wreck

Results




Comments

I was kind of surpried to see how much better Resteasy performed and looked under the hoods of the frameworks to figure out what they do when I hit the URL. Well, after a few hours of digging I concluded that most of it is the abstraction layer. CXF is not only for REST but also for JAX-WS, it can consume messages with JMS and so on, and that abstraction layer takes it.

I have also implemented a very thin experimental JAX-RS 2.0 implementation to see how that works and with the same tests it outperformed the mainstream frameworks with roughly 2000 request/second. I would not dare to replace CXF with that :-D but kind of interesting to think about it.

Sunday, 11 May 2014

Hello Babel!

Lucas van Valckenborch's painting
source: commons.wikipedia.org
Even though the new Java 8 is out with long expected features like lambda expressions (closures, for simplicity), millions of software developers are looking for a better java.

There are tons of different reasons why you may consider switching to another language, performance is just one of those. I was interested in a very basic aspect of performance: how quickly is the code compiled from the source code able to create a string.

The tested languages are:
  • Java 1.7
  • Scala 2.10.0
  • Kotlin 0.7.271
  • Groovy 2.3.0
  • Clojure 1.3.10
I am really missing ceylon from the list, but it does not have a maven plugin, it is not even published to maven central or any public repositories, so I skipped it.

And the results are...


Well, the single-threaded performance and simple String handling may not be a good reason to switch at the moment. As you see, java far outperformed the other languages. If you look into the bytecode generated by Scala, Groovy and Clojure, the reason is obvious. It does so many things, that it just can't perform quick.
While Kotlin  performed only about half as quick as the code compiled from java, the problem is a bit harder to spot. So let me highlight the problem...

       0: new           #19                 // class java/lang/StringBuilder
       3: dup          
       4: invokespecial #23                 // Method java/lang/StringBuilder."<init>":()V
       7: ldc           #25                 // String Hello
       9: invokevirtual #29                 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
      12: aload_1      
      13: invokevirtual #29                 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
      16: ldc           #31                 // String !
      18: invokevirtual #29                 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
      21: invokevirtual #35                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
The problem is that Kotlin compiler generated code to append method accepting Object arguments, while it is known that the argument is going to be String. Should be an easy fix, I registered a bug in Kotlin's bugtracker.
Update: I played a little with it and found where the code is generated in the compiler. The compiler with my patch generated line by line the same code as the java compiler, and therefore it performed the same.

The truth is, even javac generated suboptimal code, it could be beaten. And next time I will give it a try.


Test environment:
  • AMD E2-1800
  • Oracle java 1.7.0_45
  • Fedora linux
Test code on github.