Last active
August 29, 2015 14:03
-
-
Save gurbuzali/a7d561ba1b0a75bbefbf to your computer and use it in GitHub Desktop.
map-store-performance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.hazelcast.config.Config; | |
| import com.hazelcast.config.MapConfig; | |
| import com.hazelcast.config.MapIndexConfig; | |
| import com.hazelcast.config.MapStoreConfig; | |
| import com.hazelcast.core.Hazelcast; | |
| import com.hazelcast.core.HazelcastInstance; | |
| import com.hazelcast.core.IMap; | |
| import com.hazelcast.core.MapStore; | |
| import java.io.Serializable; | |
| import java.util.Collection; | |
| import java.util.Map; | |
| import java.util.Random; | |
| import java.util.Set; | |
| import java.util.UUID; | |
| import java.util.concurrent.CountDownLatch; | |
| public class Performance implements Serializable { | |
| private String name; | |
| private String value; | |
| private int age; | |
| private long time; | |
| public Performance() { | |
| } | |
| public Performance(final String name, final String value, final int age, final long time) { | |
| this.name = name; | |
| this.value = value; | |
| this.age = age; | |
| this.time = time; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public void setName(final String name) { | |
| this.name = name; | |
| } | |
| public String getValue() { | |
| return value; | |
| } | |
| public void setValue(final String value) { | |
| this.value = value; | |
| } | |
| public int getAge() { | |
| return age; | |
| } | |
| public void setAge(final int age) { | |
| this.age = age; | |
| } | |
| public long getTime() { | |
| return time; | |
| } | |
| public void setTime(final long time) { | |
| this.time = time; | |
| } | |
| public static void main(String[] args) throws InterruptedException { | |
| final Config config = new Config(); | |
| String mapName = "map"; | |
| final MapConfig mapConfig = config.getMapConfig(mapName); | |
| final MapIndexConfig nameIndexConfig = new MapIndexConfig("name", false); | |
| mapConfig.addMapIndexConfig(nameIndexConfig); | |
| final MapIndexConfig timeIndexConfig = new MapIndexConfig("time", true); | |
| mapConfig.addMapIndexConfig(timeIndexConfig); | |
| int count = 100 * 1000; | |
| final CountDownLatch latch = new CountDownLatch(count); | |
| final MapStoreConfig mapStoreConfig = new MapStoreConfig(); | |
| mapStoreConfig.setEnabled(true).setWriteDelaySeconds(10); | |
| final MyMapStore store = new MyMapStore(latch); | |
| mapStoreConfig.setImplementation(store); | |
| mapConfig.setMapStoreConfig(mapStoreConfig); | |
| final HazelcastInstance instance = Hazelcast.newHazelcastInstance(config); | |
| final IMap<Object, Object> map = instance.getMap(mapName); | |
| final long begin = System.currentTimeMillis(); | |
| for (int i = 0; i < count; i++) { | |
| final String key = randomString(); | |
| final Performance performance = create(); | |
| map.put(key, performance); | |
| } | |
| long elapsed = System.currentTimeMillis() - begin; | |
| System.err.println("elapsed: " + elapsed); | |
| latch.await(); | |
| elapsed = System.currentTimeMillis() - begin; | |
| System.err.println("elapsed: " + elapsed); | |
| } | |
| static Performance create() { | |
| final String name = randomString(); | |
| final String value = randomString(); | |
| final int age = new Random().nextInt(100); | |
| final long time = System.currentTimeMillis(); | |
| return new Performance(name, value, age, time); | |
| } | |
| static String randomString() { | |
| return UUID.randomUUID().toString(); | |
| } | |
| static class MyMapStore implements MapStore<String, Performance> { | |
| final CountDownLatch latch; | |
| MyMapStore(final CountDownLatch latch) { | |
| this.latch = latch; | |
| } | |
| @Override | |
| public void store(final String key, final Performance value) { | |
| try { | |
| Thread.sleep(1); | |
| } catch (InterruptedException e) { | |
| e.printStackTrace(); | |
| } | |
| latch.countDown(); | |
| } | |
| @Override | |
| public void storeAll(final Map<String, Performance> map) { | |
| System.err.println("size: " + map.size()); | |
| for (Map.Entry<String, Performance> entry : map.entrySet()) { | |
| store(entry.getKey(), entry.getValue()); | |
| } | |
| } | |
| @Override | |
| public void delete(final String key) { | |
| } | |
| @Override | |
| public void deleteAll(final Collection<String> keys) { | |
| } | |
| @Override | |
| public Performance load(final String key) { | |
| return null; | |
| } | |
| @Override | |
| public Map<String, Performance> loadAll(final Collection<String> keys) { | |
| return null; | |
| } | |
| @Override | |
| public Set<String> loadAllKeys() { | |
| return null; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment