Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 1 | import lldb |
| 2 | |
| 3 | class Counter: |
| 4 | def __init__(self): |
| 5 | self.count = 0 |
| 6 | self.list = [] |
| 7 | def update(self,name): |
| 8 | self.count = self.count + 1 |
Enrico Granata | 332b0b9 | 2012-03-09 19:04:53 +0000 | [diff] [blame^] | 9 | # avoid getting the full dump of this ValueObject just to save its metrics |
| 10 | if isinstance(name,lldb.SBValue): |
| 11 | self.list.append(name.GetName()) |
| 12 | else: |
| 13 | self.list.append(str(name)) |
Enrico Granata | eb4a479 | 2012-02-23 23:10:27 +0000 | [diff] [blame] | 14 | def __str__(self): |
| 15 | return str(self.count) + " times, for items [" + str(self.list) + "]" |
| 16 | |
| 17 | class MetricsPrinter_Verbose: |
| 18 | def __init__(self,metrics): |
| 19 | self.metrics = metrics |
| 20 | def __str__(self): |
| 21 | string = "" |
| 22 | for key,value in self.metrics.metrics.items(): |
| 23 | string = string + "metric " + str(key) + ": " + str(value) + "\n" |
| 24 | return string |
| 25 | |
| 26 | class MetricsPrinter_Compact: |
| 27 | def __init__(self,metrics): |
| 28 | self.metrics = metrics |
| 29 | def __str__(self): |
| 30 | string = "" |
| 31 | for key,value in self.metrics.metrics.items(): |
| 32 | string = string + "metric " + str(key) + " was hit " + str(value.count) + " times\n" |
| 33 | return string |
| 34 | |
| 35 | class Metrics: |
| 36 | def __init__(self): |
| 37 | self.metrics = {} |
| 38 | |
| 39 | def add_metric(self,name): |
| 40 | self.metrics[name] = Counter() |
| 41 | |
| 42 | def metric_hit(self,metric,trigger): |
| 43 | self.metrics[metric].update(trigger) |
| 44 | |
| 45 | def __getitem__(self,key): |
| 46 | return self.metrics[key] |
| 47 | |
| 48 | def __getattr__(self,name): |
| 49 | if name == 'compact': |
| 50 | return MetricsPrinter_Compact(self) |
| 51 | if name == 'verbose': |
| 52 | return MetricsPrinter_Verbose(self) |
| 53 | raise AttributeError("%r object has no attribute %r" % |
| 54 | (type(self).__name__, name)) |
| 55 | |
| 56 | def __str__(self): |
| 57 | return str(self.verbose) |
| 58 | |
| 59 | def metric_success(self,metric): |
| 60 | total_count = 0 |
| 61 | metric_count = self[metric].count |
| 62 | for key,value in self.metrics.items(): |
| 63 | total_count = total_count + value.count |
| 64 | if total_count > 0: |
| 65 | return metric_count / float(total_count) |
| 66 | return 0 |