@@ -27,4 +27,37 @@ def startPerfTime(self, title: str = "perfTime") -> PerformanceTime:
2727 return PerformanceTime (title )
2828 def stopPerfTime (self , perfTime : PerformanceTime ):
2929 perfTime .stop ()
30- self .performanceTimes .append (perfTime )
30+ self .performanceTimes .append (perfTime )
31+
32+ def export_stats (self , filename : str = None ) -> str :
33+ """
34+ Exports all current stats as a formatted string.
35+ If filename is given, it will also save the stats to that file.
36+ """
37+ lines = []
38+ lines .append ("PyWSGIRef Statistic-Export" )
39+ lines .append ("=" * 30 )
40+ lines .append (f"access counter: { self .count .count } " )
41+ lines .append ("" )
42+ lines .append ("Performance times:" )
43+ if not self .performanceTimes :
44+ lines .append (" (no entrys)" )
45+ else :
46+ for i , perf in enumerate (self .performanceTimes , 1 ):
47+ start = getattr (perf , "start" , None )
48+ end = getattr (perf , "end" , None )
49+ title = getattr (perf , "title" , "unnamed" )
50+ if start and end :
51+ duration = (end - start ).total_seconds ()
52+ lines .append (f" { i } . { title } : { start } - { end } (duration: { duration :.3f} s)" )
53+ elif start :
54+ lines .append (f" { i } . { title } : started at { start } (not stopped until now)" )
55+ else :
56+ lines .append (f" { i } . { title } : (no time data)" )
57+ result = "\n " .join (lines )
58+ if filename :
59+ if not filename .endswith (".pywsgirefstats" ):
60+ filename += ".pywsgirefstats"
61+ with open (filename , "w" , encoding = "utf-8" ) as f :
62+ f .write (result )
63+ return result
0 commit comments