Sunday, November 9, 2014

Java 8 ‘Stream’ Performance Test

 

I have been recently testing out new features in JDK8. I think one of the most interesting feature which is available other than Lamda is Stream (java-doc). In summery it gives you a good way to do functional style of programming on Java collection, also it has something extra, it allows you to run operations in serial or parallel which in return allows developers to easily code parallel programming without needing to worry about complex programming logic.

I wanted to test the Stream performance for that I’m going to use a simple loop counter (source-code)

1) Normal (Serial)

2) Synchronized Object reference (Parallel)

3) Synchronised Array reference (Parallel)

4) Atomic (Serial)

5) Atomic (Parallel)

6) Stream (Serial)

7) Stream (Parallel)

8) If the Developer puts in the effort

Following is the results when running it from my laptop

image

As you can clearly see, a good developer can right a fast code, but its a game between Simplicity vs Performance.

Thursday, March 27, 2014

Are You Senior or a Junior Software Engineer ??

 

This is a not a test, it’s just for you to understand how with experience your programming mind changes.

Background, couple of days ago I had to make an urgent fix on one of our legacy system, so I checkout the code and started looking at it. I did not have a local deployment and did not have any time to test it locally had to make the change and test it on staging server and deploy it.

After I finished the changes I could not stop wondering how many places I could have gone wrong on the fix and messed up everything, I’m sure if I was just starting to programme I would have messed it up.

I will take you through the code and the changes and every time before going to the next section spend some time and try to analyze the code to find any issues.

Old code, Problem was on every element on the list we were doing a time consuming task.

public void doUpdate(List list) {

for (int i = 0; i < list.size(); i++) {
String sql = (String) list.get(i);
int count = doSomethingTimeConsuming(sql);
System.out.println(count);
}
}

Solution : I was able to find a alternative method which takes a String[] as parameter which can do the job in a batch, the method requires none Null and none Null element array

public void doUpdate(List list) {

String[] array = new String[100];

for (int i = 0; i < list.size(); i++) {

String sql = (String) list.get(i);
array[i % 100] = sql;

if (i % 100 == 0) {
int count = doSomethingTimeConsuming(array);
System.out.println(count);
}
}
}

Before looking down, spend some time and analyze the code


 


 

public void doUpdate(List list) {

String[] array = new String[100];

for (int i = 0; i < list.size(); i++) {

String sql = (String) list.get(i);
array[i % 100] = sql;

if (i % 100 == 0) {
int count = doSomethingTimeConsuming(array);
System.out.println(count);
}
}
// wright all the unwritten content
doSomethingTimeConsuming(array);
}

 


 


Were you able to identify the above issue?, okay lets continue to the next step, before going down spend some time and analyze the code even better

public void doUpdate(List list) {

String[] array = new String[100];

for (int i = 0; i < list.size(); i++) {

String sql = (String) list.get(i);
array[i % 100] = sql;

if (i % 100 == 0) {
int count = doSomethingTimeConsuming(array);
System.out.println(count);
}
}
// wright all the unwritten content
final int remainder = list.size() % 100;
final String[] dest = new String[remainder];
//avoid doing the task on already completed elements
System.arraycopy(array, 0, dest, 0, remainder);
doSomethingTimeConsuming(dest);
}

 


 


Did you get why we needed to do that?, OK again, before clicking on next analyze the code for more improvements.

public void doUpdate(List list) {

if (list.isEmpty())
return;

String[] array = new String[100];

for (int i = 0; i < list.size(); i++) {

String sql = (String) list.get(i);
array[i % 100] = sql;

if (i % 100 == 0) {
int count = doSomethingTimeConsuming(array);
System.out.println(count);
}
}
// wright all the unwritten content
final int remainder = list.size() % 100;
final String[] dest = new String[remainder];
//avoid doing the task on already completed elements
System.arraycopy(array, 0, dest, 0, remainder);
doSomethingTimeConsuming(dest);
}

 


 


 


Someone might have even done the below code, but it depends on the system performance and the overhead of introducing a List will bring in.

public void doUpdate(List list) {

if (list.isEmpty())
return;

final int batchSize = 100;
List tmpList = new ArrayList(batchSize + 1);
for (int i = 0; i < list.size(); i++) {

String sql = (String) list.get(i);
tmpList.add(sql);

if (i % batchSize == 0) {
int count = doSomethingTimeConsuming((String[]) tmpList.toArray());
System.out.println(count);
}
}
int count = doSomethingTimeConsuming((String[]) tmpList.toArray(new Object[list.size() % batchSize]));
System.out.println(count);
}

 


