创建胖罐会降低性能吗?

时间:2016-07-18 15:01:23

标签: java performance jar server client

我创建了一个非常简单的客户端 - 服务器项目。 服务器非常简单,甚至不创建任何工作线程。 它只是接收一个连接,读取一个字符串,然后将一个字符串写回客户端并继续监听。

客户端相应简单。

我这样做是为了尝试理解为什么我的真正的Java TCP服务器出现了我无法找到的性能问题。

我目睹的是一种非常奇怪的现象。 当我在我的机器上本地运行此客户端服务器时,通过IntelliJ IDE,响应时间为1毫秒(正如预期的那样)。

一旦我从这些客户端和服务器创建了一个胖罐,然后运行它,仍在本地,我突然得到50毫秒的响应时间

所以问题是,为什么会发生这种情况?我在创造肥胖罐子时做错了吗?

这是服务器代码:

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...");
    }
}
}

这是客户:

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;
}

提前感谢您的回复。

0 个答案:

没有答案