/
profiler.opa
120 lines (99 loc) · 3.22 KB
/
profiler.opa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
Copyright © 2011 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @author Adam Koprowski, April 2010
*
*
* {1 About this module}
*
* A simple module for pseudo-profiling; allows to call functions,
* measuring execution time and printing summary in the end.
*/
/**
* Perform profiling on the server
*/
Server_profiler = {{
@private
@server
data = Reference.create(StringMap.empty : stringmap((int, Duration.duration)))
init() =
Reference.set(data, StringMap.empty)
execute(f, label) =
t_beg = Date.now()
res = f()
t_end = Date.now()
old_data = Reference.get(data)
execution_time = Date.between(t_beg, t_end)
(c, t) = StringMap.get(label, old_data) ? (0, Duration.empty)
new_entry = (c + 1, Duration.add(t, execution_time))
new_data = StringMap.add(label, new_entry, old_data)
do Reference.set(data, new_data)
res
summarize() =
show(label, (c, t)) =
print("[{label}]: called {c}x, time: {Duration.to_string(t)}\n")
StringMap.iter(show, Reference.get(data))
}}
/*
/**
* Perform profiling on the client
*/
client Client_profiler = {{
data = Client_reference.create(StringMap.empty : stringmap((int, Duration.duration)))
init() =
Client_reference.set(data, StringMap.empty)
execute(f, label) =
t_beg = Date.now()
res = f()
t_end = Date.now()
old_data = Client_reference.get(data)
execution_time = Date.between(t_beg, t_end)
(c, t) = StringMap.get(label, old_data) ? (0, Duration.empty)
new_entry = (c + 1, Duration.add(t, execution_time))
new_data = StringMap.add(label, new_entry, old_data)
do Client_reference.set(data, new_data)
res
summarize() =
show(label, (c, t)) =
print("[{label}]: called {c}x, time: {Duration.short_string_of(t)}\n")
StringMap.iter(show, Client_reference.get(data))
}}
*/
/**
* [Profile] helps to monitor function execution time
*/
/*
* TODO fix the previous module and merge with this one
*/
Profile = {{
/** [measure(n)(f)] returns a pair: f() and the float representing the number of
seconds needed to execute f, n times. The timing information should have
better than millisecond precision, hence making it suitable for execution
profiling. */
measure(n)(f)=
get_time = %%BslTime.get_accurate_time%% : -> float
t_beg = get_time()
rec aux(n, v) =
if n == 0 then
t_end = get_time()
(t_end - t_beg, v)
else
aux(n-1, f())
aux(n-1, f())
/** [instrument(n,report)(f)] returns the value of f() and call report with the time needed to execute f, n times */
instrument(n, report)(f)=
(t, r) = measure(n)(f)
do report(t) : void
r
}}