/hazelcast/src/main/java/com/hazelcast/examples/SimpleFunctionalMapTest.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 161 lines · 133 code · 12 blank · 16 comment · 26 complexity · 95ba246e314b30629adcc1106d0d0f9e 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.examples;
  17. import com.hazelcast.core.Hazelcast;
  18. import com.hazelcast.core.IMap;
  19. import java.util.Collection;
  20. import java.util.Map;
  21. import java.util.Set;
  22. import java.util.concurrent.ConcurrentHashMap;
  23. import java.util.concurrent.ExecutorService;
  24. import java.util.concurrent.Executors;
  25. import java.util.concurrent.atomic.AtomicLong;
  26. public class SimpleFunctionalMapTest {
  27. public static final int ENTRY_COUNT = 1000;
  28. public static final int KB = 10240;
  29. public static final int STATS_SECONDS = 10;
  30. public static void main(String[] args) {
  31. int threadCount = 40;
  32. final Stats stats = new Stats();
  33. ExecutorService es = Executors.newFixedThreadPool(threadCount);
  34. for (int i = 0; i < threadCount; i++) {
  35. es.submit(new Runnable() {
  36. public void run() {
  37. IMap map = Hazelcast.getMap("default");
  38. while (true) {
  39. int keyInt = (int) (Math.random() * ENTRY_COUNT);
  40. int operation = ((int) (Math.random() * 1000)) % 20;
  41. Object key = String.valueOf(keyInt);
  42. if (operation < 1) {
  43. map.size();
  44. stats.increment("size");
  45. } else if (operation < 2) {
  46. map.get(key);
  47. stats.increment("get");
  48. } else if (operation < 3) {
  49. map.remove(key);
  50. stats.increment("remove");
  51. } else if (operation < 4) {
  52. map.containsKey(key);
  53. stats.increment("containsKey");
  54. } else if (operation < 5) {
  55. Object value = new String(String.valueOf(key));
  56. map.containsValue(value);
  57. stats.increment("containsValue");
  58. } else if (operation < 6) {
  59. map.putIfAbsent(key, createValue());
  60. stats.increment("putIfAbsent");
  61. } else if (operation < 7) {
  62. Collection col = map.values();
  63. for (Object o : col) {
  64. }
  65. stats.increment("values");
  66. } else if (operation < 8) {
  67. Collection col = map.keySet();
  68. for (Object o : col) {
  69. }
  70. stats.increment("keySet");
  71. } else if (operation < 9) {
  72. Collection col = map.entrySet();
  73. for (Object o : col) {
  74. }
  75. stats.increment("entrySet");
  76. } else {
  77. map.put(key, createValue());
  78. stats.increment("put");
  79. }
  80. }
  81. }
  82. });
  83. }
  84. Executors.newSingleThreadExecutor().submit(new Runnable() {
  85. public void run() {
  86. while (true) {
  87. try {
  88. //noinspection BusyWait
  89. Thread.sleep(STATS_SECONDS * 1000);
  90. System.out.println("cluster size:"
  91. + Hazelcast.getCluster().getMembers().size());
  92. Stats currentStats = stats.getAndReset();
  93. System.out.println(currentStats);
  94. } catch (Exception e) {
  95. e.printStackTrace();
  96. }
  97. }
  98. }
  99. });
  100. }
  101. public static Object createValue() {
  102. int numberOfK = (((int) (Math.random() * 1000)) % 40) + 1;
  103. return new byte[numberOfK * KB];
  104. }
  105. public static class Stats {
  106. Map<String, AtomicLong> mapStats = new ConcurrentHashMap(10);
  107. public Stats() {
  108. mapStats.put("put", new AtomicLong(0));
  109. mapStats.put("get", new AtomicLong(0));
  110. mapStats.put("remove", new AtomicLong(0));
  111. mapStats.put("size", new AtomicLong(0));
  112. mapStats.put("containsKey", new AtomicLong(0));
  113. mapStats.put("containsValue", new AtomicLong(0));
  114. mapStats.put("clear", new AtomicLong(0));
  115. mapStats.put("keySet", new AtomicLong(0));
  116. mapStats.put("values", new AtomicLong(0));
  117. mapStats.put("entrySet", new AtomicLong(0));
  118. mapStats.put("putIfAbsent", new AtomicLong(0));
  119. }
  120. public Stats getAndReset() {
  121. Stats newOne = new Stats();
  122. Set<Map.Entry<String, AtomicLong>> entries = newOne.mapStats.entrySet();
  123. for (Map.Entry<String, AtomicLong> entry : entries) {
  124. String key = entry.getKey();
  125. AtomicLong value = entry.getValue();
  126. value.set(mapStats.get(key).getAndSet(0));
  127. }
  128. return newOne;
  129. }
  130. @Override
  131. public String toString() {
  132. StringBuilder sb = new StringBuilder();
  133. long total = 0;
  134. Set<Map.Entry<String, AtomicLong>> entries = mapStats.entrySet();
  135. for (Map.Entry<String, AtomicLong> entry : entries) {
  136. String key = entry.getKey();
  137. AtomicLong value = entry.getValue();
  138. sb.append(key + ":" + value.get());
  139. sb.append("\n");
  140. total += value.get();
  141. }
  142. sb.append("Operations per Second : " + total / STATS_SECONDS + " \n");
  143. return sb.toString();
  144. }
  145. public void increment(String operation) {
  146. mapStats.get(operation).incrementAndGet();
  147. }
  148. }
  149. }