wtorek, 22 stycznia 2013

Java and RPi - continued


After I had installed both jdk (open and oracle's), I started wondering what is the difference between them, when it comes to performance. Recently I found some time in the evening and wrote simple tests, which are not very accurate, but give the general notion about performance.

Let's take a look into tests:
ArrayListAppendTest
List<Long> list = new ArrayList<Long>();
for (long l = 0; l <1000000; l++) {
   list.add(l);
}

ArrayListInsertMiddleTest
List<Long> list = new ArrayList<Long>();
for (long l = 0; l < 1000; l++) {
   list.add(l);
}
for (long l = 0; l < 10000; l++) {
   list.add(500, l);
} 

ArrayListSortTest
List list = new ArrayList();
for (int i = 100000; i > 0; i--) {
   String str = String.format("String nr %5s", i);
   list.add(str);
}
Collections.sort(list);

LinkedListAppendTest, LinkedListInsertMiddleTest, LinkedListSortTest are exactly the same as for ArrayList, but with subtle change...

DoubleAddTest
double val = 0.0;
for (int i = 0; i< 1000000; i++) {
   val += (double) i;
}

SinusTest
for(int i = 0; i < 1000000; i++) {
   double val = Math.sin((double) i);
}

StringAppendTest
String [] strings = new String[1000];
for (int i = 0; i < strings.length; i++) {
   strings[i] = String.format("Str %d", i);
}
String concat = "";
for (int i = 0; i < strings.length; i++) {
   concat += strings[i];
}

All tests *ISTest use various input streams, *OSTest - output streams.

When it comes to reflection tests, I tried to instantiate class using classe's newInstance method, in case of CallMethod test I'm calling one argument method using reflection. Simple...

For comparison I executed test on my ancient computer (Amd64 3500+). After that I compiled and run tests on Raspberry Pi using proper JDK.

And the resulst are... interesting (execution time is in ms):

Test nameJDK Oracle 1.7 @ AMD64 3500Oracle JDK 1.8 ea @ RPiOpen JDK 1.7 @ RPiRatio
ArrayListAppendTest 348,6 2632 10965,1 4,2
ArrayListInsertMiddleTest 72,6 285,7 1024,7 3,6
ArrayListSortTest 19,6 292,9 2394,4 8,2
BufferedFileOS 12,2 45,4 856,4 18,9
DoubleAddTest 0,2 35,8 390,5 10,9
FileISTest 25,3 183,8 231,5 1,3
FileOSTest 96,8 524,9 1618,9 3,1
LinkedListAppendTest 636,1 3393,7 16634,3 4,9
LinkedListInsertMiddleTest 23,3 99,5 2159 21,7
LinkedListSortTest 44,8 270,9 2225,9 8,2
ReflectionCallMethodTest 116,3 803,2 3496,7 4,4
ReflectionNewInstanceTest 55,5 202,5 542,4 2,7
SinusTest 268,6 562,5 3722 6,6
StringAppendTest 49,3 254,4 489,1 1,9

As you can see Open JDK in those test is far behind oracle's (in column Ratio you can see, how much slower Open JDK was :/).
I think I stick to JDK 1.8 ea...

sobota, 19 stycznia 2013

Java and RPi

If you gave a java developer a Raspberry Pi board, what programming language would he choose? You can make additional assumption, that he would be interested in writing a web application for RPi.
He would choose:
  1. C++
  2. Python
  3. or maybe Java?

Please send your answer anywhere you wish, in the meantime we start installing Java on RPi.

Most searches in google, if you want to find phrase "java raspberry pi", ends with plenty of links to articles, how to install the only proper one version, namely oracle java. But I would like to install for starters an open version.

Actually, installation process is not very sophisticated:
sudo apt-get install openjdk-7-jdk

When it's done, you can verify it quickly:
myuser@raspberrypi ~ $ java -version
java version "1.7.0_07"
OpenJDK Runtime Environment (IcedTea7 2.3.2) (7u7-2.3.2a-1+rpi1)
OpenJDK Zero VM (build 22.0-b10, mixed mode)

When it comes to web application, web container could be quite handy when running application. So far I haven't used jetty, therefore:
sudo apt-get install jetty8

When installation is completed (ignore an error while starting jetty) you need to update you startup scripts. But first, the only valid text editor should be installeed:
sudo apt-get install vim

Before we update a configuration file, let's see where java has landed:
update-alternatives --list java

and we get:
/usr/lib/jvm/java-7-openjdk-armhf/jre/bin/java

so java home is: /usr/lib/jvm/java-7-openjdk-armhf

Knowing all that important stuff we can modify a script file:
sudo vim /etc/default/jetty8

first thing, we shall allow jetty to start along wiht the system:
# change to 0 to allow Jetty to start
NO_START=0

after that, setting JAVA_HOME is proper thing to do:
# Home of Java installation.
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-armhf

If we completed our work now, jetty could be accessed only on... localhost. In order to access web server from anywhere, we need to set JETTY_HOST as well:
# Listen to connections from this network host
# Use 0.0.0.0 as host to accept all connections.
# Uncomment to restrict access to localhost
#JETTY_HOST=$(uname -n)
JETTY_HOST=0.0.0.0

Let's restart jetty:
sudo /etc/init.d/jetty8 restart

and what we get is default page for our jetty server:



However...
I would like to have the possibility to use oracle's jdk as well.
But...
It's not so easy for me... At least not on a wheezy version I have installed nor with jdk 1.7.0_10.
The problem is that I had wanted to have more efficient system, therefore I've installed wheezy with hardware ABI, and current oracle's jdk can use only slower soft-bloat ABI.

Fortunatelly, Oralce released "early access" jdk 1.8 with JavaFX included (you can get that package from http://jdk8.java.net/fxarmpreview/)

Whern you download it and unpack in /usr/lib/jvm directory, following command should be executed:
myuser@raspberrypi ~ $ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.8.0/bin/java 1
myuser@raspberrypi ~ $ sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.8.0/bin/javac 1

One more thing to - setting which java alternative should be used:
myuser@raspberrypi ~ $ sudo update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-7-openjdk-armhf/jre/bin/java   1063      auto mode
  1            /usr/lib/jvm/java-7-openjdk-armhf/jre/bin/java   1063      manual mode
  2            /usr/lib/jvm/jdk1.8.0/bin/java                   1         manual mode

Press enter to keep the current choice[*], or type selection number: 2

myuser@raspberrypi ~ $ sudo update-alternatives --config javac
There are 3 choices for the alternative javac (providing /usr/bin/javac).

  Selection    Path                                         Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-7-openjdk-armhf/bin/javac   1063      auto mode
  1            /usr/lib/jvm/java-7-openjdk-armhf/bin/javac   1063      manual mode
  2            /usr/lib/jvm/jdk1.8.0/bin/javac               1         manual mode

Press enter to keep the current choice[*], or type selection number: 2

Version check is simple:
myuser@raspberrypi ~ $ java -version
java version "1.8.0-ea"
Java(TM) SE Runtime Environment (build 1.8.0-ea-b36e)
Java HotSpot(TM) Client VM (build 25.0-b04, mixed mode)

Last thing to is is changing jetty configuration. I did that, and for now everything seems to work fine.

sobota, 5 stycznia 2013

New Year's Resolution

New year has come and with many New Year's Resolutions I've made one affecting this blog, namely I would like to try writing my posts in English (naturally I would like to translate all previous entries as well).
Why? There are many reasons. First of all I have to improve it. Not only my blog naturally ;) I haven't used written English for a long time (come on, short e-mails at work do not count). Besides, I really like this language and don't treat it as a tool.
Moreover, maybe I write something valuable one day... I that case more people could use that information (whatever).

At least my primordial assumption that this blog serves mainly me, is still valid.

Enjoy!