Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
djrandom-py
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ale
djrandom-py
Commits
f8f9ba2f
Commit
f8f9ba2f
authored
13 years ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
beautify
parent
eed4cfe3
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
server/djrandom/frontend/latency.py
+33
-18
33 additions, 18 deletions
server/djrandom/frontend/latency.py
with
33 additions
and
18 deletions
server/djrandom/frontend/latency.py
+
33
−
18
View file @
f8f9ba2f
...
...
@@ -3,8 +3,8 @@ import time
import
pygooglechart
LATENCY
_BUCKETS
=
[
1
,
2
,
5
,
DEFAULT
_BUCKETS
=
[
2
,
5
,
10
,
20
,
50
,
100
,
200
,
500
,
]
...
...
@@ -12,9 +12,6 @@ LATENCY_BUCKETS = [
N_BUCKETS
=
len
(
LATENCY_BUCKETS
)
+
1
def
_build_buckets
():
return
[
0
]
*
N_BUCKETS
def
_percent
(
counts
):
tot
=
float
(
sum
(
counts
))
return
[(
x
,
100
*
x
/
tot
)
for
x
in
counts
]
...
...
@@ -22,10 +19,15 @@ def _percent(counts):
class
LatencyProfilerMiddleware
(
object
):
def
__init__
(
self
,
app
,
urls
=
[]
):
def
__init__
(
self
,
app
,
urls
=
None
,
buckets
=
None
):
self
.
_app
=
app
self
.
_urls
=
sorted
(
urls
,
key
=
lambda
x
:
len
(
x
),
reverse
=
True
)
self
.
_buckets
=
collections
.
defaultdict
(
_build_buckets
)
self
.
_buckets
=
buckets
or
DEFAULT_BUCKETS
self
.
_n_buckets
=
len
(
self
.
_buckets
)
+
1
self
.
_urls
=
sorted
(
urls
or
[],
key
=
lambda
x
:
len
(
x
),
reverse
=
True
)
def
_build_buckets
():
return
[
0
]
*
self
.
_n_buckets
self
.
_data
=
collections
.
defaultdict
(
_build_buckets
)
def
__call__
(
self
,
environ
,
start_response
):
start
=
time
.
time
()
...
...
@@ -41,12 +43,12 @@ class LatencyProfilerMiddleware(object):
if
url
.
startswith
(
url_prefix
):
url
=
url_prefix
break
for
bkt
,
threshold
in
enumerate
(
LATENCY_BUCKETS
):
for
bkt
,
threshold
in
enumerate
(
self
.
_buckets
):
if
elapsed_ms
<
threshold
:
break
else
:
bkt
=
len
(
LATENCY_BUCKETS
)
self
.
_
buckets
[
url
][
bkt
]
+=
1
bkt
=
self
.
_n_buckets
-
1
self
.
_
data
[
url
][
bkt
]
+=
1
def
handler
(
self
,
environ
,
start_response
):
headers
=
[(
'
Content-type
'
,
'
text/html
'
)]
...
...
@@ -57,23 +59,36 @@ class LatencyProfilerMiddleware(object):
'
<html><head><title>Latency Breakdown</title>
\n
'
'
<style type=
"
text/css
"
>
\n
'
'
body { background: white; font-family: sans-serif; }
\n
'
'
tbody td { text-align: left; padding-right: 10px; }
\n
'
'
thead th { font-weight: bold; text-align: left; color: white;
'
'
background-color: #666; }
\n
'
'
td.url { font-weight: bold; border-right: 1px solid #666; }
\n
'
'
tbody tr { border-bottom: 1px solid #ccc; }
\n
'
'
</style></head><body>
\n
'
'
<h3>Latency report by URL</h3>
\n
'
'
<table><thead><tr>
'
'
<table
cellspacing=
"
0
"
cellpadding=
"
0
"
border=
"
0
"
><thead><tr>
'
'
<th rowspan=
"
2
"
>URL</th>
'
,
'
<th colspan=
"
%d
"
>Latencies (ms)</th>
'
%
N_BUCKETS
,
'
<th rowspan=
"
2
"
>Total</th>
'
,
'
<th colspan=
"
%d
"
>Latencies (ms)</th>
'
%
self
.
_n_buckets
,
'
<th rowspan=
"
2
"
></th>
'
,
'
</tr><tr>
'
]
for
ms
in
LATENCY_BUCKETS
:
for
ms
in
self
.
_buckets
:
result
.
append
(
'
<th><%d</th>
'
%
ms
)
result
.
append
(
'
<th>>%d</th>
'
%
ms
)
result
.
append
(
'
</tr></thead><tbody>
'
)
for
url
in
sorted
(
self
.
_buckets
.
keys
()):
result
.
append
(
'
<tr><td class=
"
url
"
>%s</td>
'
%
url
)
for
count
,
pct
in
_percent
(
self
.
_buckets
[
url
]):
result
.
append
(
'
<td>%d (%.3g%%)</td>
'
%
(
count
,
pct
))
for
url
in
sorted
(
self
.
_data
.
keys
()):
values
=
self
.
_data
[
url
]
result
.
append
(
'
<tr><td class=
"
url
"
>%s</td><td>%d</td>
'
%
(
url
,
sum
(
values
)))
chart
=
pygooglechart
.
SparkLineChart
(
80
,
25
)
chart
.
set_data
(
values
)
for
count
,
pct
in
_percent
(
values
):
result
.
append
(
'
<td>%d (%.2g%%)</td>
'
%
(
count
,
pct
))
result
.
append
(
'
<td><img src=
"
%s
"
width=
"
80
"
height=
"
25
"
></td>
'
%
chart
.
get_url
())
result
.
append
(
'
</tr>
'
)
result
.
append
(
'
</tbody></table>
'
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment