Archive

Posts Tagged ‘java performance’

Java Pipes vs. Sockets in a Single JVM

In a recent project I should have implemented an InputStream in a java code. There are many ways to achieve this and in this blog post I will show off the performance of just two of them, Sockets and Pipes ( PipedInputStream and PipedOutputStream ).

Scenario is really simple, I’ll send both 10 and 100 million messages from producer to consumer for each scenario. Message is just a sentence, “This is a message sent from Producer”.

I run all the scenario for 3 times and then get the average due to some os environment factor, other processes etc. And also I used the PrintStream as data writer for both test and BufferedReader for reader.

Buffer values can be set for Pipes by passing the long value to the PipedInputStream constructor. And for socket, buffer can be set from BufferedReader constructor. Buffer value for this test is 1024*1024*8 for both scenarios.

Results:

Pipe Socket
10M msg 12sec 26sec
100M msg 72sec 239sec

The main code of the test:

public class PipeAndSocketTester {

	public static void main(String args[]) throws IOException {
		boolean autoFlush = false;
		long messageNumber = 10000000;

		PipedOutputStream pos = new PipedOutputStream();
		PipedInputStream pis = new PipedInputStream(pos, 1024*1024*10);

		new PipeWriter(pos,messageNumber,autoFlush).start();
		new PipeReader(pis).start();

		// after above code executed I remove the pipe code then execute follows
		new SocketWriter(4544,messageNumber).start();
		new SocketReader(4544,1024*1024*10).start();

	}
}
}
Advertisements
Categories: Java Tags: ,