PageRenderTime 32ms CodeModel.GetById 7ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast-client/src/test/java/com/hazelcast/client/longrunning/HazelcastClientPerformanceTest.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 231 lines | 197 code | 14 blank | 20 comment | 17 complexity | 51c76a6ae9c3c2b304486a7b100e1182 MD5 | raw file
  1/*
  2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package com.hazelcast.client.longrunning;
 18
 19import com.hazelcast.client.*;
 20import com.hazelcast.core.Hazelcast;
 21import com.hazelcast.core.HazelcastInstance;
 22import com.hazelcast.impl.ClusterOperation;
 23import com.hazelcast.util.Clock;
 24import org.junit.AfterClass;
 25import org.junit.BeforeClass;
 26import org.junit.Ignore;
 27import org.junit.Test;
 28
 29import java.io.IOException;
 30import java.net.InetSocketAddress;
 31import java.net.ServerSocket;
 32import java.net.Socket;
 33import java.util.*;
 34import java.util.concurrent.*;
 35import java.util.concurrent.atomic.AtomicInteger;
 36import java.util.concurrent.atomic.AtomicLong;
 37
 38import static org.junit.Assert.assertEquals;
 39import static org.junit.Assert.assertTrue;
 40import static org.mockito.Mockito.mock;
 41import static org.mockito.Mockito.when;
 42
 43public class HazelcastClientPerformanceTest extends HazelcastClientTestBase {
 44
 45    @Test
 46    public void putAndget100000RecordsWith1ClusterMember() {
 47        HazelcastClient hClient = getHazelcastClient();
 48        Map<String, String> map = hClient.getMap("putAndget100000RecordsWith1ClusterMember");
 49        putAndGet(map, 100000);
 50    }
 51
 52    private void putAndGet(Map<String, String> map, int counter) {
 53        long beginTime = Clock.currentTimeMillis();
 54        for (int i = 1; i <= counter; i++) {
 55            if (i % (counter / 10) == 0) {
 56                System.out.println(i + ": " + (Clock.currentTimeMillis() - beginTime) + " ms");
 57            }
 58            map.put("key_" + i, String.valueOf(i));
 59        }
 60        beginTime = Clock.currentTimeMillis();
 61        for (int i = 1; i <= counter; i++) {
 62            if (i % (counter / 10) == 0) {
 63                System.out.println(i + ": " + (Clock.currentTimeMillis() - beginTime) + " ms");
 64            }
 65            assertEquals(String.valueOf(i), map.get("key_" + i));
 66        }
 67    }
 68
 69    @Test
 70    public void putAndget100000RecordsWith1ClusterMemberFrom10Threads() throws InterruptedException {
 71        HazelcastClient hClient = getHazelcastClient();
 72        final Map<String, String> map = hClient.getMap("putAndget100000RecordsWith1ClusterMemberFrom10Threads");
 73        int count = 100000;
 74        int threads = 16;
 75        final AtomicInteger getCounter = new AtomicInteger(count);
 76        final AtomicInteger putCounter = new AtomicInteger(count);
 77        ExecutorService executorService = Executors.newFixedThreadPool(threads);
 78        final long beginTime = Clock.currentTimeMillis();
 79        final CountDownLatch latch = new CountDownLatch(threads);
 80        for (int i = 0; i < threads; i++) {
 81            executorService.execute(new Runnable() {
 82                public void run() {
 83                    int i;
 84                    while ((i = putCounter.getAndDecrement()) > 0) {
 85                        map.put("key_" + i, String.valueOf(i));
 86                    }
 87                    while ((i = getCounter.getAndDecrement()) > 0) {
 88                        map.get("key_" + i);
 89                    }
 90                    latch.countDown();
 91                }
 92            });
 93        }
 94        latch.await();
 95        System.out.println(threads + " Threads made in total " + count +
 96                " puts and gets in " + (Clock.currentTimeMillis() - beginTime) + " ms");
 97    }
 98
 99    @Test
100    public void putFromMultipleThreads() throws InterruptedException {
101        final HazelcastInstance h = Hazelcast.newHazelcastInstance(null);
102        final AtomicInteger counter = new AtomicInteger(0);
103        class Putter implements Runnable {
104            volatile Boolean run = true;
105
106            public void run() {
107                HazelcastClient hClient = TestUtility.newHazelcastClient(h);
108                while (run) {
109                    Map<String, String> clientMap = hClient.getMap("putFromMultipleThreads");
110                    clientMap.put(String.valueOf(counter.incrementAndGet()), String.valueOf(counter.get()));
111                }
112            }
113        }
114        ;
115        List<Putter> list = new ArrayList<Putter>();
116        for (int i = 0; i < 10; i++) {
117            Putter p = new Putter();
118            list.add(p);
119            new Thread(p).start();
120        }
121        Thread.sleep(5000);
122        for (Iterator<Putter> it = list.iterator(); it.hasNext(); ) {
123            Putter p = it.next();
124            p.run = false;
125        }
126        Thread.sleep(100);
127        assertEquals(counter.get(), h.getMap("putFromMultipleThreads").size());
128    }
129
130    @Test
131    public void putBigObject() {
132        HazelcastClient hClient = getHazelcastClient();
133        Map<String, Object> clientMap = hClient.getMap("putABigObject");
134        List list = new ArrayList();
135        int size = 10000000;
136        byte[] b = new byte[size];
137        b[size - 1] = (byte) 144;
138        list.add(b);
139        clientMap.put("obj", b);
140        byte[] bigB = (byte[]) clientMap.get("obj");
141        assertTrue(Arrays.equals(b, bigB));
142        assertEquals(size, bigB.length);
143    }
144
145    @Test
146    @Ignore
147    public void testOutThreadPerformance() throws IOException, InterruptedException {
148        new Thread(new Runnable() {
149            public void run() {
150                ServerSocket serverSocket = null;
151                try {
152                    serverSocket = new ServerSocket(5799);
153                } catch (IOException e) {
154                    System.out.println("Could not listen on port: 4444");
155                    System.exit(-1);
156                }
157                Socket clientSocket = null;
158                try {
159                    clientSocket = serverSocket.accept();
160                    byte[] bytes = new byte[1000000];
161                    while (true) {
162                        clientSocket.getInputStream().read(bytes);
163                    }
164                } catch (IOException e) {
165                    System.out.println("Accept failed: 4444");
166                    System.exit(-1);
167                }
168            }
169        }).start();
170        HazelcastClient client = mock(HazelcastClient.class);
171        ConnectionManager connectionManager = mock(ConnectionManager.class);
172        when(client.getConnectionManager()).thenReturn(connectionManager);
173        Connection connection = new Connection(3, new InetSocketAddress("localhost", 5799), 1);
174        when(connectionManager.getConnection()).thenReturn(connection);
175        PacketWriter packetWriter = new PacketWriter();
176        packetWriter.setConnection(connection);
177        final OutRunnable outRunnable = new OutRunnable(client, new HashMap<Long, Call>(), packetWriter);
178        new Thread(outRunnable).start();
179        final AtomicLong callCounter = new AtomicLong();
180        final long start = Clock.currentTimeMillis();
181        ExecutorService executorService = Executors.newFixedThreadPool(20);
182        final BlockingQueue<Object> queue = new LinkedBlockingQueue<Object>();
183        final Object object = new Object();
184        for (int i = 0; i < 16; i++) {
185            executorService.execute(new Runnable() {
186
187                public void run() {
188                    for (; ; ) {
189                        try {
190                            queue.take();
191                        } catch (InterruptedException e) {
192                            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
193                        }
194                        Packet packet = new Packet();
195                        packet.set("c:default", ClusterOperation.CONCURRENT_MAP_GET, new byte[30], null);
196                        Call call = new Call(callCounter.incrementAndGet(), packet);
197                        outRunnable.enQueue(call);
198                    }
199                }
200            });
201        }
202        Executors.newSingleThreadExecutor().submit(new Runnable() {
203            public void run() {
204                int numberOfTasks = 10000;
205//                int numberOfTasks = 11000;
206                while (true) {
207                    try {
208                        for (int i = 0; i < numberOfTasks; i++) {
209                            queue.offer(object);
210                        }
211//                        numberOfTasks = numberOfTasks + numberOfTasks/10;
212                        Thread.sleep(1 * 1000);
213                        System.out.println("Operations per millisecond : " + callCounter.get() / (Clock.currentTimeMillis() - start));
214                        System.out.println("out runnable Queue size: " + outRunnable.getQueueSize());
215                    } catch (Exception e) {
216                        e.printStackTrace();
217                    }
218                }
219            }
220        });
221        Thread.sleep(1000000);
222    }
223
224    @AfterClass
225    @BeforeClass
226    public static void shutdown() {
227//        getHazelcastClient().shutdown();
228//        Hazelcast.shutdownAll();
229//        TestUtility.client = null;
230    }
231}