def __init__(self, description): from ..queries.windows import Stats if ON and not hasattr(self, "description"): self.description = description self.samples = [] self.stats = Stats()() profiles[description] = self
class Profiler(object): """ """ def __new__(cls, *args): if ON: output = profiles.get(args[0], None) if output: return output output = object.__new__(cls, *args) return output def __init__(self, description): from ..queries.windows import Stats if ON and not hasattr(self, "description"): self.description = description self.samples = [] self.stats = Stats()() profiles[description] = self def __enter__(self): if ON: self.start = clock() return self def __exit__(self, type, value, traceback): if ON: self.end = clock() duration = self.end - self.start from util.queries.windows import Stats self.stats.add(duration) if self.samples is not None: self.samples.append(duration) if len(self.samples) > 100: self.samples = None