Skip to content

Commit 24ef082

Browse files
committed
fix(bench): cpu time stats
1 parent 956cf4e commit 24ef082

File tree

10 files changed

+29
-35
lines changed

10 files changed

+29
-35
lines changed

benchmark/config/config.exs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
use Mix.Config
22

33
config :logger, :console, metadata: [:module, :line]
4-
5-
config :sasl, errlog_type: :error
6-
7-
import_config "#{Mix.env()}.exs"
4+
config :logger, level: :info

benchmark/config/dev.exs

Lines changed: 0 additions & 1 deletion
This file was deleted.

benchmark/config/prod.exs

Lines changed: 0 additions & 3 deletions
This file was deleted.

benchmark/lib/benchmark/client_manager.ex

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule Benchmark.ClientManager do
55
alias Benchmark.Stats.Histogram
66

77
defstruct init_time: nil,
8-
cpu_acc: %{sys_t: 0, user_t: 0},
8+
init_rusage: nil,
99
histograms: %{},
1010
histogram_opts: nil
1111

@@ -39,12 +39,10 @@ defmodule Benchmark.ClientManager do
3939
end)
4040
end)
4141

42-
# relative util will be returned next time calling
43-
:cpu_sup.util()
44-
4542
state = %__MODULE__{
4643
histograms: histograms,
4744
init_time: Time.utc_now(),
45+
init_rusage: Benchmark.Syscall.getrusage(),
4846
histogram_opts: hs_opts
4947
}
5048

benchmark/lib/benchmark/client_worker.ex

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,27 @@ defmodule Benchmark.ClientWorker do
1717
def unary_loop(ch, %{req_size: req_size, resp_size: resp_size} = payload, {pid, no} = manager) do
1818
start = Time.utc_now()
1919
unary_call(ch, req_size, resp_size)
20-
dur = Time.diff(Time.utc_now(), start, :microsecond)
20+
dur = Time.diff(Time.utc_now(), start, :nanosecond)
2121
GenServer.cast(pid, {:track_rpc, no, dur})
2222
unary_loop(ch, payload, manager)
2323
end
2424

2525
def unary_call(ch, req_size, resp_size) do
26+
payload_type = Grpc.Testing.PayloadType.value(:COMPRESSABLE)
27+
2628
payload =
2729
Grpc.Testing.Payload.new(
28-
type: Grpc.Testing.PayloadType.value(:COMPRESSABLE),
29-
body: String.duplicate(<<0>>, req_size)
30+
type: payload_type,
31+
body: List.duplicate(<<0>>, req_size)
3032
)
3133

3234
req =
3335
Grpc.Testing.SimpleRequest.new(
34-
response_type: payload.type,
36+
response_type: payload_type,
3537
response_size: resp_size,
3638
payload: payload
3739
)
3840

39-
Logger.debug("Sending rpc #{inspect(req)}")
4041
Grpc.Testing.BenchmarkService.Stub.unary_call(ch, req)
4142
end
4243
end

benchmark/lib/benchmark/server.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule Benchmark.Server do
33
port: nil,
44
pid: nil,
55
init_time: nil,
6-
cpu_acc: %{sys_t: 0, user_t: 0}
6+
init_rusage: nil
77

88
def get_stats(server, reset \\ false) do
99
{server, stats} = Benchmark.Stats.CpuTime.get_stats(server, reset)

benchmark/lib/benchmark/server_manager.ex

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ defmodule Benchmark.ServerManager do
99
cores = Benchmark.Manager.set_cores(config.core_limit)
1010
{:ok, pid, port} = GRPC.Server.start(Grpc.Testing.BenchmarkService.Server, config.port)
1111

12-
# relative util will be returned next time calling
13-
:cpu_sup.util()
14-
1512
%Benchmark.Server{
1613
cores: cores,
1714
port: port,
1815
pid: pid,
19-
init_time: Time.utc_now()
16+
init_time: Time.utc_now(),
17+
init_rusage: Benchmark.Syscall.getrusage()
2018
}
2119
end
2220

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
defmodule Benchmark.Stats.CpuTime do
22
def get_stats(state, reset \\ false) do
33
now = Time.utc_now()
4-
util = :cpu_sup.util([:detailed])
4+
rusage = Benchmark.Syscall.getrusage()
55
duration = Time.diff(now, state.init_time, :microsecond) / 1000_000
6-
{_, [kernel: sys_t, nice_user: _, user: user_t], _, _} = util
7-
%{sys_t: sys_t0, user_t: user_t0} = state.cpu_acc
8-
sys_t = sys_t0 + sys_t
9-
user_t = user_t0 + user_t
6+
7+
{utime, stime} = cpu_time_diff(rusage, state.init_rusage)
108

119
state =
1210
if reset do
13-
%{state | init_time: Time.utc_now(), cpu_acc: %{sys_t: 0, user_t: 0}}
11+
%{state | init_time: Time.utc_now(), init_rusage: rusage}
1412
else
15-
%{state | cpu_acc: %{sys_t: sys_t, user_t: user_t}}
13+
state
1614
end
1715

1816
stats = %{
1917
time_elapsed: duration,
20-
time_user: user_t,
21-
time_system: sys_t
18+
time_user: utime,
19+
time_system: stime
2220
}
2321

2422
{state, stats}
2523
end
24+
25+
def cpu_time_diff(
26+
%{utime: {u_sec1, u_usec1}, stime: {s_sec1, s_usec1}},
27+
%{utime: {u_sec0, u_usec0}, stime: {s_sec0, s_usec0}}
28+
) do
29+
utime = u_sec1 - u_sec0 + (u_usec1 - u_usec0) / 1000_000
30+
stime = s_sec1 - s_sec0 + (s_usec1 - s_usec0) / 1000_000
31+
{utime, stime}
32+
end
2633
end

benchmark/lib/grpc/worker_server.ex

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,4 @@ defmodule Grpc.Testing.WorkerService.Server do
7272
send(stream.local[:main_pid], {:quit, self()})
7373
Grpc.Testing.Void.new()
7474
end
75-
76-
def get_stats(_) do
77-
end
7875
end

benchmark/mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ defmodule Benchmark.MixProject do
1414
# Run "mix help compile.app" to learn about applications.
1515
def application do
1616
[
17-
extra_applications: [:logger, :os_mon]
17+
extra_applications: [:logger]
1818
]
1919
end
2020

0 commit comments

Comments
 (0)