yoshi | 28bac13 | 2014-01-22 11:00:17 -0800 | [diff] [blame] | 1 | package com.tinkerpop.blueprints.impls.ramcloud; |
| 2 | |
| 3 | import java.util.HashMap; |
| 4 | import java.util.Iterator; |
| 5 | import java.util.Map; |
| 6 | import org.slf4j.Logger; |
| 7 | import org.slf4j.LoggerFactory; |
| 8 | import sun.reflect.Reflection; |
| 9 | |
| 10 | |
| 11 | public final class PerfMon { |
| 12 | private static final PerfMon instance = new PerfMon(); |
| 13 | public final long measureAllTimeProp = Long.valueOf(System.getProperty("benchmark.measureAll", "0")); |
| 14 | private final static Logger log = LoggerFactory.getLogger(PerfMon.class); |
| 15 | |
| 16 | private static final int debug = 0; |
| 17 | |
| 18 | private static long read_latency_sum; |
| 19 | private static long read_latency_cnt; |
| 20 | private static int read_flag; |
| 21 | |
| 22 | private static long write_latency_sum; |
| 23 | private static long write_latency_cnt; |
| 24 | private static int write_flag; |
| 25 | |
| 26 | private static long serialize_latency_sum; |
| 27 | private static long serialize_latency_cnt; |
| 28 | private static HashMap<String, Long> ser_flag = new HashMap<String, Long>(); |
| 29 | //private static int ser_flag; |
| 30 | |
| 31 | private static long deserialize_latency_sum; |
| 32 | private static long deserialize_latency_cnt; |
| 33 | private static int deser_flag; |
| 34 | |
| 35 | private static long addsw_time; |
| 36 | private static long addport_time; |
| 37 | private static long addlink_time; |
| 38 | private static long addport_cnt; |
| 39 | private static HashMap<String, Long> ser_time = new HashMap<String, Long>(); |
| 40 | private static HashMap<String, Long> deser_time = new HashMap<String, Long>(); |
| 41 | private static HashMap<String, Long> write_time = new HashMap<String, Long>(); |
| 42 | private static HashMap<String, Long> read_time = new HashMap<String, Long>(); |
| 43 | |
| 44 | public static PerfMon getInstance() { |
| 45 | return instance; |
| 46 | } |
| 47 | private PerfMon(){ |
| 48 | } |
| 49 | |
| 50 | private void clear(){ |
| 51 | if(! Thread.currentThread().getName().equals("main")){ |
| 52 | return; |
| 53 | } |
| 54 | read_latency_sum=0L; |
| 55 | read_latency_cnt=0L; |
| 56 | write_latency_sum=0L; |
| 57 | write_latency_cnt=0L; |
| 58 | serialize_latency_sum=0L; |
| 59 | serialize_latency_cnt=0L; |
| 60 | deserialize_latency_sum=0L; |
| 61 | deserialize_latency_cnt=0L; |
| 62 | read_flag=write_flag=deser_flag=0; |
| 63 | for (Iterator<Map.Entry<String, Long>> it = ser_flag.entrySet().iterator(); it.hasNext(); ) { |
| 64 | Map.Entry<String, Long> entry = it.next(); |
| 65 | entry.setValue(0L); |
| 66 | } |
| 67 | //log.error("flag cleared"); |
| 68 | } |
| 69 | public void addswitch_start(){ |
| 70 | if(measureAllTimeProp==0) |
| 71 | return; |
| 72 | |
| 73 | if(! Thread.currentThread().getName().equals("main")){ |
| 74 | return; |
| 75 | } |
| 76 | clear(); |
| 77 | addsw_time = System.nanoTime(); |
| 78 | } |
| 79 | public void addswitch_end(){ |
| 80 | if(measureAllTimeProp==0) |
| 81 | return; |
| 82 | |
| 83 | long delta; |
| 84 | long sum; |
| 85 | |
| 86 | if(! Thread.currentThread().getName().equals("main")){ |
| 87 | return; |
| 88 | } |
| 89 | delta = System.nanoTime() - addsw_time; |
| 90 | sum = read_latency_sum + write_latency_sum + serialize_latency_sum + deserialize_latency_sum; |
| 91 | log.error("Performance add_switch {} read {} ({}) write {} ({}) serialize {} ({}) deserialize {} ({}) rwsd total {} other {} ({})", |
| 92 | delta, read_latency_sum, read_latency_cnt, write_latency_sum, write_latency_cnt, serialize_latency_sum, serialize_latency_cnt, deserialize_latency_sum, deserialize_latency_cnt, sum, delta-sum, ((float)(delta-sum))*100.0/((float) delta)); |
| 93 | } |
| 94 | public void addport_start(){ |
| 95 | if(measureAllTimeProp==0) |
| 96 | return; |
| 97 | if(! Thread.currentThread().getName().equals("main")){ |
| 98 | return; |
| 99 | } |
| 100 | clear(); |
| 101 | addport_cnt = 0; |
| 102 | addport_time = System.nanoTime(); |
| 103 | } |
| 104 | public void addport_incr(){ |
| 105 | if(measureAllTimeProp==0) |
| 106 | return; |
| 107 | if(! Thread.currentThread().getName().equals("main")){ |
| 108 | return; |
| 109 | } |
| 110 | clear(); |
| 111 | addport_cnt ++; |
| 112 | } |
| 113 | public void addport_end(){ |
| 114 | if(measureAllTimeProp==0) |
| 115 | return; |
| 116 | long delta; |
| 117 | long sum; |
| 118 | if(! Thread.currentThread().getName().equals("main")){ |
| 119 | return; |
| 120 | } |
| 121 | delta = System.nanoTime() - addport_time; |
| 122 | sum = read_latency_sum + write_latency_sum + serialize_latency_sum + deserialize_latency_sum; |
| 123 | log.error("Performance add_port {} ( {} ports ) read {} ({}) write {} ({}) serialize {} ({}) deserialize {} ({}) rwsd total {} other {} ({})", |
| 124 | delta, addport_cnt, read_latency_sum, read_latency_cnt, write_latency_sum, write_latency_cnt, serialize_latency_sum, serialize_latency_cnt, deserialize_latency_sum, deserialize_latency_cnt, sum, delta-sum, ((float)(delta-sum))*100.0/((float) delta)); |
| 125 | } |
| 126 | public void addlink_start(){ |
| 127 | if(measureAllTimeProp==0) |
| 128 | return; |
| 129 | if(! Thread.currentThread().getName().equals("main")){ |
| 130 | return; |
| 131 | } |
| 132 | clear(); |
| 133 | addlink_time = System.nanoTime(); |
| 134 | } |
| 135 | public void addlink_end(){ |
| 136 | if(measureAllTimeProp==0) |
| 137 | return; |
| 138 | long delta; |
| 139 | long sum; |
| 140 | if(! Thread.currentThread().getName().equals("main")){ |
| 141 | return; |
| 142 | } |
| 143 | delta = System.nanoTime() - addlink_time; |
| 144 | sum = read_latency_sum + write_latency_sum + serialize_latency_sum + deserialize_latency_sum; |
| 145 | log.error("Performance add_link {} read {} ({}) write {} ({}) serialize {} ({}) deserialize {} ({}) rwsd total {} other {} ({})", |
| 146 | delta, read_latency_sum, read_latency_cnt, write_latency_sum, write_latency_cnt, serialize_latency_sum, serialize_latency_cnt, deserialize_latency_sum, deserialize_latency_cnt, sum, delta-sum, ((float)(delta-sum))*100.0/((float) delta)); |
| 147 | } |
| 148 | |
| 149 | public void read_start(String key){ |
| 150 | if(measureAllTimeProp==0) |
| 151 | return; |
| 152 | if(! Thread.currentThread().getName().equals("main")){ |
| 153 | return; |
| 154 | } |
| 155 | if ( debug==1 ) |
| 156 | log.error("read start {}", key); |
| 157 | read_time.put(key, System.nanoTime()); |
| 158 | //read_time = System.nanoTime(); |
| 159 | if ( read_flag != 0){ |
| 160 | log.error("read is already started"); |
| 161 | } |
| 162 | read_flag = 1; |
| 163 | } |
| 164 | public void read_end(String key){ |
| 165 | if(measureAllTimeProp==0) |
| 166 | return; |
| 167 | long delta; |
| 168 | if(! Thread.currentThread().getName().equals("main")){ |
| 169 | return; |
| 170 | } |
| 171 | //read_latency_sum += System.nanoTime() - read_time; |
| 172 | if ( debug==1 ) |
| 173 | log.error("read end {}", key); |
| 174 | delta = System.nanoTime() - read_time.get(key); |
| 175 | read_latency_sum += delta; |
| 176 | read_latency_cnt ++; |
| 177 | if ( read_flag != 1){ |
| 178 | log.error("read is not started"); |
| 179 | } |
| 180 | read_flag = 0; |
| 181 | } |
| 182 | public void write_start(String key){ |
| 183 | if(measureAllTimeProp==0) |
| 184 | return; |
| 185 | if(! Thread.currentThread().getName().equals("main")){ |
| 186 | return; |
| 187 | } |
| 188 | if ( debug==1 ) |
| 189 | log.error("write start {}", key); |
| 190 | write_time.put(key, System.nanoTime()); |
| 191 | //write_time = System.nanoTime(); |
| 192 | if ( write_flag != 0){ |
| 193 | log.error("write is already started"); |
| 194 | } |
| 195 | write_flag = 1; |
| 196 | } |
| 197 | public void write_end(String key){ |
| 198 | if(measureAllTimeProp==0) |
| 199 | return; |
| 200 | if(! Thread.currentThread().getName().equals("main")){ |
| 201 | return; |
| 202 | } |
| 203 | if ( debug==1 ) |
| 204 | log.error("write end {}", key); |
| 205 | write_latency_sum += (System.nanoTime() - write_time.get(key)); |
| 206 | //write_latency_sum += System.nanoTime() - write_time; |
| 207 | write_latency_cnt ++; |
| 208 | if ( write_flag != 1){ |
| 209 | log.error("write is not started"); |
| 210 | } |
| 211 | write_flag = 0; |
| 212 | } |
| 213 | public void ser_add(long time){ |
| 214 | if(measureAllTimeProp==0) |
| 215 | return; |
| 216 | if(! Thread.currentThread().getName().equals("main")){ |
| 217 | return; |
| 218 | } |
| 219 | serialize_latency_sum += time; |
| 220 | serialize_latency_cnt ++; |
| 221 | } |
| 222 | public void ser_start(String key){ |
| 223 | if(measureAllTimeProp==0) |
| 224 | return; |
| 225 | if(! Thread.currentThread().getName().equals("main")){ |
| 226 | return; |
| 227 | } |
| 228 | //ser_time = System.nanoTime(); |
| 229 | if ( debug==1 ) |
| 230 | log.error("ser start {}", key); |
| 231 | ser_time.put(key, System.nanoTime()); |
| 232 | // log.error("ser {} start at {} flag {}", key, ser_time, ser_flag); |
| 233 | if ( ser_flag.containsKey(key) ){ |
| 234 | if ( ser_flag.get(key) != 0L){ |
| 235 | log.error("ser {} sarted but has been already started", key); |
| 236 | } |
| 237 | } |
| 238 | ser_flag.put(key, 1L); |
| 239 | } |
| 240 | public void ser_end(String key){ |
| 241 | if(measureAllTimeProp==0) |
| 242 | return; |
| 243 | if(! Thread.currentThread().getName().equals("main")){ |
| 244 | return; |
| 245 | } |
| 246 | if ( debug==1 ) |
| 247 | log.error("ser end {}", key); |
| 248 | //serialize_latency_sum += System.nanoTime() - ser_time; |
| 249 | serialize_latency_sum += (System.nanoTime() - ser_time.get(key)); |
| 250 | serialize_latency_cnt ++; |
| 251 | // log.error("ser {} end at {} flag {}", key, ser_time, ser_flag); |
| 252 | if ( ser_flag.containsKey(key) ){ |
| 253 | if ( ser_flag.get(key) != 1L){ |
| 254 | log.error("ser {} ended but hasn't started", key); |
| 255 | } |
| 256 | } |
| 257 | ser_flag.put(key, 0L); |
| 258 | } |
| 259 | public void deser_start(String key){ |
| 260 | if(measureAllTimeProp==0) |
| 261 | return; |
| 262 | if(! Thread.currentThread().getName().equals("main")){ |
| 263 | return; |
| 264 | } |
| 265 | if ( debug==1 ) |
| 266 | log.error("deser start {}", key); |
| 267 | deser_time.put(key, System.nanoTime()); |
| 268 | //deser_time = System.nanoTime(); |
| 269 | if ( deser_flag != 0){ |
| 270 | log.error("deser is already started"); |
| 271 | } |
| 272 | deser_flag = 1; |
| 273 | } |
| 274 | public void deser_end(String key){ |
| 275 | if(measureAllTimeProp==0) |
| 276 | return; |
| 277 | if(! Thread.currentThread().getName().equals("main")){ |
| 278 | return; |
| 279 | } |
| 280 | if ( debug==1 ) |
| 281 | log.error("deser end {}", key); |
| 282 | //deserialize_latency_sum += System.nanoTime() - deser_time; |
| 283 | deserialize_latency_sum += System.nanoTime() - deser_time.get(key); |
| 284 | deserialize_latency_cnt ++; |
| 285 | if ( deser_flag != 1){ |
| 286 | log.error("deser is not started"); |
| 287 | } |
| 288 | deser_flag = 0; |
| 289 | } |
| 290 | } |