コード例 #1
0
  @Test
  public void putFromMultipleThreads() throws InterruptedException {
    final HazelcastInstance h = Hazelcast.newHazelcastInstance(null);
    final AtomicInteger counter = new AtomicInteger(0);
    class Putter implements Runnable {
      volatile Boolean run = true;

      public void run() {
        HazelcastClient hClient = TestUtility.newHazelcastClient(h);
        while (run) {
          Map<String, String> clientMap = hClient.getMap("putFromMultipleThreads");
          clientMap.put(String.valueOf(counter.incrementAndGet()), String.valueOf(counter.get()));
        }
      }
    };
    List<Putter> list = new ArrayList<Putter>();
    for (int i = 0; i < 10; i++) {
      Putter p = new Putter();
      list.add(p);
      new Thread(p).start();
    }
    Thread.sleep(5000);
    for (Iterator<Putter> it = list.iterator(); it.hasNext(); ) {
      Putter p = it.next();
      p.run = false;
    }
    Thread.sleep(100);
    assertEquals(counter.get(), h.getMap("putFromMultipleThreads").size());
  }
コード例 #2
0
  @Test
  @Ignore
  public void testOutThreadPerformance() throws IOException, InterruptedException {
    new Thread(
            new Runnable() {
              public void run() {
                ServerSocket serverSocket = null;
                try {
                  serverSocket = new ServerSocket(5799);
                } catch (IOException e) {
                  System.out.println("Could not listen on port: 4444");
                  System.exit(-1);
                }
                Socket clientSocket = null;
                try {
                  clientSocket = serverSocket.accept();
                  byte[] bytes = new byte[1000000];
                  while (true) {
                    clientSocket.getInputStream().read(bytes);
                  }
                } catch (IOException e) {
                  System.out.println("Accept failed: 4444");
                  System.exit(-1);
                }
              }
            })
        .start();
    HazelcastClient client = mock(HazelcastClient.class);
    ConnectionManager connectionManager = mock(ConnectionManager.class);
    when(client.getConnectionManager()).thenReturn(connectionManager);
    Connection connection = new Connection("localhost", 5799, 1);
    when(connectionManager.getConnection()).thenReturn(connection);
    PacketWriter packetWriter = new PacketWriter();
    packetWriter.setConnection(connection);
    final OutRunnable outRunnable =
        new OutRunnable(client, new HashMap<Long, Call>(), packetWriter);
    new Thread(outRunnable).start();
    final AtomicLong callCounter = new AtomicLong();
    final long start = Clock.currentTimeMillis();
    ExecutorService executorService = Executors.newFixedThreadPool(20);
    final BlockingQueue<Object> queue = new LinkedBlockingQueue<Object>();
    final Object object = new Object();
    for (int i = 0; i < 16; i++) {
      executorService.execute(
          new Runnable() {

            public void run() {
              for (; ; ) {
                try {
                  queue.take();
                } catch (InterruptedException e) {
                  e.printStackTrace(); // To change body of catch statement use File | Settings |
                  // File Templates.
                }
                Packet packet = new Packet();
                packet.set("c:default", ClusterOperation.CONCURRENT_MAP_GET, new byte[30], null);
                Call call = new Call(callCounter.incrementAndGet(), packet);
                outRunnable.enQueue(call);
              }
            }
          });
    }
    Executors.newSingleThreadExecutor()
        .submit(
            new Runnable() {
              public void run() {
                int numberOfTasks = 10000;
                //                int numberOfTasks = 11000;
                while (true) {
                  try {
                    for (int i = 0; i < numberOfTasks; i++) {
                      queue.offer(object);
                    }
                    //                        numberOfTasks = numberOfTasks + numberOfTasks/10;
                    Thread.sleep(1 * 1000);
                    System.out.println(
                        "Operations per millisecond : "
                            + callCounter.get() / (Clock.currentTimeMillis() - start));
                    System.out.println("out runnable Queue size: " + outRunnable.getQueueSize());
                  } catch (Exception e) {
                    e.printStackTrace();
                  }
                }
              }
            });
    Thread.sleep(1000000);
  }