Likewise I’m sure everyone with experience would think about all the usecases before even compiling the code.

Thursday, August 29, 2013

Programming Is An Art

 

When I started programming I used programming language as a tool to solve problems. I could care less about the architecture or the beauty of the solution I came up with; long as the programme worked I was happy and satisfied.

After a few years everything changed I realized that programming is more than that. It is a way of expressing yourself. Every time you write a programme you put a bit of yourself into it, your proud of what you have written and it’s something you can call it your own. This is “ART” The expression or application of human creative skill and imagination.

If you look at an experienced programmers code, you can see a structure, a pattern (not the dreadful design patterns), but how he is naming the variables, methods, classes, solutions he comes up with, how the flow works, so on. Over the years the programmer’s expression changes, even if he read a tons and tons of books about coding style, best practices and design patterns, his code will always have a personal touch, I have seen some of my colleagues code which I think is complicated but that does not mean it’s bad coding, it’s just a personal touch that he has, something that looks complicated for me would look different to another person.

Sure, all developers (most I know) we read tons and tons of best practices books, but none of us adopted a practice or style fully. I honestly think that our coding style and practice is a mix of things we learn and personal touch which we have gained over the years.

I sometimes wonder reading best practice books does it actually help us develop an efficient code or is it actually blocking us from being an Artist.

Sunday, July 14, 2013

Software Engineers Reactions

 

Thank you #Brayan for sharing the content.

When I show the boss that I have finally fixed this bug

When the project manager enters the office

When I'm deploying code to production

When I try to fix a bug at 3 in the morning

When my regex returned exactly what I expected

When a friend of mine asks me to fix his website built with Joomla

When I'm told that the module on which I have worked all the week will never be used

When the code that I have not tested on dev works perfectly in production

When the sales people announce they have sold our product to the customer

When I apply a new CSS for the first time

When sysadmin finally gives us the root access

When I launch my script for the first time after several hours of development

When I go off for the weekend while everyone else is still trying to fix bugs

When the app goes into beta and the first bug reports arrive

Wednesday, July 10, 2013

Some of the new features in JDK 8

 

Oracle has really pushed for JDK 8 release, trying to keep to their word of releasing it within two years. We are currently at M7 (b94) which was released on 2013/06/13 I think its right about time to talk a bit on some of the interesting new features which are available.

  • Remove the Permanent Generation
  • Lambda expressions
  • Parallel Array Sorting
  • New Date & Time API
  • Define a standard API for Base64 encoding and decoding
  • New Javascript engine.

 

Remove the PermGen

The PermGen (Permanent Generation) space has been completely removed and replaced by a new space called Metaspace.

The consequences of the PermGen removal is that the PermSize and MaxPermSize JVM arguments will be ignored with warning messages if present when starting the JVM

However that doesn't mean you won't have to worry about the class metadata memory footprint and it does not eliminate class and Classloader memory leaks (if there any). Most allocations for the class metadata are now allocated out of native memory. By default class metadata allocation is limited by the amount of available native memory.

I really think this is a bad idea, currently if do have a memory leak in PermGen we end up only crashing the JVM and we can easily restart the JVM but if we end up filling the native memory we might end up crashing the server, but a new flag is available (MaxMetaspaceSize), allowing you to limit the amount of native memory used for class Metadata. The Metaspace will dynamically re-size depending on the application demand at runtime. There is obviously some garbage collection involved but I don't think that there is much to worry about here unless you have a problem of  Classloader memory leak. We just need to wait and see what are the new problems this is going to introduces.

 

Lambda expressions

This is something I was really looking forward to for couple of years now, I think this would be the Scala killer Winking smile

Lets get something straight here, Lambda does not let you do something new which cannot be done in JDK 7 < it just gives you a better (less lines) way to represent a block of code. Until JDK 8, the way to achieve processing of a method via another method is to pass an object. which is similar to a callback method or pass an anonyms override object.

   1:  action.execute(new Runnable() { 
   2:      @Override 
   3:      public void handle(Event event) { 
   4:          System.out.println("Hi There !!"); 
   5:      } 
   6:  }); 




The issue with callbacks is that it leads to verbose code. Like you would write 5 lines of code when the real job is in 1 of these 5 lines.


With JDK 8 you will have something similar to the following :


   1:  action.execute( 
   2:      event -> System.out.println("Hi There !!") 
   3:  ); 



You can do method referencing, meaning you can pass a method as an argument to another method. Method references work differently between static and non static methods.


