Advertisement
Guest User

Untitled

a guest
Dec 27th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 10.95 KB | None | 0 0
  1. Index: java/com/l2jserver/tests/CpuTimeMeasurer.java
  2. ===================================================================
  3. --- java/com/l2jserver/tests/CpuTimeMeasurer.java   (revision 0)
  4. +++ java/com/l2jserver/tests/CpuTimeMeasurer.java   (revision 0)
  5. @@ -0,0 +1,134 @@
  6. +package com.l2jserver.tests;
  7. +
  8. +import java.lang.management.ManagementFactory;
  9. +import java.lang.management.ThreadMXBean;
  10. +import java.util.HashMap;
  11. +import java.util.Map;
  12. +import java.util.Map.Entry;
  13. +
  14. +public final class CpuTimeMeasurer
  15. +{
  16. +   private final class CpuTime
  17. +   {
  18. +       public long measurements = 0;
  19. +       public long measureStart = 0;
  20. +       public long worst = Long.MIN_VALUE;
  21. +       public long best = Long.MAX_VALUE;
  22. +       public long total = 0;
  23. +      
  24. +       protected CpuTime()
  25. +       {
  26. +       }
  27. +   }
  28. +  
  29. +   private final ThreadMXBean _tmxb = ManagementFactory.getThreadMXBean();
  30. +   private final Map<String, CpuTime> _cpuTimes = new HashMap<>();
  31. +   private long _bestWorst = Long.MAX_VALUE;
  32. +   private long _bestBest = Long.MAX_VALUE;
  33. +  
  34. +   public void addCpuTimer(String name)
  35. +   {
  36. +       _cpuTimes.put(name, new CpuTime());
  37. +   }
  38. +  
  39. +   private String makeStringTime(long nanos)
  40. +   {
  41. +       long seconds = nanos / 1000000 / 1000;
  42. +       nanos -= seconds * 1000 * 1000000;
  43. +       long millis = nanos / 1000000;
  44. +       nanos -= millis * 1000000;
  45. +       return seconds + "s " + millis + "ms " + nanos + "ns";
  46. +   }
  47. +  
  48. +   public void resetCpuTimers()
  49. +   {
  50. +       for (CpuTime time : _cpuTimes.values())
  51. +       {
  52. +           time.measurements = 0;
  53. +           time.measureStart = 0;
  54. +           time.worst = Long.MIN_VALUE;
  55. +           time.best = Long.MAX_VALUE;
  56. +           time.total = 0;
  57. +       }
  58. +   }
  59. +  
  60. +   public void dumpCpuTimers()
  61. +   {
  62. +       long bestTotal = Long.MAX_VALUE;
  63. +       for (Entry<String, CpuTime> entry : _cpuTimes.entrySet())
  64. +       {
  65. +           CpuTime time = entry.getValue();
  66. +           if (time.total < bestTotal)
  67. +           {
  68. +               bestTotal = time.total;
  69. +           }
  70. +       }
  71. +      
  72. +       for (Entry<String, CpuTime> entry : _cpuTimes.entrySet())
  73. +       {
  74. +           CpuTime time = entry.getValue();
  75. +           System.out.println(entry.getKey() + ": " + ((double) time.total / (double) bestTotal) + "(" + makeStringTime(time.total) + ")");
  76. +       }
  77. +   }
  78. +  
  79. +   public void startMeasure(String name)
  80. +   {
  81. +       CpuTime time = _cpuTimes.get(name);
  82. +       time.measureStart = _tmxb.getCurrentThreadCpuTime();
  83. +   }
  84. +  
  85. +   public void finishMeasure(String name)
  86. +   {
  87. +       long threadCpuTime = _tmxb.getCurrentThreadCpuTime();
  88. +       CpuTime time = _cpuTimes.get(name);
  89. +       long elapsed = threadCpuTime - time.measureStart;
  90. +       if (elapsed > time.worst)
  91. +       {
  92. +           time.worst = elapsed;
  93. +           if (time.worst < _bestWorst)
  94. +           {
  95. +               _bestWorst = time.worst;
  96. +           }
  97. +       }
  98. +       if (elapsed < time.best)
  99. +       {
  100. +           time.best = elapsed;
  101. +           if (time.best < _bestBest)
  102. +           {
  103. +               _bestBest = time.best;
  104. +           }
  105. +       }
  106. +       time.total += elapsed;
  107. +       ++time.measurements;
  108. +   }
  109. +  
  110. +   public long getWorst(String name)
  111. +   {
  112. +       return _cpuTimes.get(name).worst;
  113. +   }
  114. +  
  115. +   public long getBest(String name)
  116. +   {
  117. +       return _cpuTimes.get(name).best;
  118. +   }
  119. +  
  120. +   public long getTotal(String name)
  121. +   {
  122. +       return _cpuTimes.get(name).total;
  123. +   }
  124. +  
  125. +   public long getAvg(String name)
  126. +   {
  127. +       return _cpuTimes.get(name).total / _cpuTimes.get(name).measurements;
  128. +   }
  129. +  
  130. +   public long getBestWorst()
  131. +   {
  132. +       return _bestWorst;
  133. +   }
  134. +  
  135. +   public long getBestBest()
  136. +   {
  137. +       return _bestBest;
  138. +   }
  139. +}
  140. Index: java/com/l2jserver/tests/maplookup/Main.java
  141. ===================================================================
  142. --- java/com/l2jserver/tests/maplookup/Main.java    (revision 0)
  143. +++ java/com/l2jserver/tests/maplookup/Main.java    (revision 0)
  144. @@ -0,0 +1,135 @@
  145. +package com.l2jserver.tests.maplookup;
  146. +
  147. +import java.security.SecureRandom;
  148. +import java.util.ArrayList;
  149. +import java.util.List;
  150. +import java.util.Random;
  151. +
  152. +import com.l2jserver.tests.CpuTimeMeasurer;
  153. +
  154. +public final class Main
  155. +{
  156. +   public static void main(String[] args)
  157. +   {
  158. +       List<WorldObject> objectList = new ArrayList<>();
  159. +      
  160. +       // 10 tests
  161. +       int testStart = (int) System.currentTimeMillis();
  162. +       int testEnd = testStart;
  163. +       testStart -= testEnd;
  164. +       testEnd = testStart + 10;
  165. +      
  166. +       // 100 iterations per test
  167. +       int iterationsStart = (int) System.currentTimeMillis();
  168. +       int iterationsEnd = iterationsStart;
  169. +       iterationsStart -= iterationsEnd;
  170. +       iterationsEnd = iterationsStart + 100;
  171. +      
  172. +       // 1,000,000 iterations per lookup
  173. +       int iterationsObjectStart = (int) System.currentTimeMillis();
  174. +       int iterationsObjectEnd = iterationsObjectStart;
  175. +       iterationsObjectStart -= iterationsObjectEnd;
  176. +       iterationsObjectEnd = iterationsObjectStart + 1000000;
  177. +      
  178. +       // 2,000,000 object id offset
  179. +       int objectIdOffset = 2000000;
  180. +       // 50,000 objects
  181. +       for (int i = 0; i < 50000; ++i)
  182. +       {
  183. +           objectList.add(new WorldObject(i + objectIdOffset));
  184. +       }
  185. +      
  186. +       final ObjectCollections mo = ObjectCollections.getInstance();
  187. +       mo.fillCollections(objectList);
  188. +       final CpuTimeMeasurer ctm = new CpuTimeMeasurer();
  189. +       ctm.addCpuTimer("FastMap<I, O>");
  190. +       ctm.addCpuTimer("FastMap<O, I>");
  191. +       ctm.addCpuTimer("FastSet<O>");
  192. +       ctm.addCpuTimer("TroveMap<I, O>");
  193. +       ctm.addCpuTimer("TroveMap<O, I>");
  194. +       final Random rnd = new SecureRandom();
  195. +      
  196. +       // some warmup
  197. +       for (int i = testStart; i < testEnd; ++i)
  198. +       {
  199. +           for (int j = iterationsStart; j < iterationsEnd; ++j)
  200. +           {
  201. +               final WorldObject object = objectList.get(rnd.nextInt(objectList.size()));
  202. +              
  203. +               for (int k = iterationsObjectStart; k < iterationsObjectEnd; ++k)
  204. +               {
  205. +                   mo.addIntObjFastMap(object);
  206. +               }
  207. +              
  208. +               for (int k = iterationsObjectStart; k < iterationsObjectEnd; ++k)
  209. +               {
  210. +                   mo.addObjIntFastMap(object);
  211. +               }
  212. +              
  213. +               for (int k = iterationsObjectStart; k < iterationsObjectEnd; ++k)
  214. +               {
  215. +                   mo.addObjFastSet(object);
  216. +               }
  217. +              
  218. +               for (int k = iterationsObjectStart; k < iterationsObjectEnd; ++k)
  219. +               {
  220. +                   mo.addIntObjTroveMap(object);
  221. +               }
  222. +              
  223. +               for (int k = iterationsObjectStart; k < iterationsObjectEnd; ++k)
  224. +               {
  225. +                   mo.addObjIntTroveMap(object);
  226. +               }
  227. +           }
  228. +       }
  229. +      
  230. +       // measurements
  231. +       for (int i = testStart; i < testEnd; ++i)
  232. +       {
  233. +           ctm.resetCpuTimers();
  234. +          
  235. +           for (int j = iterationsStart; j < iterationsEnd; ++j)
  236. +           {
  237. +               final WorldObject object = objectList.get(rnd.nextInt(objectList.size()));
  238. +              
  239. +               ctm.startMeasure("FastMap<I, O>");
  240. +               for (int k = iterationsObjectStart; k < iterationsObjectEnd; ++k)
  241. +               {
  242. +                   mo.addIntObjFastMap(object);
  243. +               }
  244. +               ctm.finishMeasure("FastMap<I, O>");
  245. +              
  246. +               ctm.startMeasure("FastMap<O, I>");
  247. +               for (int k = iterationsObjectStart; k < iterationsObjectEnd; ++k)
  248. +               {
  249. +                   mo.addObjIntFastMap(object);
  250. +               }
  251. +               ctm.finishMeasure("FastMap<O, I>");
  252. +              
  253. +               ctm.startMeasure("FastSet<O>");
  254. +               for (int k = iterationsObjectStart; k < iterationsObjectEnd; ++k)
  255. +               {
  256. +                   mo.addObjFastSet(object);
  257. +               }
  258. +               ctm.finishMeasure("FastSet<O>");
  259. +              
  260. +               ctm.startMeasure("TroveMap<I, O>");
  261. +               for (int k = iterationsObjectStart; k < iterationsObjectEnd; ++k)
  262. +               {
  263. +                   mo.addIntObjTroveMap(object);
  264. +               }
  265. +               ctm.finishMeasure("TroveMap<I, O>");
  266. +              
  267. +               ctm.startMeasure("TroveMap<O, I>");
  268. +               for (int k = iterationsObjectStart; k < iterationsObjectEnd; ++k)
  269. +               {
  270. +                   mo.addObjIntTroveMap(object);
  271. +               }
  272. +               ctm.finishMeasure("TroveMap<O, I>");
  273. +           }
  274. +          
  275. +           System.out.println("### Test " + (i + 1));
  276. +           ctm.dumpCpuTimers();
  277. +       }
  278. +   }
  279. +}
  280. Index: java/com/l2jserver/tests/maplookup/ObjectCollections.java
  281. ===================================================================
  282. --- java/com/l2jserver/tests/maplookup/ObjectCollections.java   (revision 0)
  283. +++ java/com/l2jserver/tests/maplookup/ObjectCollections.java   (revision 0)
  284. @@ -0,0 +1,93 @@
  285. +package com.l2jserver.tests.maplookup;
  286. +
  287. +import java.util.List;
  288. +import java.util.concurrent.locks.ReentrantLock;
  289. +
  290. +import javolution.util.FastMap;
  291. +import javolution.util.FastSet;
  292. +import gnu.trove.map.hash.TIntObjectHashMap;
  293. +import gnu.trove.map.hash.TObjectIntHashMap;
  294. +
  295. +public final class ObjectCollections
  296. +{
  297. +   private static final class SingletonHolder
  298. +   {
  299. +       protected static final ObjectCollections INSTANCE = new ObjectCollections();
  300. +   }
  301. +  
  302. +   public static final ObjectCollections getInstance()
  303. +   {
  304. +       return SingletonHolder.INSTANCE;
  305. +   }
  306. +  
  307. +   private final FastMap<Integer, WorldObject> _intObjFastMap;
  308. +   private final FastMap<WorldObject, Integer> _objIntFastMap;
  309. +   private final FastSet<WorldObject> _objFastSet;
  310. +   private final TIntObjectHashMap<WorldObject> _intObjTroveMap;
  311. +   private final TObjectIntHashMap<WorldObject> _objIntTroveMap;
  312. +   private final ReentrantLock _troveLock;
  313. +  
  314. +   protected ObjectCollections()
  315. +   {
  316. +       _intObjFastMap = new FastMap<Integer, WorldObject>().shared();
  317. +       _objIntFastMap = new FastMap<WorldObject, Integer>().shared();
  318. +       _objFastSet = new FastSet<WorldObject>().shared();
  319. +       _intObjTroveMap = new TIntObjectHashMap<>();
  320. +       _objIntTroveMap = new TObjectIntHashMap<>();
  321. +       _troveLock = new ReentrantLock();
  322. +   }
  323. +  
  324. +   public void fillCollections(List<WorldObject> objectList)
  325. +   {
  326. +       _objFastSet.addAll(objectList);
  327. +      
  328. +       for (final WorldObject object : objectList)
  329. +       {
  330. +           _intObjFastMap.put(object.getObjectId(), object);
  331. +           _objIntFastMap.put(object, object.getObjectId());
  332. +           _intObjTroveMap.put(object.getObjectId(), object);
  333. +           _objIntTroveMap.put(object, object.getObjectId());
  334. +       }
  335. +   }
  336. +  
  337. +   public void addIntObjFastMap(final WorldObject object)
  338. +   {
  339. +       _intObjFastMap.putIfAbsent(object.getObjectId(), object);
  340. +   }
  341. +  
  342. +   public void addObjIntFastMap(final WorldObject object)
  343. +   {
  344. +       _objIntFastMap.putIfAbsent(object, object.getObjectId());
  345. +   }
  346. +  
  347. +   public void addObjFastSet(final WorldObject object)
  348. +   {
  349. +       _objFastSet.add(object);
  350. +   }
  351. +  
  352. +   public void addIntObjTroveMap(final WorldObject object)
  353. +   {
  354. +       _troveLock.lock();
  355. +       try
  356. +       {
  357. +           _intObjTroveMap.putIfAbsent(object.getObjectId(), object);
  358. +       }
  359. +       finally
  360. +       {
  361. +           _troveLock.unlock();
  362. +       }
  363. +   }
  364. +  
  365. +   public void addObjIntTroveMap(final WorldObject object)
  366. +   {
  367. +       _troveLock.lock();
  368. +       try
  369. +       {
  370. +           _objIntTroveMap.putIfAbsent(object, object.getObjectId());
  371. +       }
  372. +       finally
  373. +       {
  374. +           _troveLock.unlock();
  375. +       }
  376. +   }
  377. +}
  378. Index: java/com/l2jserver/tests/maplookup/WorldObject.java
  379. ===================================================================
  380. --- java/com/l2jserver/tests/maplookup/WorldObject.java (revision 0)
  381. +++ java/com/l2jserver/tests/maplookup/WorldObject.java (revision 0)
  382. @@ -0,0 +1,32 @@
  383. +package com.l2jserver.tests.maplookup;
  384. +
  385. +public final class WorldObject
  386. +{
  387. +   private final int _objectId;
  388. +  
  389. +   public WorldObject(final int objectId)
  390. +   {
  391. +       _objectId = objectId;
  392. +   }
  393. +  
  394. +   @Override
  395. +   public int hashCode()
  396. +   {
  397. +       return _objectId;
  398. +   }
  399. +  
  400. +   public int getObjectId()
  401. +   {
  402. +       return _objectId;
  403. +   }
  404. +  
  405. +   @Override
  406. +   public boolean equals(Object other)
  407. +   {
  408. +       if (other == this)
  409. +       {
  410. +           return true;
  411. +       }
  412. +       return (other instanceof WorldObject) && (((WorldObject) other)._objectId == _objectId);
  413. +   }
  414. +}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement