1 /* 2 * Created on May 19, 2005 3 * Created by Alon Rohter 4 * Copyright (C) Azureus Software, Inc, 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 */ 19 20 package com.aelitis.azureus.core.networkmanager.impl; 21 22 import java.util.*; 23 24 import org.gudy.azureus2.core3.util.Timer; 25 import org.gudy.azureus2.core3.util.TimerEvent; 26 import org.gudy.azureus2.core3.util.TimerEventPerformer; 27 28 public class TransportStats { 29 30 private static final int PRINT_INTERVAL = 60*1000; 31 private static final int GRANULARITY = 10; //bytes 32 33 private final TreeMap read_sizes = new TreeMap(); 34 private final TreeMap write_sizes = new TreeMap(); 35 36 private long total_reads = 0; 37 private long total_writes = 0; 38 39 TransportStats()40 public TransportStats() { 41 Timer printer = new Timer("TransportStats:Printer"); 42 printer.addPeriodicEvent( 43 PRINT_INTERVAL, 44 new TimerEventPerformer() { 45 public void perform( TimerEvent ev ) { 46 printStats(); 47 } 48 } 49 ); 50 } 51 52 bytesRead( int num_bytes_read )53 public void bytesRead( int num_bytes_read ) { 54 total_reads++; 55 updateSizes( read_sizes, num_bytes_read ); 56 } 57 58 bytesWritten( int num_bytes_written )59 public void bytesWritten( int num_bytes_written ) { 60 total_writes++; 61 updateSizes( write_sizes, num_bytes_written ); 62 } 63 64 updateSizes( TreeMap io_sizes, int num_bytes )65 private void updateSizes( TreeMap io_sizes, int num_bytes ) { 66 Integer size_key; 67 68 if( num_bytes == 0 ) { 69 size_key = new Integer( 0 ); 70 } 71 else { 72 size_key = new Integer( (num_bytes / GRANULARITY) +1 ); 73 } 74 75 Long count = (Long)io_sizes.get( size_key ); 76 77 if( count == null ) { 78 io_sizes.put( size_key, new Long( 1 ) ); 79 } 80 else { 81 io_sizes.put( size_key, new Long( count.longValue() +1 ) ); 82 } 83 } 84 85 printStats()86 private void printStats() { 87 System.out.println( "\n------------------------------" ); 88 System.out.println( "***** TCP SOCKET READ SIZE STATS *****" ); 89 printSizes( read_sizes, total_reads ); 90 91 System.out.println( "\n***** TCP SOCKET WRITE SIZE STATS *****" ); 92 printSizes( write_sizes, total_writes ); 93 System.out.println( "------------------------------" ); 94 } 95 96 97 printSizes( TreeMap size_map, long num_total )98 private void printSizes( TreeMap size_map, long num_total ) { 99 int prev_high = 1; 100 101 for( Iterator it = size_map.entrySet().iterator(); it.hasNext(); ) { 102 Map.Entry entry = (Map.Entry)it.next(); 103 int key = ((Integer)entry.getKey()).intValue(); 104 long count = ((Long)entry.getValue()).longValue(); 105 106 long percentage = (count *100) / num_total; 107 108 if( key == 0 ) { 109 if( percentage > 3 ) { 110 System.out.println( "[0 bytes]= x" +percentage+ "%" ); 111 } 112 } 113 else { 114 int high = key * GRANULARITY; 115 116 if( percentage > 3 ) { 117 System.out.println( "[" +prev_high+ "-" +(high -1)+ " bytes]= x" +percentage+ "%" ); 118 } 119 120 prev_high = high; 121 } 122 } 123 } 124 125 126 } 127