public class Utils implements Comparable<MyObject> { 
    
    public static int compareByLength(String in, String out){ 
        return in.length() - out.length(); 
    } 
    
    @Override 
    public int compareTo(MyObject obj) {
        return 1
    }
}
 
public class RunThis {
 
    public static void main(String[] ar) {
        String[] args = new String[] {"Microsoft: Rocks", "Linux: Wow", "Apple: Sucks"} 
        
        Arrays.sort(args, Utils::compareByLength);
        Utils util = new Utils();
        Arrays.sort(args, util::compareTo);
    }
}



There are many other things to discuss about lambda, but that’s for another post.


 


Parallel array sorting


Arrays#parallelSort this was introduced newly in JDK8


Arrays#parallelSort uses Fork/Join framework introduced in Java 7 which assigns the sorting tasks to multiple threads available in a pool and joins them in the end


Following methods are available :


parallelSort(byte[] a) 
parallelSort(byte[] a, int fromIndex, int toIndex) 
parallelSort(char[] a) 
parallelSort(char[] a, int fromIndex, int toIndex) 
parallelSort(double[] a) 
parallelSort(double[] a, int fromIndex, int toIndex) 
parallelSort(float[] a) 
parallelSort(float[] a, int fromIndex, int toIndex) 
parallelSort(int[] a) 
parallelSort(int[] a, int fromIndex, int toIndex) 
parallelSort(long[] a) 
parallelSort(long[] a, int fromIndex, int toIndex) 
parallelSort(short[] a) 
parallelSort(short[] a, int fromIndex, int toIndex) 
parallelSort(T[] a) 
parallelSort(T[] a, Comparator<? super T> c) 
parallelSort(T[] a, int fromIndex, int toIndex) 
parallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c) 




New Date & Time API


Nothing much to talk about here, but only the fact most of the current features available in JodaTime API is available natively in JDK8


 


Define a standard API for Base64 encoding and decoding


Again nothing much here everything available Base64 library (part of apache commons) So this feature is simply to implement a standard API for Base64 and to avoid people to have to use unsupported classes such as sun.misc.BASE64Encoder and sun.misc.BASE64Decoder.


 


New JavaScript engine


JDK 8 will juice JavaScript performance on the Java Virtual Machine using Project Nashorn, a Javascript engine that will use the JVM libraries and will replace the existing Rhino engine. Design and implement a new lightweight, high-performance implementation of JavaScript, and integrate it into the JDK. The new engine will be made available to Java applications via the existing javax.script API, and also more generally via a new command-line tool.

Sunday, June 2, 2013

Programmers vs Rockers

One of my friend pointed out that I have stopped writing blogs for awhile, so this is me starting again.

This is a comparison between “Rock Stars” and “Programmers” There was a blog post similar to this couple of years back but this is a more detailed comparison.

 

Comparison

Rock Star

Programmer

Looks

clip_image002[10]

clip_image004[10]

Fashion Trend Setters

We start it

What is fashion trend? Never heard of it.

Charisma

Yes

Yes

People asking for Autograph at the street

Yes, all the time

Not likely

# of Twitter followers

Millions

Thousands, but we came up with Twitter.

Friday nights

Night out drinking.

I have work tomorrow.

Instrument

Microphone/Guitar/Drums/other

Microphone + computer + projector

Mobile phones

My phone is not working; I’m buying a new one.

Boot the device in Safe mode and root the OS

Writes

Lyrics

Code

Shows/Talk attendance

Tens of Thousands

Hundreds

Tour the world

Few months ago

Last year I think.

Owns

Multiple cribs and a private jet

Multiple 24” LED displays with SSD hard disk with multi VM

Way to the Top

X Factor / American Idol

Hard work for years (Simon Fuller, if you read this, what about a “Programming Idol” show?)

Drugs

Yes a lot

No

Earnings

Millions of Dollars

Thousands of Dollars

Sex

So much, they regret it later

Private information

Can bring the world to a standstill.

No

Yes, Who do you want us to hack?

Has two eyes, one mouth one nose and two hands

Yes, most of them.

Yes

Thursday, October 13, 2011

Java File Handling



Lately I been working on a Java file manipulation project and I came across an issue which I never expected.

This is a sample code.

public class FileReader {

    public static void main(String[] args) throws IOException {
        new FileReader().fileDelete();
    }

