IDFactoryTest.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * $Header: IDFactoryTest.java, 26/08/2005 01:04:53 luisantonioa Exp $
  3. *
  4. * $Author: luisantonioa $
  5. * $Date: 26/08/2005 01:04:53 $
  6. * $Revision: 1 $
  7. * $Log: IDFactoryTest.java,v $
  8. * Revision 1 26/08/2005 01:04:53 luisantonioa
  9. * Added copyright notice
  10. *
  11. * This program is free software: you can redistribute it and/or modify it under
  12. * the terms of the GNU General Public License as published by the Free Software
  13. * Foundation, either version 3 of the License, or (at your option) any later
  14. * version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  18. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  19. * details.
  20. *
  21. * You should have received a copy of the GNU General Public License along with
  22. * this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. package com.l2jserver.gameserver.idfactory;
  25. import java.util.Vector;
  26. import java.util.concurrent.CountDownLatch;
  27. import java.util.concurrent.atomic.AtomicInteger;
  28. import com.l2jserver.Config;
  29. import com.l2jserver.Server;
  30. import com.l2jserver.Config.IdFactoryType;
  31. import com.l2jserver.gameserver.idfactory.IdFactory;
  32. import com.l2jserver.util.Rnd;
  33. import junit.framework.TestCase;
  34. /**
  35. * This class ...
  36. *
  37. * @version $Revision: 1.2 $ $Date: 2004/06/27 08:12:59 $
  38. */
  39. public class IDFactoryTest extends TestCase
  40. {
  41. private static final boolean _debug = false;
  42. // Compaction, BitSet, Stack, (null to use config)
  43. private static final IdFactoryType FORCED_TYPE = IdFactoryType.Stack;
  44. protected IdFactory _idFactory;
  45. protected AtomicInteger _count = new AtomicInteger(0), _adds = new AtomicInteger(0), _removes = new AtomicInteger(0);
  46. protected static final int REQUESTER_THREADS = 50;
  47. protected static final int REQUESTER_THREAD_REQUESTS = 1000;
  48. protected static final int REQUESTER_THREAD_RANDOM_DELAY = 30;
  49. protected static final int RELEASER_THREADS = 50;
  50. protected static final int RELEASER_THREAD_RELEASES = 1000;
  51. protected static final int RELEASER_THREAD_RANDOM_DELAY = 35;
  52. private static final long F_SLEEP_INTERVAL = 100;
  53. CountDownLatch _latch = new CountDownLatch(REQUESTER_THREADS + RELEASER_THREADS);
  54. protected static Vector<Integer> _map = new Vector<Integer>();
  55. public static void main(String[] args)
  56. {
  57. }
  58. /**
  59. * Constructor for IDFactoryTest.
  60. * @param arg0
  61. */
  62. public IDFactoryTest(String arg0)
  63. {
  64. super(arg0);
  65. Server.serverMode = Server.MODE_GAMESERVER;
  66. Config.load();
  67. if(FORCED_TYPE != null)
  68. Config.IDFACTORY_TYPE = FORCED_TYPE;
  69. _idFactory = IdFactory.getInstance();
  70. }
  71. /*
  72. * @see TestCase#setUp()
  73. */
  74. @Override
  75. protected void setUp() throws Exception
  76. {
  77. super.setUp();
  78. System.out.println("Initial Free ID's: "+IdFactory.FREE_OBJECT_ID_SIZE);
  79. System.out.println("IdFactoryType: "+Config.IDFACTORY_TYPE.name());
  80. /*long startMemoryUse = getMemoryUse();
  81. BitSet freeIds = new BitSet(0x6FFFFFFF);
  82. long endMemoryUse = getMemoryUse();
  83. freeIds.clear();
  84. long approximateSize = ( endMemoryUse - startMemoryUse ) / (1024*1024);
  85. System.out.println("Size: "+approximateSize+"Mb.");*/
  86. }
  87. /*
  88. * @see TestCase#tearDown()
  89. */
  90. @Override
  91. protected void tearDown() throws Exception
  92. {
  93. super.tearDown();
  94. _idFactory = null;
  95. }
  96. /*
  97. * Test method for 'com.l2jserver.gameserver.idfactory.IdFactory.getNextId()'
  98. */
  99. public final void testFactory()
  100. {
  101. System.out.println("Free ID's: "+_idFactory.size());
  102. System.out.println("Used ID's: "+(IdFactory.FREE_OBJECT_ID_SIZE - _idFactory.size()));
  103. _map.add(_idFactory.getNextId());
  104. for (int i=0; i<REQUESTER_THREADS; i++)
  105. {
  106. new Thread(new RequestID(), "Request-Thread-"+i).start();
  107. }
  108. for (int i=0; i<RELEASER_THREADS; i++)
  109. {
  110. new Thread(new ReleaseID(), "Release-Thread-"+i).start();
  111. }
  112. try
  113. {
  114. _latch.await();
  115. }
  116. catch (InterruptedException e)
  117. {
  118. e.printStackTrace();
  119. }
  120. System.out.println("Free ID's: "+_idFactory.size());
  121. System.out.println("Used ID's: "+(IdFactory.FREE_OBJECT_ID_SIZE - _idFactory.size()));
  122. System.out.println("Count: "+_count.get());
  123. }
  124. public class RequestID implements Runnable
  125. {
  126. long _time1;
  127. long _time2;
  128. AtomicInteger _myCount = new AtomicInteger(0);
  129. public void run()
  130. {
  131. for (int i=0; i<REQUESTER_THREAD_REQUESTS; i++)
  132. {
  133. synchronized (_map)
  134. {
  135. _time1 = System.nanoTime();
  136. int newId = _idFactory.getNextId();
  137. _time2 = System.nanoTime() - _time1;
  138. _count.incrementAndGet();
  139. _adds.incrementAndGet();
  140. _myCount.incrementAndGet();
  141. _map.add(newId);
  142. if (_debug) System.out.println("Got new ID "+newId);
  143. if (Rnd.nextInt(10) == 0)
  144. {
  145. System.out.println(" Total ID requests: "+_adds.get()+". "+_time2+"ns");
  146. }
  147. }
  148. try
  149. {
  150. Thread.sleep(Rnd.nextInt(REQUESTER_THREAD_RANDOM_DELAY));
  151. }
  152. catch (InterruptedException e)
  153. {
  154. System.out.println(Thread.currentThread().getName()+" was Interupted.");
  155. }
  156. }
  157. if (_debug) System.out.println(getName()+ " myCount is "+_myCount.get()+"/100.");
  158. _latch.countDown();
  159. }
  160. }
  161. public class ReleaseID implements Runnable
  162. {
  163. AtomicInteger _myCount = new AtomicInteger(100);
  164. long _time1;
  165. long _time2;
  166. public void run()
  167. {
  168. for (int i=0; i<RELEASER_THREAD_RELEASES; i++)
  169. {
  170. synchronized (_map)
  171. {
  172. int size = _map.size();
  173. if (_map.size() <= 0)
  174. {
  175. i--;
  176. continue;
  177. }
  178. //if (size > 0)
  179. //{
  180. int pos = Rnd.nextInt(size);
  181. int id = _map.get(pos);
  182. _time1 = System.nanoTime();
  183. _idFactory.releaseId(id);
  184. _time2 = System.nanoTime() - _time1;
  185. _map.remove(pos);
  186. _count.decrementAndGet();
  187. _myCount.decrementAndGet();
  188. _removes.incrementAndGet();
  189. if (_debug) System.out.println("Released ID "+id);
  190. if (Rnd.nextInt(10) == 0)
  191. {
  192. System.out.println("Total ID releases: "+_removes.get()+". "+_time2+"ns");
  193. }
  194. //}
  195. }
  196. try
  197. {
  198. Thread.sleep(Rnd.nextInt(RELEASER_THREAD_RANDOM_DELAY));
  199. }
  200. catch (InterruptedException e)
  201. {
  202. }
  203. }
  204. if (_debug) System.out.println(getName()+ " count is "+_myCount.get()+"/100.");
  205. _latch.countDown();
  206. }
  207. }
  208. @SuppressWarnings("unused")
  209. private static long getMemoryUse(){
  210. putOutTheGarbage();
  211. long totalMemory = Runtime.getRuntime().totalMemory();
  212. putOutTheGarbage();
  213. long freeMemory = Runtime.getRuntime().freeMemory();
  214. return (totalMemory - freeMemory);
  215. }
  216. private static void putOutTheGarbage() {
  217. collectGarbage();
  218. collectGarbage();
  219. }
  220. private static void collectGarbage() {
  221. try {
  222. System.gc();
  223. Thread.sleep(F_SLEEP_INTERVAL);
  224. System.runFinalization();
  225. Thread.sleep(F_SLEEP_INTERVAL);
  226. }
  227. catch (InterruptedException ex){
  228. ex.printStackTrace();
  229. }
  230. }
  231. }