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