summary refs log tree commit diff
diff options
context:
space:
mode:
-rwxr-xr-xsynapse/app/homeserver.py43
1 files changed, 31 insertions, 12 deletions
diff --git a/synapse/app/homeserver.py b/synapse/app/homeserver.py
index d93afdc1c2..f6e222f1da 100755
--- a/synapse/app/homeserver.py
+++ b/synapse/app/homeserver.py
@@ -520,27 +520,46 @@ class SynapseSite(Site):
 
 
 def run(hs):
-    PROFILE_SYNAPSE = False
+    PROFILE_SYNAPSE = True
     if PROFILE_SYNAPSE:
         def profile(func):
-            from cProfile import Profile
+            from pyinstrument import Profiler
             from threading import current_thread
+            import time
+            import resource
 
             def profiled(*args, **kargs):
-                profile = Profile()
-                profile.enable()
+                profile = Profiler()
+
+                rusage = resource.getrusage(resource.RUSAGE_SELF)
+                start_utime = rusage.ru_utime * 1000
+                start = int(time.time()*1000)
+
+                profile.start()
                 func(*args, **kargs)
-                profile.disable()
-                ident = current_thread().ident
-                profile.dump_stats("/tmp/%s.%s.%i.pstat" % (
-                    hs.hostname, func.__name__, ident
-                ))
+                profile.stop()
+
+                end = int(time.time()*1000)
+                rusage = resource.getrusage(resource.RUSAGE_SELF)
+                end_utime = rusage.ru_utime * 1000
+
+                if end_utime - start_utime > 50:
+                    ident = current_thread().ident
+                    name = "/tmp/%s.%s.%i.%d-%d.%d-%d" % (
+                        hs.hostname, func.__name__, ident, start, end,
+                        end-start, end_utime - start_utime,
+                    )
+                    # profile.dump_stats(name + ".html")
+                    html = profile.output_html()
+                    with open(name + ".html", "w") as f:
+                        f.write(html)
 
             return profiled
 
-        from twisted.python.threadpool import ThreadPool
-        ThreadPool._worker = profile(ThreadPool._worker)
-        reactor.run = profile(reactor.run)
+        # from twisted.python.threadpool import ThreadPool
+        # ThreadPool._worker = profile(ThreadPool._worker)
+        reactor.runUntilCurrent = profile(reactor.runUntilCurrent)
+        reactor.doIteration = profile(reactor.doIteration)
 
     def in_thread():
         with LoggingContext("run"):