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

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 584 lines · 525 code · 28 blank · 31 comment · 22 complexity · a1b36dbf06ea99f2058678002a67122b 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.*;
  18. import com.hazelcast.util.Clock;
  19. import com.hazelcast.monitor.LocalMapOperationStats;
  20. import com.hazelcast.monitor.LocalQueueOperationStats;
  21. import com.hazelcast.query.SqlPredicate;
  22. import java.io.Serializable;
  23. import java.util.*;
  24. import java.util.concurrent.*;
  25. import java.util.concurrent.atomic.AtomicInteger;
  26. import java.util.logging.Logger;
  27. public class AllTest {
  28. private volatile boolean running = true;
  29. private final int nThreads;
  30. private final List<Runnable> operations = new ArrayList<Runnable>();
  31. private final ExecutorService ex;
  32. private final Random random = new Random(System.nanoTime());
  33. private final AtomicInteger messagesReceived = new AtomicInteger(0);
  34. private final AtomicInteger messagesSend = new AtomicInteger(0);
  35. private final int size = 10000;
  36. private static final int STATS_SECONDS = 10;
  37. final Logger logger = Logger.getLogger("All-test");
  38. public static void main(String[] args) {
  39. int nThreads = (args.length == 0) ? 10 : new Integer(args[0]);
  40. final AllTest allTest = new AllTest(nThreads);
  41. allTest.start();
  42. Executors.newSingleThreadExecutor().execute(new Runnable() {
  43. public void run() {
  44. while (true) {
  45. try {
  46. //noinspection BusyWait
  47. Thread.sleep(STATS_SECONDS * 1000);
  48. System.out.println("cluster size:"
  49. + Hazelcast.getCluster().getMembers().size());
  50. allTest.mapStats();
  51. allTest.qStats();
  52. allTest.topicStats();
  53. } catch (InterruptedException ignored) {
  54. return;
  55. }
  56. }
  57. }
  58. });
  59. }
  60. private void qStats() {
  61. LocalQueueOperationStats qOpStats = Hazelcast.getQueue("myQ").getLocalQueueStats().getOperationStats();
  62. long period = ((qOpStats.getPeriodEnd() - qOpStats.getPeriodStart()) / 1000);
  63. if (period == 0) {
  64. return;
  65. }
  66. log(qOpStats);
  67. log("Q Operations per Second : " + (qOpStats.getNumberOfOffers() + qOpStats.getNumberOfEmptyPolls() + qOpStats.getNumberOfEmptyPolls() + qOpStats.getNumberOfRejectedOffers()) / period);
  68. }
  69. private void log(Object message) {
  70. if (message != null) {
  71. logger.info(message.toString());
  72. }
  73. }
  74. private void mapStats() {
  75. LocalMapOperationStats mapOpStats = Hazelcast.getMap("myMap").getLocalMapStats().getOperationStats();
  76. long period = ((mapOpStats.getPeriodEnd() - mapOpStats.getPeriodStart()) / 1000);
  77. if (period == 0) {
  78. return;
  79. }
  80. log(mapOpStats);
  81. log("Map Operations per Second : " + mapOpStats.total() / period);
  82. }
  83. private void topicStats() {
  84. log("Topic Messages Sent : " + messagesSend.getAndSet(0) / STATS_SECONDS + "::: Messages Received: " + messagesReceived.getAndSet(0) / STATS_SECONDS);
  85. }
  86. AllTest(int nThreads) {
  87. this.nThreads = nThreads;
  88. ex = Executors.newFixedThreadPool(nThreads);
  89. List<Runnable> mapOperations = loadMapOperations();
  90. List<Runnable> qOperations = loadQOperations();
  91. List<Runnable> topicOperations = loadTopicOperations();
  92. this.operations.addAll(mapOperations);
  93. this.operations.addAll(qOperations);
  94. this.operations.addAll(topicOperations);
  95. Collections.shuffle(operations);
  96. }
  97. private void addOperation(List<Runnable> operations, Runnable runnable, int priority) {
  98. for (int i = 0; i < priority; i++) {
  99. operations.add(runnable);
  100. }
  101. }
  102. private void start() {
  103. for (int i = 0; i < nThreads; i++) {
  104. ex.submit(new Runnable() {
  105. public void run() {
  106. while (running) {
  107. int opId = random.nextInt(operations.size());
  108. Runnable operation = operations.get(opId);
  109. operation.run();
  110. // System.out.println("Runnning..." + Thread.currentThread());
  111. }
  112. }
  113. });
  114. }
  115. }
  116. private void stop() {
  117. running = false;
  118. }
  119. public static class Customer implements Serializable {
  120. private int year;
  121. private String name;
  122. private byte[] field = new byte[100];
  123. public Customer(int i, String s) {
  124. this.year = i;
  125. this.name = s;
  126. }
  127. }
  128. private List<Runnable> loadTopicOperations() {
  129. ITopic topic = Hazelcast.getTopic("myTopic");
  130. topic.addMessageListener(new MessageListener() {
  131. public void onMessage(Message message) {
  132. messagesReceived.incrementAndGet();
  133. }
  134. });
  135. List<Runnable> operations = new ArrayList<Runnable>();
  136. addOperation(operations, new Runnable() {
  137. public void run() {
  138. ITopic topic = Hazelcast.getTopic("myTopic");
  139. topic.publish(String.valueOf(random.nextInt(100000000)));
  140. messagesSend.incrementAndGet();
  141. }
  142. }, 10);
  143. return operations;
  144. }
  145. private List<Runnable> loadQOperations() {
  146. List<Runnable> operations = new ArrayList<Runnable>();
  147. addOperation(operations, new Runnable() {
  148. public void run() {
  149. IQueue q = Hazelcast.getQueue("myQ");
  150. q.offer(new byte[100]);
  151. }
  152. }, 10);
  153. addOperation(operations, new Runnable() {
  154. public void run() {
  155. IQueue q = Hazelcast.getQueue("myQ");
  156. try {
  157. q.offer(new byte[100], 10, TimeUnit.MILLISECONDS);
  158. } catch (InterruptedException e) {
  159. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  160. }
  161. }
  162. }, 10);
  163. addOperation(operations, new Runnable() {
  164. public void run() {
  165. IQueue q = Hazelcast.getQueue("myQ");
  166. q.contains(new byte[100]);
  167. }
  168. }, 1);
  169. addOperation(operations, new Runnable() {
  170. public void run() {
  171. IQueue q = Hazelcast.getQueue("myQ");
  172. q.isEmpty();
  173. }
  174. }, 1);
  175. addOperation(operations, new Runnable() {
  176. public void run() {
  177. IQueue q = Hazelcast.getQueue("myQ");
  178. q.size();
  179. }
  180. }, 1);
  181. addOperation(operations, new Runnable() {
  182. public void run() {
  183. IQueue q = Hazelcast.getQueue("myQ");
  184. q.remove(new byte[100]);
  185. }
  186. }, 1);
  187. addOperation(operations, new Runnable() {
  188. public void run() {
  189. IQueue q = Hazelcast.getQueue("myQ");
  190. q.remainingCapacity();
  191. }
  192. }, 1);
  193. addOperation(operations, new Runnable() {
  194. public void run() {
  195. IQueue q = Hazelcast.getQueue("myQ");
  196. q.poll();
  197. }
  198. }, 10);
  199. addOperation(operations, new Runnable() {
  200. public void run() {
  201. IQueue q = Hazelcast.getQueue("myQ");
  202. q.add(new byte[100]);
  203. }
  204. }, 10);
  205. addOperation(operations, new Runnable() {
  206. public void run() {
  207. IQueue q = Hazelcast.getQueue("myQ");
  208. try {
  209. q.take();
  210. } catch (InterruptedException e) {
  211. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  212. }
  213. }
  214. }, 10);
  215. addOperation(operations, new Runnable() {
  216. public void run() {
  217. IQueue q = Hazelcast.getQueue("myQ");
  218. List list = new ArrayList();
  219. for (int i = 0; i < 10; i++) {
  220. list.add(new byte[100]);
  221. }
  222. q.addAll(list);
  223. }
  224. }, 1);
  225. addOperation(operations, new Runnable() {
  226. public void run() {
  227. IQueue q = Hazelcast.getQueue("myQ");
  228. List list = new ArrayList();
  229. q.drainTo(list);
  230. }
  231. }, 1);
  232. return operations;
  233. }
  234. private List<Runnable> loadMapOperations() {
  235. ArrayList<Runnable> operations = new ArrayList<Runnable>();
  236. addOperation(operations, new Runnable() {
  237. public void run() {
  238. IMap map = Hazelcast.getMap("myMap");
  239. map.evict(random.nextInt(size));
  240. }
  241. }, 5);
  242. addOperation(operations, new Runnable() {
  243. public void run() {
  244. IMap map = Hazelcast.getMap("myMap");
  245. try {
  246. map.getAsync(random.nextInt(size)).get();
  247. } catch (InterruptedException e) {
  248. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  249. } catch (ExecutionException e) {
  250. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  251. }
  252. }
  253. }, 1);
  254. addOperation(operations, new Runnable() {
  255. public void run() {
  256. IMap map = Hazelcast.getMap("myMap");
  257. map.containsKey(random.nextInt(size));
  258. }
  259. }, 2);
  260. addOperation(operations, new Runnable() {
  261. public void run() {
  262. IMap map = Hazelcast.getMap("myMap");
  263. map.containsValue(new Customer(random.nextInt(100), String.valueOf(random.nextInt(100000))));
  264. }
  265. }, 2);
  266. addOperation(operations, new Runnable() {
  267. public void run() {
  268. IMap map = Hazelcast.getMap("myMap");
  269. int key = random.nextInt(size);
  270. map.lock(key);
  271. try {
  272. Thread.sleep(1);
  273. } catch (InterruptedException e) {
  274. } finally {
  275. map.unlock(key);
  276. }
  277. }
  278. }, 1);
  279. // addOperation(operations, new Runnable() {
  280. // public void run() {
  281. // IMap map = Hazelcast.getMap("myMap");
  282. // int key = random.nextInt(size);
  283. // map.lockMap(10, TimeUnit.MILLISECONDS);
  284. // try {
  285. // Thread.sleep(1);
  286. // } catch (InterruptedException e) {
  287. // } finally {
  288. // map.unlockMap();
  289. // }
  290. // }
  291. // }, 1);
  292. addOperation(operations, new Runnable() {
  293. public void run() {
  294. IMap map = Hazelcast.getMap("myMap");
  295. int key = random.nextInt(size);
  296. boolean locked = map.tryLock(key);
  297. if (locked) {
  298. try {
  299. Thread.sleep(1);
  300. } catch (InterruptedException e) {
  301. } finally {
  302. map.unlock(key);
  303. }
  304. }
  305. }
  306. }, 1);
  307. addOperation(operations, new Runnable() {
  308. public void run() {
  309. IMap map = Hazelcast.getMap("myMap");
  310. int key = random.nextInt(size);
  311. boolean locked = map.tryLock(key, 10, TimeUnit.MILLISECONDS);
  312. if (locked) {
  313. try {
  314. Thread.sleep(1);
  315. } catch (InterruptedException e) {
  316. } finally {
  317. map.unlock(key);
  318. }
  319. }
  320. }
  321. }, 1);
  322. addOperation(operations, new Runnable() {
  323. public void run() {
  324. IMap map = Hazelcast.getMap("myMap");
  325. Iterator it = map.entrySet().iterator();
  326. for (int i = 0; i < 10 && it.hasNext(); i++) {
  327. it.next();
  328. }
  329. }
  330. }, 1);
  331. addOperation(operations, new Runnable() {
  332. public void run() {
  333. IMap map = Hazelcast.getMap("myMap");
  334. map.getMapEntry(random.nextInt(size));
  335. }
  336. }, 2);
  337. addOperation(operations, new Runnable() {
  338. public void run() {
  339. IMap map = Hazelcast.getMap("myMap");
  340. map.isEmpty();
  341. }
  342. }, 3);
  343. addOperation(operations, new Runnable() {
  344. public void run() {
  345. IMap map = Hazelcast.getMap("myMap");
  346. map.put(random.nextInt(size), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))));
  347. }
  348. }, 50);
  349. addOperation(operations, new Runnable() {
  350. public void run() {
  351. IMap map = Hazelcast.getMap("myMap");
  352. map.tryPut(random.nextInt(size), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))), 10, TimeUnit.MILLISECONDS);
  353. }
  354. }, 5);
  355. addOperation(operations, new Runnable() {
  356. public void run() {
  357. IMap map = Hazelcast.getMap("myMap");
  358. try {
  359. map.putAsync(random.nextInt(size), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000)))).get();
  360. } catch (InterruptedException e) {
  361. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  362. } catch (ExecutionException e) {
  363. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  364. }
  365. }
  366. }, 5);
  367. addOperation(operations, new Runnable() {
  368. public void run() {
  369. IMap map = Hazelcast.getMap("myMap");
  370. map.put(random.nextInt(size), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))), 10, TimeUnit.MILLISECONDS);
  371. }
  372. }, 5);
  373. addOperation(operations, new Runnable() {
  374. public void run() {
  375. IMap map = Hazelcast.getMap("myMap");
  376. map.putIfAbsent(random.nextInt(size), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))), 10, TimeUnit.MILLISECONDS);
  377. }
  378. }, 5);
  379. addOperation(operations, new Runnable() {
  380. public void run() {
  381. IMap map = Hazelcast.getMap("myMap");
  382. map.putIfAbsent(random.nextInt(size), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))));
  383. }
  384. }, 5);
  385. addOperation(operations, new Runnable() {
  386. public void run() {
  387. IMap map = Hazelcast.getMap("myMap");
  388. Map localMap = new HashMap();
  389. for (int i = 0; i < 10; i++) {
  390. localMap.put(random.nextInt(size), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))));
  391. }
  392. map.putAll(localMap);
  393. }
  394. }, 1);
  395. addOperation(operations, new Runnable() {
  396. public void run() {
  397. IMap map = Hazelcast.getMap("myMap");
  398. map.get(random.nextInt(size));
  399. }
  400. }, 100);
  401. addOperation(operations, new Runnable() {
  402. public void run() {
  403. IMap map = Hazelcast.getMap("myMap");
  404. map.remove(random.nextInt(size));
  405. }
  406. }, 10);
  407. addOperation(operations, new Runnable() {
  408. public void run() {
  409. IMap map = Hazelcast.getMap("myMap");
  410. try {
  411. map.tryRemove(random.nextInt(size), 10, TimeUnit.MILLISECONDS);
  412. } catch (TimeoutException e) {
  413. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  414. }
  415. }
  416. }, 10);
  417. addOperation(operations, new Runnable() {
  418. public void run() {
  419. IMap map = Hazelcast.getMap("myMap");
  420. map.removeAsync(random.nextInt(size));
  421. }
  422. }, 10);
  423. addOperation(operations, new Runnable() {
  424. public void run() {
  425. IMap map = Hazelcast.getMap("myMap");
  426. map.remove(random.nextInt(size), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))));
  427. }
  428. }, 10);
  429. addOperation(operations, new Runnable() {
  430. public void run() {
  431. IMap map = Hazelcast.getMap("myMap");
  432. map.replace(random.nextInt(size), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))));
  433. }
  434. }, 4);
  435. addOperation(operations, new Runnable() {
  436. public void run() {
  437. IMap map = Hazelcast.getMap("myMap");
  438. map.replace(random.nextInt(size), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))), new Customer(random.nextInt(100), String.valueOf(random.nextInt(10000))));
  439. }
  440. }, 5);
  441. addOperation(operations, new Runnable() {
  442. public void run() {
  443. IMap map = Hazelcast.getMap("myMap");
  444. map.size();
  445. }
  446. }, 4);
  447. addOperation(operations, new Runnable() {
  448. public void run() {
  449. long begin = Clock.currentTimeMillis();
  450. IMap map = Hazelcast.getMap("myMap");
  451. Iterator it = map.entrySet(new SqlPredicate("year=" + random.nextInt(100))).iterator();
  452. while (it.hasNext()) {
  453. it.next();
  454. }
  455. // System.out.println("Took: " + (Clock.currentTimeMillis() - begin));
  456. }
  457. }, 1);
  458. addOperation(operations, new Runnable() {
  459. public void run() {
  460. IMap map = Hazelcast.getMap("myMap");
  461. Iterator it = map.entrySet(new SqlPredicate("name=" + random.nextInt(10000))).iterator();
  462. while (it.hasNext()) {
  463. it.next();
  464. }
  465. }
  466. }, 10);
  467. addOperation(operations, new Runnable() {
  468. public void run() {
  469. IMap map = Hazelcast.getMap("myMap");
  470. Iterator it = map.keySet(new SqlPredicate("name=" + random.nextInt(10000))).iterator();
  471. while (it.hasNext()) {
  472. it.next();
  473. }
  474. }
  475. }, 10);
  476. addOperation(operations, new Runnable() {
  477. public void run() {
  478. IMap map = Hazelcast.getMap("myMap");
  479. Iterator it = map.localKeySet().iterator();
  480. while (it.hasNext()) {
  481. it.next();
  482. }
  483. }
  484. }, 10);
  485. addOperation(operations, new Runnable() {
  486. public void run() {
  487. IMap map = Hazelcast.getMap("myMap");
  488. Iterator it = map.localKeySet(new SqlPredicate("name=" + random.nextInt(10000))).iterator();
  489. while (it.hasNext()) {
  490. it.next();
  491. }
  492. }
  493. }, 10);
  494. addOperation(operations, new Runnable() {
  495. public void run() {
  496. IMap map = Hazelcast.getMap("myMap");
  497. final CountDownLatch latch = new CountDownLatch(1);
  498. EntryListener listener = new EntryListener() {
  499. public void entryAdded(EntryEvent entryEvent) {
  500. latch.countDown();
  501. }
  502. public void entryRemoved(EntryEvent entryEvent) {
  503. latch.countDown();
  504. }
  505. public void entryUpdated(EntryEvent entryEvent) {
  506. latch.countDown();
  507. }
  508. public void entryEvicted(EntryEvent entryEvent) {
  509. latch.countDown();
  510. }
  511. };
  512. map.addEntryListener(listener, true);
  513. try {
  514. latch.await();
  515. } catch (InterruptedException e) {
  516. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  517. }
  518. map.removeEntryListener(listener);
  519. }
  520. }, 1);
  521. addOperation(operations, new Runnable() {
  522. public void run() {
  523. IMap map = Hazelcast.getMap("myMap");
  524. map.addIndex("year", true);
  525. }
  526. }, 1);
  527. addOperation(operations, new Runnable() {
  528. public void run() {
  529. IMap map = Hazelcast.getMap("myMap");
  530. final CountDownLatch latch = new CountDownLatch(1);
  531. EntryListener listener = new EntryListener() {
  532. public void entryAdded(EntryEvent entryEvent) {
  533. latch.countDown();
  534. }
  535. public void entryRemoved(EntryEvent entryEvent) {
  536. latch.countDown();
  537. }
  538. public void entryUpdated(EntryEvent entryEvent) {
  539. latch.countDown();
  540. }
  541. public void entryEvicted(EntryEvent entryEvent) {
  542. latch.countDown();
  543. }
  544. };
  545. map.addLocalEntryListener(listener);
  546. try {
  547. latch.await();
  548. } catch (InterruptedException e) {
  549. e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
  550. }
  551. map.removeEntryListener(listener);
  552. }
  553. }, 1);
  554. return operations;
  555. }
  556. }