PageRenderTime 91ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/azureus-4.7.0.2/com/aelitis/azureus/core/peermanager/unchoker/UnchokerUtilTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 312 lines | 231 code | 39 blank | 42 comment | 9 complexity | e3ba67ad9a761d42de34db379dea54f7 MD5 | raw file
  1. /*
  2. * Created on May 28, 2005
  3. * Created by Alon Rohter
  4. * Copyright (C) 2005, 2006 Aelitis, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * AELITIS, SAS au capital de 46,603.30 euros
  19. * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
  20. *
  21. */
  22. package com.aelitis.azureus.core.peermanager.unchoker;
  23. import java.net.InetAddress;
  24. import java.util.*;
  25. import org.gudy.azureus2.core3.disk.DiskManagerReadRequest;
  26. import org.gudy.azureus2.core3.peer.PEPeer;
  27. import org.gudy.azureus2.core3.peer.PEPeerListener;
  28. import org.gudy.azureus2.core3.peer.PEPeerManager;
  29. import org.gudy.azureus2.core3.peer.PEPeerStats;
  30. import org.gudy.azureus2.core3.util.RandomUtils;
  31. import org.gudy.azureus2.plugins.network.Connection;
  32. import com.aelitis.azureus.core.networkmanager.LimitedRateGroup;
  33. import com.aelitis.azureus.core.peermanager.messaging.Message;
  34. import com.aelitis.azureus.core.peermanager.piecepicker.util.BitFlags;
  35. /**
  36. * @author MjrTom
  37. * 2005/Oct/08: s/getLastPiece
  38. * 2006/Jan/02: use RandomUtils
  39. */
  40. public class UnchokerUtilTest {
  41. private static final int NUM_PEERS_TO_TEST = 100;
  42. private static final int BYTE_RANGE = 100*1024*1024;
  43. private static final int TEST_ROUNDS = 1000000;
  44. public static void main(String[] args) {
  45. TreeMap counts = new TreeMap( new Comparator(){
  46. public int compare( Object o1, Object o2 ) {
  47. PEPeer peer1 = (PEPeer)o1;
  48. PEPeer peer2 = (PEPeer)o2;
  49. long score1 = peer1.getStats().getTotalDataBytesSent() - peer1.getStats().getTotalDataBytesReceived();
  50. long score2 = peer2.getStats().getTotalDataBytesSent() - peer2.getStats().getTotalDataBytesReceived();
  51. return (int)(score1 - score2);
  52. }
  53. });
  54. ArrayList test_peers = generateTestPeers();
  55. for( int i=0; i < TEST_ROUNDS; i++ ) {
  56. if( i % 100000 == 0 ) System.out.println( "round=" +i );
  57. PEPeer opt_peer = UnchokerUtil.getNextOptimisticPeer( test_peers, true, false );
  58. Integer count = (Integer)counts.get( opt_peer );
  59. if( count == null ) count = new Integer( 0 );
  60. counts.put( opt_peer, new Integer( count.intValue() + 1 ) );
  61. }
  62. int max_picked = 0;
  63. for( Iterator it = counts.values().iterator(); it.hasNext(); ) {
  64. int count = ((Integer)it.next()).intValue();
  65. if( count > max_picked ) max_picked = count;
  66. }
  67. int pos = 0;
  68. for( Iterator it = counts.entrySet().iterator(); it.hasNext(); ) {
  69. Map.Entry entry = (Map.Entry)it.next();
  70. PEPeer peer = (PEPeer)entry.getKey();
  71. int count = ((Integer)entry.getValue()).intValue();
  72. long score = peer.getStats().getTotalDataBytesSent() - peer.getStats().getTotalDataBytesReceived();
  73. float ratio = (float)peer.getStats().getTotalDataBytesSent() / (peer.getStats().getTotalDataBytesReceived() + 1);
  74. int percentile = (count *100) / max_picked;
  75. System.out.println( "[" +pos+ "] score=" +score+ ", ratio=" +ratio+ ", picked=" +count+ "x, percentile=" +percentile+ "%" );
  76. pos++;
  77. }
  78. }
  79. private static ArrayList generateTestPeers() {
  80. ArrayList peers = new ArrayList();
  81. for( int i=0; i < NUM_PEERS_TO_TEST; i++ ) {
  82. final int bytes_received = RandomUtils.nextInt( BYTE_RANGE );
  83. final int bytes_sent = RandomUtils.nextInt( BYTE_RANGE );
  84. final PEPeerStats[] f_stats = { null };
  85. final PEPeer peer = new PEPeer() {
  86. public InetAddress getAlternativeIPv6() { return null; }
  87. public void addListener( PEPeerListener listener ){}
  88. public void removeListener( PEPeerListener listener ){}
  89. public int getPeerState(){ return PEPeer.TRANSFERING; }
  90. public PEPeerManager getManager(){ return null; }
  91. public String getPeerSource(){ return null; }
  92. public byte[] getId(){ return null; }
  93. public String getIp(){ return null; }
  94. public int getPort(){ return 0; }
  95. public String getIPHostName(){ return null; }
  96. public int getTCPListenPort(){ return 0; }
  97. public int getUDPListenPort(){ return 0; }
  98. public int getUDPNonDataListenPort() { return 0;}
  99. public BitFlags getAvailable(){ return null; }
  100. public boolean isPieceAvailable(int pieceNumber){ return false; }
  101. public boolean transferAvailable(){ return true; }
  102. public boolean isDownloadPossible() { return true; }
  103. public void setSnubbed(boolean b){}
  104. public boolean isChokingMe(){ return true; }
  105. public boolean isChokedByMe() { return true; }
  106. public void sendChoke(){}
  107. public void sendUnChoke(){}
  108. public void sendStatsRequest(Map request) {}
  109. public boolean isInteresting(){ return true; }
  110. public boolean isInterested(){ return true; }
  111. public boolean isRelativeSeed() { return false; }
  112. public boolean isSeed(){ return false; }
  113. public boolean isSnubbed(){ return false; }
  114. public long getSnubbedTime() { return 0; }
  115. public boolean hasReceivedBitField() {return false; }
  116. public PEPeerStats getStats(){ return f_stats[0]; }
  117. public boolean isIncoming(){ return false; }
  118. public int getPercentDoneInThousandNotation(){ return 0; }
  119. public String getClient(){ return null; }
  120. public boolean isOptimisticUnchoke(){ return false; }
  121. public void setOptimisticUnchoke( boolean is_optimistic ){}
  122. public void setUploadHint(int timeToSpread){}
  123. public int getUploadHint(){ return 0; }
  124. public void setUniqueAnnounce(int uniquePieceNumber){}
  125. public int getUniqueAnnounce(){ return 0; }
  126. public Object getData (String key){ return null; }
  127. public void setData (String key, Object value){}
  128. public Connection getPluginConnection(){ return null; }
  129. public boolean supportsMessaging(){ return false; }
  130. public int getMessagingMode(){ return PEPeer.MESSAGING_BT_ONLY; }
  131. public Message[] getSupportedMessages(){ return null; }
  132. public String getEncryption(){ return( "" ); }
  133. public String getProtocol(){ return( "" ); }
  134. public int getReservedPieceNumber() { return -1; }
  135. public void addReservedPieceNumber(int pieceNumber) {}
  136. public void removeReservedPieceNumber(int pieceNumber) {}
  137. public int[] getReservedPieceNumbers() { return null; }
  138. public int[] getIncomingRequestedPieceNumbers() { return null; }
  139. public int[] getOutgoingRequestedPieceNumbers() { return null; }
  140. public int getPercentDoneOfCurrentIncomingRequest(){ return 0; }
  141. public int getPercentDoneOfCurrentOutgoingRequest(){ return 0; }
  142. public long getTimeSinceConnectionEstablished(){ return 0; }
  143. public int getLastPiece() { return -1; }
  144. public void setLastPiece(int pieceNumber) {}
  145. public int getConsecutiveNoRequestCount() {return 0; }
  146. public void setConsecutiveNoRequestCount(int num) {}
  147. public void setSuspendedLazyBitFieldEnabled(boolean enable) {
  148. }
  149. public int getIncomingRequestCount() {
  150. // TODO Auto-generated method stub
  151. return 0;
  152. }
  153. public int getOutgoingRequestCount() {
  154. // TODO Auto-generated method stub
  155. return 0;
  156. }
  157. public boolean isLANLocal() {
  158. // TODO Auto-generated method stub
  159. return false;
  160. }
  161. public boolean sendRequestHint(int piece_number, int offset, int length, int life) {
  162. // TODO Auto-generated method stub
  163. return false;
  164. }
  165. public int[] getRequestHint() {
  166. // TODO Auto-generated method stub
  167. return null;
  168. }
  169. public void
  170. clearRequestHint()
  171. {
  172. }
  173. public void sendRejectRequest(DiskManagerReadRequest request) {
  174. // TODO Auto-generated method stub
  175. }
  176. public void setUploadRateLimitBytesPerSecond( int bytes ){}
  177. public void setDownloadRateLimitBytesPerSecond( int bytes ){}
  178. public int getUploadRateLimitBytesPerSecond(){ return 0 ;}
  179. public int getDownloadRateLimitBytesPerSecond(){ return 0; }
  180. public void addRateLimiter(LimitedRateGroup limiter, boolean upload) {
  181. // TODO Auto-generated method stub
  182. }
  183. public void removeRateLimiter(LimitedRateGroup limiter, boolean upload) {
  184. // TODO Auto-generated method stub
  185. }
  186. public void setHaveAggregationEnabled(boolean enabled) {
  187. // TODO Auto-generated method stub
  188. }
  189. public int getOutboundDataQueueSize() {
  190. // TODO Auto-generated method stub
  191. return 0;
  192. }
  193. public byte[] getHandshakeReservedBytes() {
  194. return null;
  195. }
  196. public String getClientNameFromExtensionHandshake() {return null;}
  197. public String getClientNameFromPeerID() {return null;}
  198. public long getBytesRemaining() {
  199. // TODO Auto-generated method stub
  200. return 0;
  201. }
  202. public Object getUserData(Object key) {
  203. // TODO Auto-generated method stub
  204. return null;
  205. }
  206. public void setUserData(Object key, Object value) {
  207. // TODO Auto-generated method stub
  208. }
  209. public boolean isPriorityConnection() {
  210. // TODO Auto-generated method stub
  211. return false;
  212. }
  213. public void setPriorityConnection(boolean is_priority) {
  214. // TODO Auto-generated method stub
  215. }
  216. public boolean isUnchokeOverride() {
  217. // TODO Auto-generated method stub
  218. return false;
  219. }
  220. };
  221. f_stats[0] = new PEPeerStats() {
  222. public PEPeer getPeer() {return( peer );}
  223. public void setPeer(PEPeer p) {}
  224. public void dataBytesSent( int num_bytes ){}
  225. public void protocolBytesSent( int num_bytes ){}
  226. public void dataBytesReceived( int num_bytes ){}
  227. public void protocolBytesReceived( int num_bytes ){}
  228. public void bytesDiscarded( int num_bytes ){}
  229. public void hasNewPiece( int piece_size ){}
  230. public void statisticalSentPiece( int piece_size ){}
  231. public long getDataReceiveRate(){ return 0; }
  232. public long getProtocolReceiveRate(){ return 0; }
  233. public long getTotalDataBytesReceived(){ return bytes_received; }
  234. public long getTotalProtocolBytesReceived(){ return 0; }
  235. public long getDataSendRate(){ return 0; }
  236. public long getProtocolSendRate(){ return 0; }
  237. public long getTotalDataBytesSent(){ return bytes_sent; }
  238. public long getTotalProtocolBytesSent(){ return 0; }
  239. public long getSmoothDataReceiveRate(){ return 0; }
  240. public long getTotalBytesDiscarded(){ return 0; }
  241. public long getEstimatedDownloadRateOfPeer(){ return 0; }
  242. public long getEstimatedUploadRateOfPeer(){ return 0; }
  243. public long getTotalBytesDownloadedByPeer(){ return 0; }
  244. public void diskReadComplete( long bytes ){};
  245. public int getTotalDiskReadCount(){ return 0; }
  246. public int getAggregatedDiskReadCount(){ return 0; }
  247. public long getTotalDiskReadBytes(){ return 0; }
  248. public void setUploadRateLimitBytesPerSecond( int bytes ){}
  249. public void setDownloadRateLimitBytesPerSecond( int bytes ){}
  250. public int getUploadRateLimitBytesPerSecond(){return 0;}
  251. public int getDownloadRateLimitBytesPerSecond(){return 0;}
  252. public long getEstimatedSecondsToCompletion(){return(0);};
  253. public int getPermittedBytesToSend(){ return 0; }
  254. public void permittedSendBytesUsed( int num ){}
  255. public int getPermittedBytesToReceive(){ return 0; }
  256. public void permittedReceiveBytesUsed( int num ){}
  257. };
  258. peers.add( peer );
  259. }
  260. return peers;
  261. }
  262. public static class
  263. UF
  264. extends UnchokerFactory
  265. {
  266. public Unchoker
  267. getUnchoker(
  268. boolean seeding)
  269. {
  270. return super.getUnchoker(seeding);
  271. }
  272. }
  273. }