htparser: Put root handler in a new session regardless of daemonization.
[ashd.git] / python / ashd / perf.py
CommitLineData
d5ee5cde
FT
1try:
2 import pdm.perf
3except:
4 pdm = None
5
6reqstat = {}
7
8if pdm:
9 statistics = pdm.perf.staticdir()
10 statistics["req"] = pdm.perf.valueattr(reqstat)
11 requests = pdm.perf.eventobj()
12
13 class reqstart(pdm.perf.startevent):
14 def __init__(self, env):
15 super(reqstart, self).__init__()
16 self.method = env.get("REQUEST_METHOD")
17 self.uri = env.get("REQUEST_URI")
18 self.host = env.get("HTTP_HOST")
44f2a2fe
FT
19 self.remoteaddr = env.get("REMOTE_ADDR")
20 self.remoteport = env.get("REMOTE_PORT")
d5ee5cde
FT
21
22 class reqfinish(pdm.perf.finishevent):
23 def __init__(self, start, aborted, status):
24 super(reqfinish, self).__init__(start, aborted)
25 self.status = status
26
27class request(object):
28 def __init__(self, env):
29 self.resp = None
30 if pdm:
31 self.startev = reqstart(env)
32 requests.notify(self.startev)
33
34 def response(self, resp):
35 self.resp = resp
36
37 def finish(self, aborted):
38 key = None
39 status = None
40 try:
41 if len(self.resp) > 0:
42 status = self.resp[0]
43 p = status.find(" ")
44 if p < 0:
45 key = status
46 else:
47 key = status[:p]
48 except:
49 pass
50 reqstat[key] = reqstat.setdefault(key, 0) + 1
51 if pdm:
52 requests.notify(reqfinish(self.startev, aborted, status))
53
54 def __enter__(self):
55 return self
56
57 def __exit__(self, *excinfo):
58 self.finish(bool(excinfo[0]))
59 return False