Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cyclone support, @STATS.foo.time() decorator support #43

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 56 additions & 6 deletions src/greplin/scales/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Classes for tracking system statistics."""

import collections
import functools
import inspect
import itertools
import gc
Expand Down Expand Up @@ -465,7 +466,7 @@ class PmfStatDict(UserDict):
"""Ugly hack defaultdict-like thing."""

class TimeManager(object):
"""Context manager for timing."""
"""Context manager for timing. Also works as a function decorator."""

def __init__(self, container):
self.container = container
Expand Down Expand Up @@ -501,14 +502,22 @@ def discard(self):
"""Discard this sample."""
self.__discard = True

def __call__(self, func):
"""Decorator mode"""
def newFunc(*args, **kw):
with self:
return func(*args, **kw)
functools.update_wrapper(newFunc, func)
return newFunc

def __init__(self, sample = None):
def __init__(self, sample = None, recalcPeriod = 20):
UserDict.__init__(self)
if sample:
self.__sample = sample
else:
self.__sample = ExponentiallyDecayingReservoir()
self.__timestamp = 0
self.__recalcPeriod = recalcPeriod
self.percentile99 = None
self['count'] = 0

Expand All @@ -524,7 +533,7 @@ def addValue(self, value):
"""Updates the dictionary."""
self['count'] += 1
self.__sample.update(value)
if time.time() > self.__timestamp + 20 and len(self.__sample) > 1:
if time.time() > self.__timestamp + self.__recalcPeriod and len(self.__sample) > 1:
self.__timestamp = time.time()
self['min'] = self.__sample.min
self['max'] = self.__sample.max
Expand All @@ -542,7 +551,15 @@ def addValue(self, value):


def time(self):
"""Measure the time this section of code takes. For use in with statements."""
"""Measure the time this section of code takes. For use in with-
statements or as a function decorator.

Decorator example:

@STATS.foo.time()
def foo():
...
"""
return self.TimeManager(self)


Expand All @@ -553,12 +570,13 @@ class PmfStat(Stat):
bit expensive, so its child values are only updated once every
twenty seconds."""

def __init__(self, name, _=None):
def __init__(self, name, _=None, recalcPeriod=20):
Stat.__init__(self, name, None)
self.__recalcPeriod = recalcPeriod


def _getDefault(self, _):
return PmfStatDict()
return PmfStatDict(recalcPeriod=self.__recalcPeriod)


def __set__(self, instance, value):
Expand Down Expand Up @@ -591,7 +609,39 @@ class NamedPmfDictStat(Stat):
def _getDefault(self, _):
return NamedPmfDict()

class RecentFps(object):
def __init__(self, window=20):
self.window = window
self.recentTimes = []

def mark(self):
now = time.time()
self.recentTimes.append(now)
self.recentTimes = self.recentTimes[-self.window:]

def rate(self):
def dec(innerFunc):
def f(*a, **kw):
self.mark()
return innerFunc(*a, **kw)
return f
return dec

def __call__(self):
if len(self.recentTimes) < 2:
return {}
recents = sorted(round(1 / (b - a), 3)
for a, b in zip(self.recentTimes[:-1],
self.recentTimes[1:]))
avg = (len(self.recentTimes) - 1) / (
self.recentTimes[-1] - self.recentTimes[0])
return {'average': round(avg, 5),
'recents': recents,
'period': round(1 / avg, 1) if avg > 0 else None}

class RecentFpsStat(Stat):
def _getDefault(self, _):
return RecentFps()

class StateTimeStatDict(UserDict):
"""Special dict that tracks time spent in current state."""
Expand Down
3 changes: 1 addition & 2 deletions src/greplin/scales/bottlehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ def bottlestats(server_name, path=''):

def register_stats_handler(app, server_name, prefix='/status/'):
"""Register the stats handler with a Flask app, serving routes
with a given prefix. The prefix defaults to '/_stats/', which is
generally what you want."""
with a given prefix."""
if not prefix.endswith('/'):
prefix += '/'
handler = functools.partial(bottlestats, server_name)
Expand Down
32 changes: 32 additions & 0 deletions src/greplin/scales/cyclonehandler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2011 The scales Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Defines a Cyclone request handler for status reporting.

Install like:
handlers=[
...
(r'/stats/(.*)', StatsHandler, {'serverName': 'my_app_name'}),
]

"""


from greplin.scales import tornadolike

import cyclone.web


class StatsHandler(tornadolike.Handler, cyclone.web.RequestHandler):
"""Cyclone request handler for a status page."""
40 changes: 2 additions & 38 deletions src/greplin/scales/tornadohandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,10 @@
"""Defines a Tornado request handler for status reporting."""


from greplin import scales
from greplin.scales import formats, util
from greplin.scales import tornadolike

import tornado.web



class StatsHandler(tornado.web.RequestHandler):
class StatsHandler(tornadolike.Handler, tornado.web.RequestHandler):
"""Tornado request handler for a status page."""

serverName = None


def initialize(self, serverName): # pylint: disable=W0221
"""Initializes the handler."""
self.serverName = serverName


def get(self, path): # pylint: disable=W0221
"""Renders a GET request, by showing this nodes stats and children."""
path = path or ''
path = path.lstrip('/')
parts = path.split('/')
if not parts[0]:
parts = parts[1:]
statDict = util.lookup(scales.getStats(), parts)

if statDict is None:
self.set_status(404)
self.finish('Path not found.')
return

outputFormat = self.get_argument('format', default='html')
query = self.get_argument('query', default=None)
if outputFormat == 'json':
formats.jsonFormat(self, statDict, query)
elif outputFormat == 'prettyjson':
formats.jsonFormat(self, statDict, query, pretty=True)
else:
formats.htmlHeader(self, '/' + path, self.serverName, query)
formats.htmlFormat(self, tuple(parts), statDict, query)

return None
39 changes: 39 additions & 0 deletions src/greplin/scales/tornadolike.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from greplin import scales
from greplin.scales import formats, util


class Handler(object):

serverName = None


def initialize(self, serverName): # pylint: disable=W0221
"""Initializes the handler."""
self.serverName = serverName


def get(self, path): # pylint: disable=W0221
"""Renders a GET request, by showing this nodes stats and children."""
path = path or ''
path = path.lstrip('/')
parts = path.split('/')
if not parts[0]:
parts = parts[1:]
statDict = util.lookup(scales.getStats(), parts)

if statDict is None:
self.set_status(404)
self.finish('Path not found.')
return

outputFormat = self.get_argument('format', default='html')
query = self.get_argument('query', default=None)
if outputFormat == 'json':
formats.jsonFormat(self, statDict, query)
elif outputFormat == 'prettyjson':
formats.jsonFormat(self, statDict, query, pretty=True)
else:
formats.htmlHeader(self, '/' + path, self.serverName, query)
formats.htmlFormat(self, tuple(parts), statDict, query)

return None