0

I have created a very simple client-server project. The server is so simple, it does not even create any worker threads. It simply receives a connection, reads a string, and then writes a string back to the client and keeps listening.

The client is correspondingly simple.

I have done this in order to try and understand why my real Java TCP server is having performance issues which I fail to find.

What I have witnessed is a very strange phenomenon. When I run this client-server, locally on my machine, via the IntelliJ IDE, the response time is 1ms (as to be expected).

Once I created a fat jar out of these client and server, and run it, still locally, I suddenly get a response time of 50ms

So the question is, why is this happening? am I doing something wrong in the creation of the fat jars?

This is the server code:

ublic class NakedServer {

public static void main(String[] args) throws IOException {

    LogsManager.createServerLogsDir();
    Logger logger = LogsManager.getLogger("NakedServer");

    int port = 9989;
    logger.info("Starting server at port " + port + "...");

    try (ServerSocket serverSocket = new ServerSocket(port)) {
        //boolean readyToStop = false;
        while(true) {
            try {
                Socket clientSocket = serverSocket.accept();
                InputStream is = clientSocket.getInputStream();
                OutputStream os = clientSocket.getOutputStream();
                ObjectInputStream ois = new ObjectInputStream(is);
                ObjectOutputStream oos = new ObjectOutputStream(os);


                String str = (String) ois.readObject();
                logger.info("Received message from client:" + str);

                String serverReply = str + "_serverReply";

                logger.info("Returning message to client:" + str + "_serverReply");

                oos.writeObject(serverReply);
            } catch (IOException e) {
                logger.severe("Exception while handling client: " + e.getMessage());
            }
        }

    }catch (Exception e) {
        e.printStackTrace();
        logger.severe("Server failure:" + e.getMessage() + " Terminating server...");
    }
}
}

And this is the client:

public class NakedClient {

public static void main(String[] args) throws IOException {

    LogsManager.createServerLogsDir();
    Logger logger = LogsManager.getLogger("NakedClient");

    Properties prop = loadProperties();

    String host = prop.getProperty("server.host");
    int port = Integer.parseInt(prop.getProperty("server.port"));

    logger.info("Connecting to:" + host + ":" + port + "...");

    try (Socket client = new Socket(host, port)) {
        logger.info("Connected.");
        ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());

        String msg = "client message";

        logger.info("Sending message:");

        oos.writeObject(msg);

        long start = System.currentTimeMillis();
        ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
        String reply = (String) ois.readObject();
        long end = System.currentTimeMillis();

        logger.info("Received reply:" + reply + " in " + (end-start) + " millis");
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
        logger.severe("Client failure:" + e.getMessage());
    }
}

private static Properties loadProperties() throws IOException {
    Properties prop = new Properties();
    prop.load(new FileInputStream(new File("config.properties")));
    return prop;
}

Thanks in advance for any replies.

Nom1fan
  • 846
  • 2
  • 11
  • 27
  • 2
    What's a "fat jar"? I know what a jar is, but I'm not aware of their coming in sizes... – T.J. Crowder Jul 18 '16 at 15:02
  • "fat" is not a size, please be more exhaustive. – n00dl3 Jul 18 '16 at 15:04
  • 3
    Just to be sure: you did proper measurement; so you are not just witnessing that starting a client to connect; to then end the JVM ... takes more time, when the JVM has to load a larger JAR? You did repeated measuring; and found your client to be "faster" within your IDE?! – GhostCat Jul 18 '16 at 15:06
  • 1
    Is your server's response *always* ~50ms when run via the jar? Does it perhaps go down on the second request, or over time as it serves multiple requests? – John Bollinger Jul 18 '16 at 15:07
  • Thanks all for replying. a "fat jar" is an expression which means a jar containing all of its dependencies that can be run completely independently: [link](http://stackoverflow.com/questions/19150811/what-is-a-fat-jar). As for your questions, it is always ~50ms, no matter how many times the client addresses the server. – Nom1fan Jul 19 '16 at 08:42

0 Answers0