    public void fileDelete() throws IOException {

        final String fileLocation = System.getProperty("user.dir") + "/test.txt";

        File fileCreate = new File(fileLocation);
        final boolean isFileCreated = fileCreate.createNewFile();

        if (isFileCreated) {
            System.out.println("File Successfully Created");
        } else {
            System.out.println("File Creation Failed");
        }

        final BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileLocation));
        for (int i = 1; i <= 5; i++) {
            bufferedWriter.append("Line : " + i + "\n");
        }

        final BufferedReader bufferedReader = new BufferedReader(new java.io.FileReader(fileLocation));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }

        File fileDelete = new File(fileLocation);
        final boolean isDeleteSuccess = fileDelete.delete();

        if (isDeleteSuccess) {
            System.out.println("File Deleted Successfully");
        } else {
            System.out.println("File Deletion Failed");
        }
    }
}
Assume the file test.txt does not exists when running the programme. Can you predict the output of this programme? 
If you have thought it will print

File Successfully Created
Line : 1
Line : 2
Line : 3
Line : 4
Line : 5
File Deleted Successfully


If you have thought a bit more you might have predicted it will print

File Successfully Created
File Deleted Successfully



You’re completely wrong at least on a Windows, both the above outputs are wrong. Actual output on windows is "File Deletion Failed". It took me some time to actually realize the issue when my programme dint work as expected. As for the part of the Line 1 .. 5 not printing that’s straight forward I dint close the bufferedWriter. The problem here is that if you read or write to a file and you don’t close the stream you cannot delete the file in windows. Even when you’re actually creating a new File object it will still not let you delete the file and to make things worse it does not give you any other explanation except for Boolean false Imagine searching for this in thousands of line. So guys whenever you’re reading or writing to streams always remember to close them.

Friday, September 2, 2011

CSS Hacks For Every Browser

There are many CSS hacks that I have come across while developing web applications, mostly used one is the IE's conditional comments to apply classes to the body tag but there are some situation where you will want to add conditional CSS within the stylesheet, specially if your working with Vaadin

The following is a documentation of browser specific CSS style attribute I've seen. These CSS will help you better make specific changes to IE, Firefox, Chrome, Safari and Opera from within the CSS.



/***** Selector Hacks ******/

/* IE6 and below */
* html #uno { color: blue }

/* IE7 */
*:first-child+html #dos { color: blue }

/* IE7, FF, Saf, Opera */
html>body #tres { color: blue }

/* IE8, FF, Saf, Opera (Everything but IE 6,7) */
html>/**/body #cuatro { color: blue }

/* Opera 9.27 and below, safari 2 */
html:first-child #cinco { color: blue }

/* Safari 2-3 */
html[xmlns*=""] body:last-child #seis { color: blue }

/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:nth-of-type(1) #siete { color: blue }

/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
body:first-of-type #ocho { color: blue }

/* saf3+, chrome1+ */
@media screen and (-webkit-min-device-pixel-ratio:0) {
#diez { color: blue }
}

/* iPhone / mobile webkit */
@media screen and (max-device-width: 480px) {
#veintiseis { color: blue }
}


/* Safari 2 - 3.1 */
html[xmlns*=""]:root #trece { color: blue }

/* Safari 2 - 3.1, Opera 9.25 */
*|html[xmlns*=""] #catorce { color: blue }

/* Everything but IE6-8 */
:root *> #quince { color: blue }

/* IE7 */
*+html #dieciocho { color: blue }

/* Firefox only. 1+ */
#veinticuatro, x:-moz-any-link { color: blue }

/* Firefox 3.0+ */
#veinticinco, x:-moz-any-link, x:default { color: blue }

/* FF 3.5+ */
body:not(:-moz-handler-blocked) #cuarenta { color: blue; }


/***** Attribute Hacks ******/

/* IE6 */
#once { _color: blue }

/* IE6, IE7 */
#doce { *color: blue; /* or #color: blue */ }

/* Everything but IE6 */
#diecisiete { color/**/: blue }

/* IE6, IE7, IE8 */
#diecinueve { color: blue\9; }

/* IE7, IE8 */
#veinte { color/*\**/: blue\9; }

/* IE6, IE7 -- acts as an !important */
#veintesiete { color: blue !ie; } /* string after ! can be anything */

/* IE8, IE9 */
#anotherone {color: blue\0/;} /* must go at the END of all rules */

Thursday, May 26, 2011

Gnome 3 New Features and Does It Look Like Windows 7 ?


