nickva commented on issue #4650:
URL: https://github.com/apache/couchdb/issues/4650#issuecomment-1599233435
I tried to compare the two approaches:
```erlang
-module(upstats).
-export([go/0, go/1, go/2]).
go() ->
go(1000, 10000).
go(N) ->
go(N, 10000).
go(N, X) ->
T0 = erlang:monotonic_time(),
Workers = [spawn_monitor(fun() ->
rand:seed(default, os:timestamp()),
upstat(X)
end) || _ <- lists:seq(1, N)],
{_Pids, Refs} = lists:unzip(Workers),
WorkersSet = sets:from_list(Refs, [{version,2}]),
ok = wait_workers(WorkersSet),
Dt = erlang:monotonic_time() - T0,
erlang:convert_time_unit(Dt, native, millisecond).
upstat(0) ->
ok;
upstat(Times) ->
Hist = case rand:uniform(5) of
1 -> [couchdb, request_time];
2 -> [couchdb, dbinfo];
3 -> [couchdb, httpd, bulk_docs];
4 -> [fsync, time];
5 -> [couchdb, db_open_time]
end,
couch_stats:update_histogram(Hist, 100),
upstat(Times - 1).
wait_workers(Workers) when is_map(Workers) ->
case sets:size(Workers) of
0 ->
ok;
_ ->
receive
{'DOWN', Ref, process, _, normal} ->
Workers1 = sets:del_element(Ref, Workers),
wait_workers(Workers1);
{'DOWN', Ref, process, _, Err} ->
Workers1 = sets:del_element(Ref, Workers),
io:format("~n worker ~p crashed: ~p~n", [Ref, Err]),
wait_workers(Workers1)
end
end.
```
With an example counters update:
``````erlang
-module(upstats_counter).
-export([go/0, go/1, go/2]).
go() ->
go(1000, 10000).
go(N) ->
go(N, 10000).
go(N, X) ->
Ref = counters:new(1024, [write_concurrency]),
persistent_term:put(term, Ref),
T0 = erlang:monotonic_time(),
Workers = [spawn_monitor(fun() ->
rand:seed(default, os:timestamp()),
upstat(X)
end) || _ <- lists:seq(1, N)],
{_Pids, Refs} = lists:unzip(Workers),
WorkersSet = sets:from_list(Refs, [{version,2}]),
ok = wait_workers(WorkersSet),
Dt = erlang:monotonic_time() - T0,
erlang:convert_time_unit(Dt, native, millisecond).
upstat(0) ->
ok;
upstat(Times) ->
Ref = persistent_term:get(term),
counters:add(Ref, rand:uniform(1024), 100),
upstat(Times - 1).
wait_workers(Workers) when is_map(Workers) ->
case sets:size(Workers) of
0 ->
ok;
_ ->
receive
{'DOWN', Ref, process, _, normal} ->
Workers1 = sets:del_element(Ref, Workers),
wait_workers(Workers1);
{'DOWN', Ref, process, _, Err} ->
Workers1 = sets:del_element(Ref, Workers),
io:format("~n worker ~p crashed: ~p~n", [Ref, Err]),
wait_workers(Workers1)
end
end.
```
```erlang
([email protected])18> upstats_counter:go(50000, 1000).
1385
([email protected])19> upstats_counter:go(50000, 1000).
1425
([email protected])20> upstats_counter:go(50000, 1000).
1427
([email protected])21> upstats_counter:go(50000, 1000).
1438
([email protected])22> upstats:go(50000, 1000).
15131
([email protected])23> upstats:go(50000, 1000).
16075
([email protected])24> upstats:go(50000, 1000).
14869
```
Even with folsom front loading and updating only 1024 values per second it's
till 10x slower!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]