Permalink
Browse files

Original version of StdInClient and StdOutServer

  • Loading branch information...
pzemtsov committed Jan 29, 2015
1 parent ece24bd commit 239ae47ff8cbbf7d955e7a826452587acf307409
Showing with 85 additions and 0 deletions.
  1. +55 −0 StdInClient.java
  2. +30 −0 StdOutServer.java
View
@@ -0,0 +1,55 @@
+import java.io.EOFException;
+import java.io.IOException;
+import java.io.InputStream;
+
+
+public class StdInClient
+{
+ public static void main (String [] args) throws Exception
+ {
+ InputStream in = System.in;
+ while (true) {
+ long t0 = System.currentTimeMillis ();
+ long sum = 0;
+ int N = 10000000;
+ for (int i = 0; i < N; i++) {
+ byte [] type = readBytes (in, 4);
+ int len = readInt (in);
+ byte [] msg = readBytes (in, len);
+ processMessage (type, msg);
+ sum += msg.length + 8;
+ }
+ long t1 = System.currentTimeMillis ();
+ long t = t1 - t0;
+ System.out.printf ("Time for %d msg: %d; speed: %d msg/s; %.1f MB/s\n",
+ N, t, N * 1000L / t, sum * 0.001 / t);
+ }
+ }
+
+ private static byte [] readBytes (InputStream in, int expectedSize) throws IOException
+ {
+ final byte[] buffer = new byte[expectedSize];
+ int totalReadSize = 0;
+ while (totalReadSize < expectedSize) {
+ int readSize = in.read(buffer, totalReadSize, expectedSize - totalReadSize);
+ if (readSize < 0) throw new EOFException ();
+ totalReadSize += readSize;
+ }
+ return buffer;
+ }
+
+ private static final int readInt (InputStream in) throws IOException
+ {
+ int ch1 = in.read();
+ int ch2 = in.read();
+ int ch3 = in.read();
+ int ch4 = in.read();
+ if ((ch1 | ch2 | ch3 | ch4) < 0)
+ throw new EOFException();
+ return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
+ }
+
+ private static void processMessage (byte [] type, byte [] msg)
+ {
+ }
+}
View
@@ -0,0 +1,30 @@
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+
+
+public class StdOutServer
+{
+ public static void main (String [] args) throws Exception
+ {
+ byte [] MESSAGE_TYPE = new byte[] {1, 2, 3, 4};
+ int MESSAGE_LEN = 100;
+ int N = 1024;
+
+ byte [] buf = new byte [(4 + 4 + MESSAGE_LEN) * N];
+ ByteBuffer buffer = ByteBuffer.wrap (buf);
+ int num = 0;
+ for (int i = 0; i < N; i++) {
+ buffer.put (MESSAGE_TYPE);
+ buffer.putInt (MESSAGE_LEN);
+ for (int j = 0; j < MESSAGE_LEN; j++) {
+ buffer.put ((byte) num);
+ num += 3;
+ }
+ }
+
+ OutputStream out = System.out;
+ while (true) {
+ out.write (buf);
+ }
+ }
+}

0 comments on commit 239ae47

Please sign in to comment.