PageRenderTime 79ms CodeModel.GetById 17ms app.highlight 56ms RepoModel.GetById 1ms app.codeStats 1ms

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