-
Notifications
You must be signed in to change notification settings - Fork 413
/
execution_timing.ex
184 lines (147 loc) · 4.33 KB
/
execution_timing.ex
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
defmodule Credo.Execution.ExecutionTiming do
@moduledoc """
The `ExecutionTiming` module can help in timing the execution of code parts and
storing those timing inside the `Credo.Execution` struct.
"""
use GenServer
alias Credo.Execution
@doc """
Runs the given `fun` and prints the time it took with the given `label`.
iex> Credo.Execution.ExecutionTiming.inspect("foo", fn -> some_complicated_stuff() end)
foo: 51284
"""
def inspect(label, fun) do
{duration, result} = :timer.tc(fun)
# credo:disable-for-lines:3 Credo.Check.Warning.IoInspect
duration
|> format_time()
|> IO.inspect(label: label)
result
end
@doc """
Returns the current timestamp in the same format (microseconds) as the returned starting times of `run/1`.
"""
def now(), do: :os.system_time(:microsecond)
@doc """
Runs the given `fun` and returns a tuple of `{started_at, duration, result}`.
iex> Credo.Execution.ExecutionTiming.run(fn -> some_complicated_stuff() end)
{1540540119448181, 51284, [:whatever, :fun, :returned]}
"""
def run(fun) do
started_at = now()
{duration, result} = :timer.tc(fun)
{started_at, duration, result}
end
@doc "Same as `run/1` but takes `fun` and `args` separately."
def run(fun, args) do
started_at = now()
{duration, result} = :timer.tc(fun, args)
{started_at, duration, result}
end
@doc """
Adds a timing to the given `exec` using the given values of `tags`, `started_at` and `duration`.
"""
def append(%Execution{timing_pid: pid}, tags, started_at, duration) do
spawn(fn ->
GenServer.call(pid, {:append, tags, started_at, duration})
end)
end
@doc """
Adds a timing piped from `run/2` to the given `exec` (using the given values of `tags`, `started_at` and `duration`).
"""
def append({started_at, duration, _result}, %Execution{timing_pid: pid}, tags) do
spawn(fn ->
GenServer.call(pid, {:append, tags, started_at, duration})
end)
end
@doc """
Returns all timings for the given `exec`.
"""
def all(%Execution{timing_pid: pid}) do
GenServer.call(pid, :all)
end
@doc """
Groups all timings for the given `exec` and `tag_name`.
"""
def grouped_by_tag(exec, tag_name) do
map =
exec
|> all()
|> Enum.filter(fn {tags, _started_at, _time} -> tags[tag_name] end)
|> Enum.group_by(fn {tags, _started_at, _time} -> tags[tag_name] end)
map
|> Map.keys()
|> Enum.map(fn map_key ->
sum = Enum.reduce(map[map_key], 0, fn {_tags, _, time}, acc -> time + acc end)
{[{tag_name, map_key}, {:accumulated, true}], nil, sum}
end)
end
@doc """
Returns all timings for the given `exec` and `tag_name`.
"""
def by_tag(exec, tag_name) do
exec
|> all()
|> Enum.filter(fn {tags, _started_at, _time} -> tags[tag_name] end)
end
@doc """
Returns all timings for the given `exec` and `tag_name` where the tag's value also matches the given `regex`.
"""
def by_tag(exec, tag_name, regex) do
exec
|> all()
|> Enum.filter(fn {tags, _started_at, _time} ->
tags[tag_name] && to_string(tags[tag_name]) =~ regex
end)
end
@doc """
Returns the earliest timestamp for the given `exec`.
"""
def started_at(exec) do
{_, started_at, _} =
exec
|> all()
|> List.first()
started_at
end
@doc """
Returns the latest timestamp plus its duration for the given `exec`.
"""
def ended_at(exec) do
{_, started_at, duration} =
exec
|> all()
|> List.last()
started_at + duration
end
defp format_time(time) do
cond do
time > 1_000_000 ->
"#{div(time, 1_000_000)}s"
time > 1_000 ->
"#{div(time, 1_000)}ms"
true ->
"#{time}μs"
end
end
# callbacks
@doc false
def start_server(exec) do
{:ok, pid} = GenServer.start_link(__MODULE__, [])
%Execution{exec | timing_pid: pid}
end
@doc false
def init(_) do
{:ok, []}
end
@doc false
def handle_call({:append, tags, started_at, time}, _from, current_state) do
new_current_state = [{tags, started_at, time} | current_state]
{:reply, :ok, new_current_state}
end
@doc false
def handle_call(:all, _from, current_state) do
list = Enum.sort_by(current_state, fn {_, started_at, _} -> started_at end)
{:reply, list, current_state}
end
end