| 1 |
""" |
| 2 |
Objective-C runtime wrapper for use by LLDB Python formatters |
| 3 |
|
| 4 |
Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 |
See https://llvm.org/LICENSE.txt for license information. |
| 6 |
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 |
""" |
| 8 |
import lldb |
| 9 |
import time |
| 10 |
import datetime |
| 11 |
import inspect |
| 12 |
|
| 13 |
|
| 14 |
class TimeMetrics: |
| 15 |
|
| 16 |
@staticmethod |
| 17 |
def generate(label=None): |
| 18 |
return TimeMetrics(label) |
| 19 |
|
| 20 |
def __init__(self, lbl=None): |
| 21 |
self.label = "" if lbl is None else lbl |
| 22 |
pass |
| 23 |
|
| 24 |
def __enter__(self): |
| 25 |
caller = inspect.stack()[1] |
| 26 |
self.function = str(caller) |
| 27 |
self.enter_time = time.clock() |
| 28 |
|
| 29 |
def __exit__(self, a, b, c): |
| 30 |
self.exit_time = time.clock() |
| 31 |
print("It took " + str(self.exit_time - self.enter_time) + |
| 32 |
" time units to run through " + self.function + self.label) |
| 33 |
return False |
| 34 |
|
| 35 |
|
| 36 |
class Counter: |
| 37 |
|
| 38 |
def __init__(self): |
| 39 |
self.count = 0 |
| 40 |
self.list = [] |
| 41 |
|
| 42 |
def update(self, name): |
| 43 |
self.count = self.count + 1 |
| 44 |
# avoid getting the full dump of this ValueObject just to save its |
| 45 |
# metrics |
| 46 |
if isinstance(name, lldb.SBValue): |
| 47 |
self.list.append(name.GetName()) |
| 48 |
else: |
| 49 |
self.list.append(str(name)) |
| 50 |
|
| 51 |
def __str__(self): |
| 52 |
return str(self.count) + " times, for items [" + str(self.list) + "]" |
| 53 |
|
| 54 |
|
| 55 |
class MetricsPrinter_Verbose: |
| 56 |
|
| 57 |
def __init__(self, metrics): |
| 58 |
self.metrics = metrics |
| 59 |
|
| 60 |
def __str__(self): |
| 61 |
string = "" |
| 62 |
for key, value in self.metrics.metrics.items(): |
| 63 |
string = string + "metric " + str(key) + ": " + str(value) + "\n" |
| 64 |
return string |
| 65 |
|
| 66 |
|
| 67 |
class MetricsPrinter_Compact: |
| 68 |
|
| 69 |
def __init__(self, metrics): |
| 70 |
self.metrics = metrics |
| 71 |
|
| 72 |
def __str__(self): |
| 73 |
string = "" |
| 74 |
for key, value in self.metrics.metrics.items(): |
| 75 |
string = string + "metric " + \ |
| 76 |
str(key) + " was hit " + str(value.count) + " times\n" |
| 77 |
return string |
| 78 |
|
| 79 |
|
| 80 |
class Metrics: |
| 81 |
|
| 82 |
def __init__(self): |
| 83 |
self.metrics = {} |
| 84 |
|
| 85 |
def add_metric(self, name): |
| 86 |
self.metrics[name] = Counter() |
| 87 |
|
| 88 |
def metric_hit(self, metric, trigger): |
| 89 |
self.metrics[metric].update(trigger) |
| 90 |
|
| 91 |
def __getitem__(self, key): |
| 92 |
return self.metrics[key] |
| 93 |
|
| 94 |
def __getattr__(self, name): |
| 95 |
if name == 'compact': |
| 96 |
return MetricsPrinter_Compact(self) |
| 97 |
if name == 'verbose': |
| 98 |
return MetricsPrinter_Verbose(self) |
| 99 |
raise AttributeError("%r object has no attribute %r" % |
| 100 |
(type(self).__name__, name)) |
| 101 |
|
| 102 |
def __str__(self): |
| 103 |
return str(self.verbose) |
| 104 |
|
| 105 |
def metric_success(self, metric): |
| 106 |
total_count = 0 |
| 107 |
metric_count = self[metric].count |
| 108 |
for key, value in self.metrics.items(): |
| 109 |
total_count = total_count + value.count |
| 110 |
if total_count > 0: |
| 111 |
return metric_count / float(total_count) |
| 112 |
return 0 |