Java 8 Parallel Streams

Parallelization is great feature with Java using streams and lambds and can be used to best use of our CPU cores to maximize the calculation.

  • parallelStream() -> On collections
  • parallel() -> on Streams

But are we sure that we need parallel always, yes, think again?

  • Do you have enough size of collation to do parallel operation?
  • Why you need parallel? If there are blocking tasks involve in parallel operation like network calls, you may possibly consume all threads in wait state and may impact other parallel operations.
  • Here is the key, all parallel streams use same thread pool (create one time) for all parallel operations. You are not going to get new thread pool for each parallel operation. If you block all threads in a pool, other parallel operations will also starving for getting thread and potentially slow down all applications.
  • If you know there are chances to have thread lock down condition, can we use our own Thread Executor for each parallel stream (rather than using internally provided).

A information is provided here: http://coopsoft.com/ar/Calamity2Article.html

Case 1 (Shared Fork Join Thread Pool):

Stream<Long> parallelStream = aList.parallelStream();

Case 2: (My own Thread Pool)

ForkJoinPool myPool = new ForkJoinPool(10);
long total= customThreadPool.submit(() -> aList.parallelStream().reduce(0L, Long::sum)).get();
—-

 

Imitating a Browser

There are several solution that QA team is using to simulate browser behavior to doing load/performance tests. I was just looking at HTMLUnit (http://htmlunit.sourceforge.net) for a internal test of browser. It is simple and have all basic features that we generally need to test a browser based application.

Code can be configured to test with multiple browsers. I have tried a simple program in java to test it, it is great!! Look at above site for more information.

package com.test;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;

import com.gargoylesoftware.htmlunit.WebClient;

import com.gargoylesoftware.htmlunit.html.HtmlForm;

import com.gargoylesoftware.htmlunit.html.HtmlImageInput;

import com.gargoylesoftware.htmlunit.html.HtmlPage;

import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;

import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

import com.gargoylesoftware.htmlunit.util.Cookie;

import java.io.IOException;

import java.net.URL;

import java.util.Iterator;

import java.util.Set;

/**

* Invoke any identified HTML files with out browser and gether result from it.

*/

public class HTMLInvoker {

private static String url = “your url”;

public static void main(String[] args) {

WebClient webClient = new WebClient();

HtmlPage page = null;

try {

URL siteUrl = new URL(url);

page = (HtmlPage) webClient.getPage(siteUrl);

HtmlForm form = page.getFormByName(“form”); // View source of page and check form name

HtmlTextInput id = (HtmlTextInput) form.getInputByName(“id”); // View source and check id/name of element/textfields

HtmlPasswordInput passwd = (HtmlPasswordInput) form.getInputByName(“password”); // View source and check name/id of password field

HtmlImageInput submitButton = (HtmlImageInput) page.getElementById(“submit”); // View page source and get button id/name

id.setValueAttribute(“arvind@gmail.com”); // Set value to text field

passwd.setValueAttribute(“arvind123”); // Set password field

webClient.getOptions().setThrowExceptionOnScriptError(false); // Avoid script validation

submitButton.click(); // Submit your page

}

catch (FailingHttpStatusCodeException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

catch (IOException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

// RESULT: Process your results

Set<Cookie> cookie = webClient.getCookieManager().getCookies();

// Here several objects related to page can be fetched.

if (cookie != null) {

Iterator<Cookie> i = cookie.iterator();

while (i.hasNext()) {

Cookie ck = i.next();

if (ck.getName().contains(“any_cookie”)) {

String cookieValue = ck.getValue();

System.out.println(“Cookie value:” + cookieValue);

break;

}

}

}

webClient.closeAllWindows();

}

}

Generating TCP Trace

TCP trace helps solving several issues whether application logger is enabled or not. These days when applications are becoming more and more service oriented, finding issue in between layers is tricky and cumbersome. TCP tracing helps to compare request and response of success and failed TCP traces to identify issues in network calls. Primarily my communication protocol is HTTP, however traces can be generated for any protocol.

Tool 1: Grinder TCP Trace;

Grinder is generally used for load testing however it is quite handy for traffic routing and TCP proxy. More information is provided here.

Just download Grinder proxy in any folder and create a sample script having following command to quick start your proxy:

java -cp grinder/grinder.jar -Xms16m -Xmx32m net.grinder.TCPProxy -localhost localhost -localport 9090 -remotehost $1 -remoteport $2

Now, just start your proxy using following simple command. You need to provide remote host and remote port information to script.

 ./grinder.sh tesserver.com 8080

Now proxy is started on localhost:9090. Just replace your real URL with proxied URL to get TCP trace on command prompt (Terminal). Ex: http://localhost:9090/index.html -> will call http://tesserver.com:8080/index.html

Tool 2: Wireshark

Wireshark does not need any introduction, it is great tool to generate trace for any communication and quite common in software/hardware industry.

Wireshark comes with installation for most of platform. However installing on MAC needs few more steps:

– Download Wireshark from here.

– It needs X windows system on some OSX. For that you can download XQuartz from here.

– Simple logout/login after installation of xQuartz.

– Start wireshark after installation. It will ask for X11 installation. Browse your folder to “/Applications/Utilities/XQuartz.app/Contents/MacOS/X11” application.

– It may open X11 application with Wireshark, close both and restart Wireshark again. Great, we are good to go next.

TCP/HTTP traces are easy with Wireshark. There are plenty of information available on internet. For me, following traces are enough:

– Start Wireshark traces with identified network trace (Select Interfaces list and identify your available networks)

Screen Shot 2015-03-09 at 5.08.47 PM

– Now, Wireshark will generate lots of traces, just select HTTP text from ‘Filter’ text box:

Screen Shot 2015-03-09 at 5.11.49 PM

– Right click on HTTP request and select ‘Follow TCP Traces”

Screen Shot 2015-03-09 at 5.14.38 PM

– Your TCP trace are zoomed out. It can be saved in your disk.

Screen Shot 2015-03-09 at 5.16.06 PM

Cheers 🙂

Eclipse – Data Assist in tooltip

While debugging in eclipse, hovering is not showing the real data (value) of parameter and annoying developer by showing method type. Hence developer needs to go debug screen (or right click -> Display) to see value of a variable while in debug mode.

I need to see value on hovering mouse on a variable rather than right click or change eclipse perspective.

For that: Go to Preferences -> Search Hover: You will see following default screen:

Screen Shot 2015-02-16 at 11.54.26 AM

Now un-select “Combined Hover” option and clean out “Source” option. Remember you need to keep “Source” option selected. Apply and Save.

Screen Shot 2015-02-16 at 11.56.11 AM

Great, now you can see respective value of a variable in tool tip. Also if you hover on a method, you can see detail implementation of method.

Setting Java Cryptography Extension (JCE) Unlimited Strength

Default java installation is not coming with high encryption extension files. These files are valid for USA country and are need to be downloaded and installed separately:

1. Download the JCE Policy jar files from the below location:

http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html

2. The zip file would contain two jar files (local_policy.jar and US_export_policy.jar).

3. These jar files need to be placed under the ‘jre/lib/security’ location. For my MAC, this location is as follow:

/Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home/jre/lib/security

Enjoy strong encryption algorithm now 🙂

Design a site like this with WordPress.com
Get started