Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: java/com/l2jserver/tests/CpuTimeMeasurer.java
- ===================================================================
- --- java/com/l2jserver/tests/CpuTimeMeasurer.java (revision 0)
- +++ java/com/l2jserver/tests/CpuTimeMeasurer.java (revision 0)
- @@ -0,0 +1,134 @@
- +package com.l2jserver.tests;
- +
- +import java.lang.management.ManagementFactory;
- +import java.lang.management.ThreadMXBean;
- +import java.util.HashMap;
- +import java.util.Map;
- +import java.util.Map.Entry;
- +
- +public final class CpuTimeMeasurer
- +{
- + private final class CpuTime
- + {
- + public long measurements = 0;
- + public long measureStart = 0;
- + public long worst = Long.MIN_VALUE;
- + public long best = Long.MAX_VALUE;
- + public long total = 0;
- +
- + protected CpuTime()
- + {
- + }
- + }
- +
- + private final ThreadMXBean _tmxb = ManagementFactory.getThreadMXBean();
- + private final Map<String, CpuTime> _cpuTimes = new HashMap<>();
- + private long _bestWorst = Long.MAX_VALUE;
- + private long _bestBest = Long.MAX_VALUE;
- +
- + public void addCpuTimer(String name)
- + {
- + _cpuTimes.put(name, new CpuTime());
- + }
- +
- + private String makeStringTime(long nanos)
- + {
- + long seconds = nanos / 1000000 / 1000;
- + nanos -= seconds * 1000 * 1000000;
- + long millis = nanos / 1000000;
- + nanos -= millis * 1000000;
- + return seconds + "s " + millis + "ms " + nanos + "ns";
- + }
- +
- + public void resetCpuTimers()
- + {
- + for (CpuTime time : _cpuTimes.values())
- + {
- + time.measurements = 0;
- + time.measureStart = 0;
- + time.worst = Long.MIN_VALUE;
- + time.best = Long.MAX_VALUE;
- + time.total = 0;
- + }
- + }
- +
- + public void dumpCpuTimers()
- + {
- + long bestTotal = Long.MAX_VALUE;
- + for (Entry<String, CpuTime> entry : _cpuTimes.entrySet())
- + {
- + CpuTime time = entry.getValue();
- + if (time.total < bestTotal)
- + {
- + bestTotal = time.total;
- + }
- + }
- +
- + for (Entry<String, CpuTime> entry : _cpuTimes.entrySet())
- + {
- + CpuTime time = entry.getValue();
- + System.out.println(entry.getKey() + ": " + ((double) time.total / (double) bestTotal) + "(" + makeStringTime(time.total) + ")");
- + }
- + }
- +
- + public void startMeasure(String name)
- + {
- + CpuTime time = _cpuTimes.get(name);
- + time.measureStart = _tmxb.getCurrentThreadCpuTime();
- + }
- +
- + public void finishMeasure(String name)
- + {
- + long threadCpuTime = _tmxb.getCurrentThreadCpuTime();
- + CpuTime time = _cpuTimes.get(name);
- + long elapsed = threadCpuTime - time.measureStart;
- + if (elapsed > time.worst)
- + {
- + time.worst = elapsed;
- + if (time.worst < _bestWorst)
- + {
- + _bestWorst = time.worst;
- + }
- + }
- + if (elapsed < time.best)
- + {
- + time.best = elapsed;
- + if (time.best < _bestBest)
- + {
- + _bestBest = time.best;
- + }
- + }
- + time.total += elapsed;
- + ++time.measurements;
- + }
- +
- + public long getWorst(String name)
- + {
- + return _cpuTimes.get(name).worst;
- + }
- +
- + public long getBest(String name)
- + {
- + return _cpuTimes.get(name).best;
- + }
- +
- + public long getTotal(String name)
- + {
- + return _cpuTimes.get(name).total;
- + }
- +
- + public long getAvg(String name)
- + {
- + return _cpuTimes.get(name).total / _cpuTimes.get(name).measurements;
- + }
- +
- + public long getBestWorst()
- + {
- + return _bestWorst;
- + }
- +
- + public long getBestBest()
- + {
- + return _bestBest;
- + }
- +}
- Index: java/com/l2jserver/tests/maplookup/Main.java
- ===================================================================
- --- java/com/l2jserver/tests/maplookup/Main.java (revision 0)
- +++ java/com/l2jserver/tests/maplookup/Main.java (revision 0)
- @@ -0,0 +1,135 @@
- +package com.l2jserver.tests.maplookup;
- +
- +import java.security.SecureRandom;
- +import java.util.ArrayList;
- +import java.util.List;
- +import java.util.Random;
- +
- +import com.l2jserver.tests.CpuTimeMeasurer;
- +
- +public final class Main
- +{
- + public static void main(String[] args)
- + {
- + List<WorldObject> objectList = new ArrayList<>();
- +
- + // 10 tests
- + int testStart = (int) System.currentTimeMillis();
- + int testEnd = testStart;
- + testStart -= testEnd;
- + testEnd = testStart + 10;
- +
- + // 100 iterations per test
- + int iterationsStart = (int) System.currentTimeMillis();
- + int iterationsEnd = iterationsStart;
- + iterationsStart -= iterationsEnd;
- + iterationsEnd = iterationsStart + 100;
- +
- + // 1,000,000 iterations per lookup
- + int iterationsObjectStart = (int) System.currentTimeMillis();
- + int iterationsObjectEnd = iterationsObjectStart;
- + iterationsObjectStart -= iterationsObjectEnd;
- + iterationsObjectEnd = iterationsObjectStart + 1000000;
- +
- + // 2,000,000 object id offset
- + int objectIdOffset = 2000000;
- + // 50,000 objects
- + for (int i = 0; i < 50000; ++i)
- + {
- + objectList.add(new WorldObject(i + objectIdOffset));
- + }
- +
- + final ObjectCollections mo = ObjectCollections.getInstance();
- + mo.fillCollections(objectList);
- + final CpuTimeMeasurer ctm = new CpuTimeMeasurer();
- + ctm.addCpuTimer("FastMap<I, O>");
- + ctm.addCpuTimer("FastMap<O, I>");
- + ctm.addCpuTimer("FastSet<O>");
- + ctm.addCpuTimer("TroveMap<I, O>");
- + ctm.addCpuTimer("TroveMap<O, I>");
- + final Random rnd = new SecureRandom();
- +
- + // some warmup
- + for (int i = testStart; i < testEnd; ++i)
- + {
- + for (int j = iterationsStart; j < iterationsEnd; ++j)
- + {
- + final WorldObject object = objectList.get(rnd.nextInt(objectList.size()));
- +
- + for (int k = iterationsObjectStart; k < iterationsObjectEnd; ++k)
- + {
- + mo.addIntObjFastMap(object);
- + }
- +
- + for (int k = iterationsObjectStart; k < iterationsObjectEnd; ++k)
- + {
- + mo.addObjIntFastMap(object);
- + }
- +
- + for (int k = iterationsObjectStart; k < iterationsObjectEnd; ++k)
- + {
- + mo.addObjFastSet(object);
- + }
- +
- + for (int k = iterationsObjectStart; k < iterationsObjectEnd; ++k)
- + {
- + mo.addIntObjTroveMap(object);
- + }
- +
- + for (int k = iterationsObjectStart; k < iterationsObjectEnd; ++k)
- + {
- + mo.addObjIntTroveMap(object);
- + }
- + }
- + }
- +
- + // measurements
- + for (int i = testStart; i < testEnd; ++i)
- + {
- + ctm.resetCpuTimers();
- +
- + for (int j = iterationsStart; j < iterationsEnd; ++j)
- + {
- + final WorldObject object = objectList.get(rnd.nextInt(objectList.size()));
- +
- + ctm.startMeasure("FastMap<I, O>");
- + for (int k = iterationsObjectStart; k < iterationsObjectEnd; ++k)
- + {
- + mo.addIntObjFastMap(object);
- + }
- + ctm.finishMeasure("FastMap<I, O>");
- +
- + ctm.startMeasure("FastMap<O, I>");
- + for (int k = iterationsObjectStart; k < iterationsObjectEnd; ++k)
- + {
- + mo.addObjIntFastMap(object);
- + }
- + ctm.finishMeasure("FastMap<O, I>");
- +
- + ctm.startMeasure("FastSet<O>");
- + for (int k = iterationsObjectStart; k < iterationsObjectEnd; ++k)
- + {
- + mo.addObjFastSet(object);
- + }
- + ctm.finishMeasure("FastSet<O>");
- +
- + ctm.startMeasure("TroveMap<I, O>");
- + for (int k = iterationsObjectStart; k < iterationsObjectEnd; ++k)
- + {
- + mo.addIntObjTroveMap(object);
- + }
- + ctm.finishMeasure("TroveMap<I, O>");
- +
- + ctm.startMeasure("TroveMap<O, I>");
- + for (int k = iterationsObjectStart; k < iterationsObjectEnd; ++k)
- + {
- + mo.addObjIntTroveMap(object);
- + }
- + ctm.finishMeasure("TroveMap<O, I>");
- + }
- +
- + System.out.println("### Test " + (i + 1));
- + ctm.dumpCpuTimers();
- + }
- + }
- +}
- Index: java/com/l2jserver/tests/maplookup/ObjectCollections.java
- ===================================================================
- --- java/com/l2jserver/tests/maplookup/ObjectCollections.java (revision 0)
- +++ java/com/l2jserver/tests/maplookup/ObjectCollections.java (revision 0)
- @@ -0,0 +1,93 @@
- +package com.l2jserver.tests.maplookup;
- +
- +import java.util.List;
- +import java.util.concurrent.locks.ReentrantLock;
- +
- +import javolution.util.FastMap;
- +import javolution.util.FastSet;
- +import gnu.trove.map.hash.TIntObjectHashMap;
- +import gnu.trove.map.hash.TObjectIntHashMap;
- +
- +public final class ObjectCollections
- +{
- + private static final class SingletonHolder
- + {
- + protected static final ObjectCollections INSTANCE = new ObjectCollections();
- + }
- +
- + public static final ObjectCollections getInstance()
- + {
- + return SingletonHolder.INSTANCE;
- + }
- +
- + private final FastMap<Integer, WorldObject> _intObjFastMap;
- + private final FastMap<WorldObject, Integer> _objIntFastMap;
- + private final FastSet<WorldObject> _objFastSet;
- + private final TIntObjectHashMap<WorldObject> _intObjTroveMap;
- + private final TObjectIntHashMap<WorldObject> _objIntTroveMap;
- + private final ReentrantLock _troveLock;
- +
- + protected ObjectCollections()
- + {
- + _intObjFastMap = new FastMap<Integer, WorldObject>().shared();
- + _objIntFastMap = new FastMap<WorldObject, Integer>().shared();
- + _objFastSet = new FastSet<WorldObject>().shared();
- + _intObjTroveMap = new TIntObjectHashMap<>();
- + _objIntTroveMap = new TObjectIntHashMap<>();
- + _troveLock = new ReentrantLock();
- + }
- +
- + public void fillCollections(List<WorldObject> objectList)
- + {
- + _objFastSet.addAll(objectList);
- +
- + for (final WorldObject object : objectList)
- + {
- + _intObjFastMap.put(object.getObjectId(), object);
- + _objIntFastMap.put(object, object.getObjectId());
- + _intObjTroveMap.put(object.getObjectId(), object);
- + _objIntTroveMap.put(object, object.getObjectId());
- + }
- + }
- +
- + public void addIntObjFastMap(final WorldObject object)
- + {
- + _intObjFastMap.putIfAbsent(object.getObjectId(), object);
- + }
- +
- + public void addObjIntFastMap(final WorldObject object)
- + {
- + _objIntFastMap.putIfAbsent(object, object.getObjectId());
- + }
- +
- + public void addObjFastSet(final WorldObject object)
- + {
- + _objFastSet.add(object);
- + }
- +
- + public void addIntObjTroveMap(final WorldObject object)
- + {
- + _troveLock.lock();
- + try
- + {
- + _intObjTroveMap.putIfAbsent(object.getObjectId(), object);
- + }
- + finally
- + {
- + _troveLock.unlock();
- + }
- + }
- +
- + public void addObjIntTroveMap(final WorldObject object)
- + {
- + _troveLock.lock();
- + try
- + {
- + _objIntTroveMap.putIfAbsent(object, object.getObjectId());
- + }
- + finally
- + {
- + _troveLock.unlock();
- + }
- + }
- +}
- Index: java/com/l2jserver/tests/maplookup/WorldObject.java
- ===================================================================
- --- java/com/l2jserver/tests/maplookup/WorldObject.java (revision 0)
- +++ java/com/l2jserver/tests/maplookup/WorldObject.java (revision 0)
- @@ -0,0 +1,32 @@
- +package com.l2jserver.tests.maplookup;
- +
- +public final class WorldObject
- +{
- + private final int _objectId;
- +
- + public WorldObject(final int objectId)
- + {
- + _objectId = objectId;
- + }
- +
- + @Override
- + public int hashCode()
- + {
- + return _objectId;
- + }
- +
- + public int getObjectId()
- + {
- + return _objectId;
- + }
- +
- + @Override
- + public boolean equals(Object other)
- + {
- + if (other == this)
- + {
- + return true;
- + }
- + return (other instanceof WorldObject) && (((WorldObject) other)._objectId == _objectId);
- + }
- +}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement