PageRenderTime 33ms CodeModel.GetById 19ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

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