As most of you all know Gnome 3 is released (http://gnome3.org/) so as always I wanted to try it out, so I installed it on my fedora 14, and then wanted to check it out with fedora 15. After messing around with it for some time, It was okay but not that great, For the most part of it I could notice the similarities to Windows 7s normal theme. So went around searching to see what were others opinion about it, but tomy surprise there were NO or just a bit of talk about it, This gave me no other option but to write something for myself.

I'm not going to compare in detail and break down the things, maybe that's for another post. This will be a comparison between the major functionalities shown on the home page of Gnome 3 (you can checkout the videos)

Feature 1:  Instance Messenger Notification

Who are you kidding?? I have not come across a messenger which actually distracts me while I'm working all the messengers I have worked with just gives a small notification on the right side corner with the beginning of the message. What is the use of putting this as a feature on Gnome 3 ?? I seriously don't get that. If any of you disagree with me on this think about a messenger which does not actually do this. I do get, Integrating the messenger to Gnome might be a good thing, but the problem is if all my messengers notification look alike its just going to be dull in the end. Sometimes it is nice to have your mind distracted with Messenger specific notification than having a black themed notification.

Feature 2:  Easier Window Management

The next video goes on to say if you drag the window to the top it gets maximized and if you drag it to the left it goes to left, ext.. First thing that pop into my head was 'This is just the same feature Windows 7 gives' So whats the big deal?

Feature 3:  App-based Window Management

There is nothing to be said on this. This is no other than 'Quick Launch' feature introduced in Windows Vista. I dint not see anything new here. Its just the same old thing.

Feature 4:  Deeper Hardware Integration

'We only show Suspend option if it's supported by you hardware' That was there even on Windows XP which is more than ten years old. Showing options that are supported by your hardware was already available in Gnome 2. Firmware is not done by Gnome it just front ends it, so even how much support you have on this first the Firmware has to be updated if you want to use this at all.

Feature 5: Create Workspaces

Well this is actually good, but I still feel the static workspace is better. The problem with this is, I actually need to do another mouse move or a keyboard shortcut to see the workspaces that I have used. Anything that makes me do one more step is something thats not worth it.Workspace is something that we use a lot so I want to be able to see the free workspaces like on Gnome 2 without another command involved.

Where the Hell is the Task-bar ?

At first  you will see is that the task-bar is missing. Whats the point? I don't want to move my mouse or do a keyboard shortcut to see the programmes that are running I want to just see it in one glance when I want to, Its not worth to remove something just to save like 10 px of space. Again anything that makes me to something more is not worth it.


I'm not trying to persuade that all features are bad, there are some good features as well, but Gnome 3 was a disappointment for me. I was expecting more than what I received. Alas, none of the features were good enough for me to switch from Gnome 2 to Gnome 3.

Tuesday, May 17, 2011

Spring Security and CAS Integration with Vaadin

We have been working with Vaadin for some time now. We have done multiple websites and admin applications. There was a customer who requested a SSO (Single Sign On) to be added to one of the new website we are creating. So we thought its not that big of a deal; we just needed to add Spring security and then do the CAS filter and we are all done. Well the solution is striaght forward but don't even try to search this on the web, there is not even one fully integrated working solution out there, so what I did was I sat down and did a Vaadin wrapper with spring security integrated. I have used Spring 3.0.5 so all spring security tags are written for that.

This is how it looked like in the end

public class SampleVaadinApplication extends VaadinApplication {

    private UriFragmentUtility fragmentUtility;
    private VerticalLayout layout;

    @Override
    public void init() {
        final Window mainWindow = new Window("Vaadin Cas Sample");
        fragmentUtility = new UriFragmentUtility();
        mainWindow.setSizeFull();
        mainWindow.addComponent(fragmentUtility);
        layout = new VerticalLayout();
        mainWindow.addComponent(layout);
        setMainWindow(mainWindow);
    }
}


public void buttonClick(Button.ClickEvent event) {
     final String path = getURL().getPath();
     if (!hasAnyRole("ROLE_DEFAULT_ACCESS")) {
        getMainWindow().open(new ExternalResource(path + "do-something/"));
     } else {
        fragmentUtility.setFragment("do-something");
     }
 }


web.xml you just need to add this to be the wrapper (and of cos the spring tags ;) ).

<servlet>
    <servlet-name>vaadin-loader-with-cas</servlet-name>
    <servlet-class>hailu.vaadin.authentication.core.VaadinApplicationServlet</servlet-class>
    <init-param>
        <param-name>application</param-name>
        <param-value>hailu.vaadin.authentication.sample.SampleVaadinApplication</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>vaadin-loader-with-cas</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>


I have hosted it in git you can have a look at the sample to get your self up and running.
https://github.com/jasondevj/cas-integration-with-vaadin