Changeset - 9d6c7cab31b0
[Not reviewed]
default
0 1 1
drewp@bigasterisk.com - 20 months ago 2023-05-24 06:44:13
drewp@bigasterisk.com
refactor, though i think i want to remove this since it's redundant with metrics
2 files changed with 30 insertions and 29 deletions:
0 comments (0 inline, 0 general)
light9/metrics.py
Show inline comments
 
@@ -96,41 +96,12 @@ class PmfStat(Stat):
 
  percentiles for arbitrary floating-point data. This is potentially a
 
  bit expensive, so its child values are only updated once every
 
  twenty seconds.
 

	
 

	
 

	
 
metrics consumer side can do this with the changing counts:
 

	
 
class RecentFps:
 
  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}
 

	
 

	
 
i think prometheus covers this one:
 

	
 
import psutil
 
def gatherProcessStats():
light9/recentfps.py
Show inline comments
 
new file 100644
 
# server side version of what the metrics consumer does with changing counts
 
import time
 

	
 
class RecentFps:
 
  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}
0 comments (0 inline, 0 general)