diff bin/collector @ 1596:7d5d6e7bc526

collector web view speedups- don't json encode all devs all the time Ignore-this: ed01dc070b5e21303b0a62ff5d814b30
author Drew Perttula <drewp@bigasterisk.com>
date Sat, 03 Jun 2017 19:58:42 +0000
parents 013cbd7a0f08
children 64c1bcff604e
line wrap: on
line diff
--- a/bin/collector	Sat Jun 03 09:20:04 2017 +0000
+++ b/bin/collector	Sat Jun 03 19:58:42 2017 +0000
@@ -60,9 +60,11 @@
 class WebListeners(object):
     def __init__(self):
         self.clients = []
-
+        self.pendingMessageForDev = {} # dev: (attrs, outputmap)
+        self.lastFlush = 0
+        
     def addClient(self, client):
-        self.clients.append([client, {}])
+        self.clients.append([client, {}]) # seen = {dev: attrs}
         log.info('added client %s', client)
 
     def delClient(self, client):
@@ -70,21 +72,33 @@
         log.info('delClient %s, %s left', client, len(self.clients))
         
     def outputAttrsSet(self, dev, attrs, outputMap):
-        now = time.time()
+        """called often- don't be slow"""
+
+        self.pendingMessageForDev[dev] = (attrs, outputMap)
+        self._flush()
 
-        msg = self.makeMsg(dev, attrs, outputMap)
+    def _flush(self):
+        now = time.time()
+        if now < self.lastFlush + .05 or not self.clients:
+            return
+        self.lastFlush = now
+
+        while self.pendingMessageForDev:
+            dev, (attrs, outputMap) = self.pendingMessageForDev.popitem()
 
-        # this omits repeats, but can still send many
-        # messages/sec. Not sure if piling up messages for the browser
-        # could lead to slowdowns in the real dmx output.
-        for client, seen in self.clients:
-            for m, t in seen.items():
-                if t < now - 5:
-                    del seen[m]
-            if msg in seen:
-                continue
-            seen[msg] = now
-            client.sendMessage(msg)
+            msg = None # lazy, since makeMsg is slow
+            
+            # this omits repeats, but can still send many
+            # messages/sec. Not sure if piling up messages for the browser
+            # could lead to slowdowns in the real dmx output.
+            for client, seen in self.clients:
+                if seen.get(dev) == attrs:
+                    continue
+                if msg is None:
+                    msg = self.makeMsg(dev, attrs, outputMap)
+
+                seen[dev] = attrs
+                client.sendMessage(msg)
 
     def makeMsg(self, dev, attrs, outputMap):
         attrRows = []
@@ -172,6 +186,8 @@
     (options, args) = parser.parse_args()
     log.setLevel(logging.DEBUG if options.verbose else logging.INFO)
 
+    logging.getLogger('colormath').setLevel(logging.INFO)
+    
     graph = SyncedGraph(networking.rdfdb.url, "collector")
 
     graph.initiallySynced.addCallback(lambda _: launch(graph, options.loadtest)).addErrback(log.error)