Ejemplo n.º 1
0
  /**
   * This is the Original ChatBot main method
   *
   * @param arg
   * @throws Exception
   */
  public static void main(String... arg) throws Exception {
    //        ChatLoadClient code
    //        String host=ChatBot.host;
    //        int port=ChatBot.port;0
    //        int clients=ChatBot.clients;
    //        int mesgs=ChatBot.messages;
    //        int idleTime = ChatBot.idleTime;
    String host = arg.length > 0 ? arg[0] : "localhost";
    int port = arg.length > 1 ? Integer.parseInt(arg[1]) : 8081;
    int clients = arg.length > 2 ? Integer.parseInt(arg[2]) : 1000;
    int messages = arg.length > 3 ? Integer.parseInt(arg[3]) : 400;
    int idleTime = 30000;

    // Create client serially
    start = System.currentTimeMillis();
    ChatClientEndpoint[] chat = new ChatClientEndpoint[clients];
    for (int i = 0; i < chat.length; i++) {
      chat[i] = new ChatClientEndpoint(new URI("ws://" + host + ":" + port + "/"), "user " + i);
      members.add(chat[i]);
      connections++;
    }
    while (members.size() < clients) {
      if (System.currentTimeMillis() > (start + idleTime)) break;
      Thread.sleep(10);
    }
    end = System.currentTimeMillis();
    conxTime = end - start;
    System.err.printf(
        "Opened %d of %d connections to %s:%d in %dms\n",
        members.size(), clients, host, port, conxTime);

    // Send messages
    Random random = new Random();
    start = System.currentTimeMillis();
    // received.set(0);
    for (int i = 0; i < messages; i++) {
      ChatClientEndpoint c = chat[random.nextInt(chat.length)];
      String msg = "Hello random " + random.nextLong();
      // c.send(msg);
      c.send(msg);
      sent.incrementAndGet();
    }

    long last = 0;
    long progress = start;
    while (received.get() < (clients * messages)) {

      if (System.currentTimeMillis() > (progress + idleTime)) break;
      if (received.get() != last) {
        progress = System.currentTimeMillis();
        last = received.get();
      }
      Thread.sleep(50);
    }

    end = System.currentTimeMillis();
    rxTime = end - start;
    System.err.printf(
        "Sent/Received %d/%d messages in %dms: %dmsg/s\n",
        sent.get(), received.get(), rxTime, (received.get() * 1000) / rxTime);

    // Close all connections

    start = System.currentTimeMillis();
    for (int i = 0; i < chat.length; i++) {
      // chat[i].disconnect();
      chat[i].userSession.close();
      members.remove(chat[i]);
    }

    while (members.size() > 0) {
      if (System.currentTimeMillis() > (start + idleTime)) break;
      Thread.sleep(10);
    }
    end = System.currentTimeMillis();

    System.err.printf(
        "Closed %d connections to %s:%d in %dms\n", clients, host, port, (end - start));

    /** ******************************* */

    /*while (true) {
        clientEndPoint.sendMessage(getMessage("Hi There!!"));
        Thread.sleep(30000);
    }*/
  }