Skip to content

Commit 92baf65

Browse files
authored
Adjust benchmark to measure operations per second rather than individual latency. (#37)
1 parent aa76770 commit 92baf65

1 file changed

Lines changed: 17 additions & 20 deletions

File tree

benchmarks/imetrics_sparse_write_benchmark.erl

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
-export([start/0]).
99

10-
-define(WorkerProcesses, 128).
11-
-define(WorkerInterval, 10). % milliseconds
10+
-define(WorkerProcesses, 4).
11+
-define(WorkerInterval, 0). % milliseconds
1212
-define(HttpInterval, timer:seconds(15)).
1313
-define(WarmPeriod, timer:seconds(20)).
14-
-define(CollectPeriod, timer:seconds(65)).
14+
-define(CollectPeriod, timer:seconds(125)).
1515
-define(TimeUnit, microsecond).
1616

1717
start() ->
@@ -100,32 +100,30 @@ http_loop(start, Port, Collect, Data) ->
100100
start_worker_threads(0, Acc) ->
101101
Acc;
102102
start_worker_threads(N, Acc) ->
103-
Pid = spawn_link(fun() -> worker_loop(idle, N, nocollect, {0, 0}) end),
103+
Pid = spawn_link(fun() -> worker_loop(idle, N, nocollect, {0, 0, 0}) end),
104104
start_worker_threads(N-1 , [Pid|Acc]).
105105

106-
worker_loop(idle, Id, Collect, Data) ->
106+
worker_loop(idle, Id, Collect, {StartTime, EndTime, Count}) ->
107107
receive
108108
start ->
109-
worker_loop(start, Id, Collect, Data);
109+
worker_loop(start, Id, Collect, {StartTime, EndTime, Count});
110110
collect ->
111-
worker_loop(idle, Id, collect, Data)
111+
worker_loop(idle, Id, collect, {erlang:monotonic_time(?TimeUnit), EndTime, Count})
112112
end;
113-
worker_loop(start, Id, Collect, Data) ->
114-
T1 = erlang:monotonic_time(?TimeUnit),
113+
worker_loop(start, Id, Collect, {StartTime, EndTime, Count}) ->
115114
imetrics:add(sparse_write_benchmark, #{id => Id}),
116-
T2 = erlang:monotonic_time(?TimeUnit),
117115
Data2 = case Collect of
118-
nocollect -> Data;
116+
nocollect -> {StartTime, EndTime, Count};
119117
collect ->
120-
Diff = T2 - T1,
121-
{Sum, Samples} = Data,
122-
{Sum+Diff, Samples+1}
118+
{StartTime, EndTime, Count+1}
123119
end,
124120
receive
125121
{stop, From, Ref} ->
126-
erlang:send(From, {Ref, Data2});
122+
{_, _, Count2} = Data2,
123+
erlang:send(From, {Ref, {StartTime, erlang:monotonic_time(?TimeUnit), Count2}});
127124
collect ->
128-
worker_loop(start, Id, collect, Data2)
125+
{_, _, Count2} = Data2,
126+
worker_loop(start, Id, collect, {erlang:monotonic_time(?TimeUnit), EndTime, Count2})
129127
after ?WorkerInterval ->
130128
worker_loop(start, Id, Collect, Data2)
131129
end.
@@ -148,7 +146,7 @@ receive_downs([M|Rest]) ->
148146

149147
output_report(WriteData, ReadData) ->
150148
{HttpSum, HttpSamples} = lists:foldl(fun(Diff, {Sum, Samples}) -> {Sum+Diff, Samples+1} end, {0, 0}, ReadData),
151-
{WorkerSum, WorkerSamples} = lists:foldl(fun ({WorkerNSum, WorkerNSamples}, {TotalSum, TotalSamples}) -> {TotalSum+WorkerNSum, TotalSamples+WorkerNSamples} end, {0, 0}, WriteData),
149+
WorkerSum = lists:foldl(fun ({StartTime, EndTime, WorkerNSamples}, TotalSum) -> TotalSum + (WorkerNSamples / ((EndTime - StartTime) / 1_000_000)) end, 0, WriteData),
152150

153151
io:format("== Read benchmark ==~n~n"),
154152

@@ -161,14 +159,13 @@ output_report(WriteData, ReadData) ->
161159
io:format("Processes = ~p~n", [?WorkerProcesses]),
162160
io:format("Worker interval = ~p ms~n~n", [?WorkerInterval]),
163161

164-
io:format("Metric writes = ~p~n", [WorkerSamples]),
165-
io:format("Latency = ~p us (avg)~n~n", [WorkerSum / WorkerSamples]),
162+
io:format("Throughput = ~p metrics/s (avg between workers)~n~n", [WorkerSum / ?WorkerProcesses]),
166163

167164
io:format("=====================~n~n"),
168165

169166
{ok, File} = file:open("./benchmark_results.json", [write]),
170167
io:format(File, "[~n", []),
171168
io:format(File, "{\"name\": \"HTTP Response Time (avg)\", \"unit\": \"microseconds\", \"value\": ~p},~n", [HttpSum / HttpSamples]),
172-
io:format(File, "{\"name\": \"Sparse metrics write latency (avg)\", \"unit\": \"microseconds\", \"value\": ~p}~n", [WorkerSum / WorkerSamples]),
169+
io:format(File, "{\"name\": \"Metrics throughput (avg between workers)\", \"unit\": \"metrics/second\", \"value\": ~p}~n", [WorkerSum / ?WorkerProcesses]),
173170
io:format(File, "]~n", []),
174171
file:close(File).

0 commit comments

Comments
 (0)