Pergunta

This method suppose to execute 20000 times, I check that everything is fine until this code is called.

I think the performance issue is with the StringBuilder; What I can tell is that when the loop is in the 275~ elements it blocks and it continue but very very slow (maybe son excepts in object creation). As soon I remove the call for this method everything run more faster and finish the loop; so the issue is here in this method. Can anyone enlighten me?

I tried to reuse the StringBuilder and it is not working.

    private void sendTrafficDataToServer(InetAddress srcIp, InetAddress destIp, String protocolSource,
        String protocolDest, int length, long epochTime, String sourceMac,
        String destMac, String network, String _interface) throws MalformedURLException, IOException {

    StringBuilder subtree = new StringBuilder(network);
    //subtree.append(".").append(URLEncoder.encode(_interface, "UTF-8"));

    //=====================================================================
    // package protocol host source
    String fqdnSource = srcIp.getCanonicalHostName().replaceAll("[.]", "+");

    subtree.append(".").append(URLEncoder.encode(fqdnSource, "UTF-8"));
    subtree.append(".").append(URLEncoder.encode(protocolSource, "UTF-8"));

    StringBuilder message = new StringBuilder();
    message.append("c=").append("packethostsrc").append("&");
    message.append("s=").append(subtree).append("&");
    message.append("t=").append(epochTime).append("&");
    message.append("v=").append(length);

    // send data to server
    //ServerDataSource.insertData(message.toString(), hostname);
    logger.info("Message sent: {}", message.toString());
    logger.debug("Counter: {}", ++counter);

    //=====================================================================
    // package protocol host dest
    String fqdnDest = destIp.getCanonicalHostName().replaceAll("[.]", "+");

    subtree = subtree.delete(network.length(), subtree.length());
    subtree.append(".").append(URLEncoder.encode(fqdnDest, "UTF-8"));
    subtree.append(".").append(URLEncoder.encode(protocolDest, "UTF-8"));

    message.setLength(0);
    message.append("c=").append("packethostdest").append("&");
    message.append("s=").append(subtree).append("&");
    message.append("t=").append(epochTime).append("&");
    message.append("v=").append(length);

    // send data to server
    //ServerDataSource.insertData(message.toString(), hostname);

    //=====================================================================
    // package protocol source
    subtree = subtree.delete(network.length(), subtree.length());
    subtree.append(".").append(URLEncoder.encode(protocolSource, "UTF-8"));

    message.setLength(0);
    message.append("c=").append("packetprotocolsrc").append("&");
    message.append("s=").append(subtree).append("&");
    message.append("t=").append(epochTime).append("&");
    message.append("v=").append(length);

    // send data to server
    //ServerDataSource.insertData(message.toString(), hostname);

    //=====================================================================
    // package protocol dest
    subtree = subtree.delete(network.length(), subtree.length());
    subtree.append(".").append(URLEncoder.encode(protocolDest, "UTF-8"));

    message.setLength(0);
    message.append("c=").append("packetprotocoldest").append("&");
    message.append("s=").append(subtree).append("&");
    message.append("t=").append(epochTime).append("&");
    message.append("v=").append(length);

    // send data to server
    //ServerDataSource.insertData(message.toString(), hostname);

    //=====================================================================     
    // package conversation
    subtree = subtree.delete(network.length(), subtree.length());
    subtree.append(".").append(URLEncoder.encode(fqdnSource, "UTF-8"));
    subtree.append(".").append(URLEncoder.encode(fqdnDest, "UTF-8"));

    message.setLength(0);
    message.append("c=").append("packetconversation").append("&");
    message.append("s=").append(subtree).append("&");
    message.append("t=").append(epochTime).append("&");
    message.append("v=").append(length);

    // send data to server
    //ServerDataSource.insertData(message.toString(), hostname);

    //=====================================================================
    // package network card source
    subtree = subtree.delete(network.length(), subtree.length());
    subtree.append(".").append(URLEncoder.encode(sourceMac, "UTF-8"));

    message.setLength(0);
    message.append("c=").append("packetnetworkcardsrc").append("&");
    message.append("s=").append(subtree).append("&");
    message.append("t=").append(epochTime).append("&");
    message.append("v=").append(length);

    // send data to server
    //ServerDataSource.insertData(message.toString(), hostname);

    //=====================================================================
    // package network card dest    
    subtree = subtree.delete(network.length(), subtree.length());
    subtree.append(".").append(URLEncoder.encode(destMac, "UTF-8"));

    message.setLength(0);
    message.append("c=").append("packetnetworkcarddest").append("&");
    message.append("s=").append(subtree).append("&");
    message.append("t=").append(epochTime).append("&");
    message.append("v=").append(length);

    // send data to server
    //ServerDataSource.insertData(message.toString(), hostname);
}
Foi útil?

Solução 2

I fixed the issue, it was the replace() method the one with the performance issue.

String fqdnSource = StringUtils.replace(srcIp.getCanonicalHostName(), ".", "+");

I am using the StringUtils.replace() instead.

Also getCanonicalHostName() add some performance issue but it is necessary if I want the name resolution for the IP, in which case the performance is not so bad.

Outras dicas

I very much doubt there is a problem with StringBuilder as it is in constant heavy use everywhere. Use a profiler (you probably have one built into your IDE) to get a better idea of exactly what is happening.

If you aren't getting any joy with the profiler then scatter time recording code throughout the method and average the time over large numbers of runs, recording average and maximum at each step for each part of the method. That should quickly identify the part that's actually causing the problem.

Having said that it looks like you don't even need a StringBuilder here, as presumably you have an OutputStream of some sort hidden somewhere. Just send your messages to the Stream as you build them and you never need the builder in the first place.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top