| 1 | package planet.simulate; |
|---|
| 2 | |
|---|
| 3 | import java.util.*; |
|---|
| 4 | |
|---|
| 5 | import planet.commonapi.Id; |
|---|
| 6 | |
|---|
| 7 | /** |
|---|
| 8 | * @author Ruben Mondejar |
|---|
| 9 | * @author Marc Sánchez <msa.ei@estudiants.urv.es> |
|---|
| 10 | * Node stress |
|---|
| 11 | * Link stress |
|---|
| 12 | * Average messages/queues (IN/OUT) |
|---|
| 13 | * Average total messages / time unit (t) |
|---|
| 14 | * Average hops/message (specific) |
|---|
| 15 | * Total number of messages |
|---|
| 16 | */ |
|---|
| 17 | public class Results { |
|---|
| 18 | |
|---|
| 19 | private static Hashtable results = new Hashtable(); |
|---|
| 20 | private static Hashtable lookups = new Hashtable(); |
|---|
| 21 | private static Hashtable inserts = new Hashtable(); |
|---|
| 22 | private static Hashtable hops = new Hashtable(); |
|---|
| 23 | private static int ringSize = 0; |
|---|
| 24 | private static int stabRate = 0; |
|---|
| 25 | private static int traffic = 0; |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | public static void incStabRate() { |
|---|
| 29 | stabRate++; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | public static void clearStabRate() { |
|---|
| 33 | stabRate=0; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | public static int getStabRate() { |
|---|
| 37 | return stabRate; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | public static void incTraffic() { |
|---|
| 41 | traffic++; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | public static void decTraffic() { |
|---|
| 45 | traffic--; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | public static void incTrafficBy(int inc) { |
|---|
| 49 | traffic = traffic + inc; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | public static int getTraffic() { |
|---|
| 53 | return traffic; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | //type --> in/out |
|---|
| 59 | public static void numMessagesTime(Id id, int step, int num, String type) { |
|---|
| 60 | updateNodeStress(id,num,type); |
|---|
| 61 | addMaxMessages(id,step,num,type); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | private static void updateNodeStress(Id id, int num, String type){ |
|---|
| 65 | |
|---|
| 66 | Hashtable node_info; |
|---|
| 67 | Integer stress; |
|---|
| 68 | |
|---|
| 69 | if (results.containsKey(id)) { |
|---|
| 70 | node_info = (Hashtable) results.get(id); |
|---|
| 71 | if (node_info.containsKey("Stress "+type)) { |
|---|
| 72 | stress = (Integer) node_info.get("Stress "+type); |
|---|
| 73 | int value = stress.intValue(); |
|---|
| 74 | value+=num; |
|---|
| 75 | node_info.put("Stress "+type,new Integer(value)); |
|---|
| 76 | results.put(id,node_info); |
|---|
| 77 | } |
|---|
| 78 | else { |
|---|
| 79 | node_info.put("Stress "+type,new Integer(num)); |
|---|
| 80 | results.put(id,node_info); |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | else { |
|---|
| 84 | node_info = new Hashtable(); |
|---|
| 85 | node_info.put("Stress "+type,new Integer(num)); |
|---|
| 86 | results.put(id,node_info); |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | private static void addMaxMessages(Id id, int step, int num ,String type){ |
|---|
| 91 | |
|---|
| 92 | Hashtable node_info; |
|---|
| 93 | Vector max; |
|---|
| 94 | |
|---|
| 95 | if (results.containsKey(id)) { |
|---|
| 96 | node_info = (Hashtable) results.get(id); |
|---|
| 97 | if (node_info.containsKey("Num max "+type)) { |
|---|
| 98 | max = (Vector) node_info.get("Num max "+type); |
|---|
| 99 | int value = ((Integer) max.get(1)).intValue(); |
|---|
| 100 | if (num>value) { |
|---|
| 101 | max.clear(); |
|---|
| 102 | max.add(new Integer(step)); |
|---|
| 103 | max.add(new Integer(num)); |
|---|
| 104 | node_info.put("Num max "+type,max); |
|---|
| 105 | results.put(id,node_info); |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | else { |
|---|
| 109 | max = new Vector(); |
|---|
| 110 | max.add(new Integer(step)); |
|---|
| 111 | max.add(new Integer(num)); |
|---|
| 112 | node_info.put("Num max "+type,max); |
|---|
| 113 | results.put(id,node_info); |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | else { |
|---|
| 117 | max = new Vector(); |
|---|
| 118 | max.add(new Integer(step)); |
|---|
| 119 | max.add(new Integer(num)); |
|---|
| 120 | |
|---|
| 121 | node_info = new Hashtable(); |
|---|
| 122 | node_info.put("Num max "+type,max); |
|---|
| 123 | results.put(id,node_info); |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | public static void updateHopsMsg(Id source, String key_msg){ |
|---|
| 128 | |
|---|
| 129 | String identif = source+" "+key_msg; |
|---|
| 130 | |
|---|
| 131 | if (hops.containsKey(identif)) { |
|---|
| 132 | int num = ((Integer) hops.get(identif)).intValue() + 1; |
|---|
| 133 | hops.put(identif,new Integer(num)); |
|---|
| 134 | } |
|---|
| 135 | else { |
|---|
| 136 | hops.put(identif,new Integer(1)); |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | public static void addInsert(String key,String dest) { |
|---|
| 141 | inserts.put(key,dest); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | public static String getInsert(String key) { |
|---|
| 145 | return (String) inserts.get(key); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | public static void resetInserts() { |
|---|
| 149 | inserts.clear(); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | public static Hashtable getAllInserts() { |
|---|
| 153 | return (Hashtable) inserts.clone(); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | public static void addLookup(Id key, Id own) { |
|---|
| 157 | lookups.put(key,own); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | public static Id getLookup(Id key) { |
|---|
| 161 | return ((Id) lookups.get(key)); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | public static void resetLookups(){ |
|---|
| 165 | lookups.clear(); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | public static Hashtable getAllLookups() { |
|---|
| 169 | return (Hashtable) lookups.clone(); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | public static void print () { |
|---|
| 173 | long total_in_msg = 0; |
|---|
| 174 | long total_out_msg = 0; |
|---|
| 175 | |
|---|
| 176 | Logger.log("************************",Logger.PRINT_LOG); |
|---|
| 177 | Logger.log(" ** Node Results **",Logger.PRINT_LOG); |
|---|
| 178 | Logger.log("************************",Logger.PRINT_LOG); |
|---|
| 179 | Collection c = results.keySet(); |
|---|
| 180 | ringSize = c.size(); |
|---|
| 181 | Iterator it = c.iterator(); |
|---|
| 182 | while (it.hasNext()) { |
|---|
| 183 | Id id = (Id) it.next(); |
|---|
| 184 | Logger.log(" *** Results of node "+id,Logger.PRINT_LOG); |
|---|
| 185 | Hashtable node_info = (Hashtable) results.get(id); |
|---|
| 186 | int link_stress = ((Integer) node_info.get("Stress in")).intValue(); |
|---|
| 187 | Logger.log(" * Link stress :"+link_stress,Logger.PRINT_LOG); |
|---|
| 188 | |
|---|
| 189 | int node_stress = link_stress + ((Integer) node_info.get("Stress out")).intValue(); |
|---|
| 190 | Logger.log(" * Node stress :"+node_stress,Logger.PRINT_LOG); |
|---|
| 191 | |
|---|
| 192 | Vector max_in = (Vector) node_info.get("Num max in"); |
|---|
| 193 | Logger.log(" * Max number in messages :"+max_in.get(1)+" at step "+max_in.get(0),Logger.PRINT_LOG); |
|---|
| 194 | |
|---|
| 195 | Vector max_out = (Vector) node_info.get("Num max out"); |
|---|
| 196 | Logger.log(" * Max number out messages :"+max_out.get(1)+" at step "+max_out.get(0),Logger.PRINT_LOG); |
|---|
| 197 | |
|---|
| 198 | total_in_msg+=link_stress; |
|---|
| 199 | total_out_msg+=node_stress-link_stress; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | long total_n_msg = total_in_msg + total_out_msg; |
|---|
| 203 | int steps = Logger.getStep(); |
|---|
| 204 | Logger.log("***********************",Logger.PRINT_LOG); |
|---|
| 205 | Logger.log(" ** Globals Results **",Logger.PRINT_LOG); |
|---|
| 206 | Logger.log("***********************",Logger.PRINT_LOG); |
|---|
| 207 | Logger.log(" * Ring size "+ringSize,Logger.PRINT_LOG); |
|---|
| 208 | Logger.log(" * Total number of message "+total_n_msg,Logger.PRINT_LOG); |
|---|
| 209 | if (ringSize>0) { |
|---|
| 210 | Logger.log(" * Average messages/in queues "+total_in_msg/ringSize,Logger.PRINT_LOG); |
|---|
| 211 | Logger.log(" * Average messages/out queues "+total_out_msg/ringSize,Logger.PRINT_LOG); |
|---|
| 212 | Logger.log(" * Average messages/nodes "+total_n_msg/ringSize,Logger.PRINT_LOG); |
|---|
| 213 | } |
|---|
| 214 | if (steps>0) Logger.log(" * Average messages/steps "+total_n_msg/steps,Logger.PRINT_LOG); |
|---|
| 215 | Logger.log(" * Stabilize Global Rate "+stabRate,Logger.PRINT_LOG); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | private static int lookup_number = 0; |
|---|
| 219 | /** |
|---|
| 220 | * print total and average hops |
|---|
| 221 | */ |
|---|
| 222 | public static void printhops() { |
|---|
| 223 | Collection keys = hops.keySet(); |
|---|
| 224 | Iterator it = keys.iterator(); |
|---|
| 225 | long total_hops = 0; |
|---|
| 226 | int lookup_number_temp = 0; |
|---|
| 227 | while (it.hasNext()) { |
|---|
| 228 | lookup_number_temp++; |
|---|
| 229 | String id_and_source =(String)it.next(); |
|---|
| 230 | total_hops += ((Integer)(hops.get(id_and_source))).longValue(); |
|---|
| 231 | // System.out.println(id_and_source + " " + |
|---|
| 232 | // hops.get(id_and_source)); |
|---|
| 233 | |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | lookup_number = lookup_number_temp; |
|---|
| 237 | Logger.log("************************", Logger.PRINT_LOG); |
|---|
| 238 | Logger.log("*****Hops Results*******", Logger.PRINT_LOG); |
|---|
| 239 | Logger.log("************************", Logger.PRINT_LOG); |
|---|
| 240 | if (lookup_number_temp == 0) { |
|---|
| 241 | Logger.log("There are no lookup operation", Logger.PRINT_LOG); |
|---|
| 242 | } else { |
|---|
| 243 | Logger.log("The total hops in the test is : [" + total_hops |
|---|
| 244 | + "] in [" + lookup_number_temp + "] lookup operations", |
|---|
| 245 | Logger.PRINT_LOG); |
|---|
| 246 | Logger.log("The average hops in the test is : " |
|---|
| 247 | + (double) ((double) total_hops / (double) lookup_number_temp), |
|---|
| 248 | Logger.PRINT_LOG); |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | public static int getLookup_number() { |
|---|
| 255 | return lookup_number; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | } |
|---|