<div dir="ltr">Try using mnesia:write instead of mnesia:dirty_write inside your fun.<div><br></div></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Mar 14, 2016 at 12:39 PM Roberto Ostinelli <<a href="mailto:roberto@widetag.com">roberto@widetag.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>So,</div><div>I took the time to benchmark a little more. I'm hoping that some kind soul can help me understand this here.</div><div><br></div><div>As said, fundamentally I don't see any speed difference when using sync_dirty and async_dirty.</div><div>The more nodes you add, the slower insertions will be even if the docs for async_dirty state [1]:</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"></blockquote></div><div dir="ltr"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>"By passing the same "fun" as an argument to the function mnesia:sync_dirty(Fun [, Args]), it is performed in almost the same context as the function mnesia:async_dirty/1,2. The difference is that the operations are performed synchronously. The caller waits for the updates to be performed on all active replicas."</div></blockquote></div><div dir="ltr"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><span style="font-size:12.8px">"The functions wait for the operation to be performed on one node but not the others. If the table resides locally, no waiting occurs."</span><br></div></blockquote></div><div dir="ltr"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"></blockquote><div><br></div><div>These are the different types of write that I've used and their benchmarks:</div><div><br></div><div><br></div><div><b>mnesia:dirty_write/1</b></div><div><br></div><div><font face="monospace, monospace">write_bench(Count) -></font></div><div><font face="monospace, monospace"> F = fun(N) -></font></div><div><font face="monospace, monospace"> mnesia:dirty_write(#test_table{key = N, value = N})</font></div><div><font face="monospace, monospace"> end,</font></div><div><font face="monospace, monospace"> lists:foreach(F, lists:seq(1, Count)).</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">1> mnesia_test:write(['<a href="mailto:1@127.0.0.1" target="_blank">1@127.0.0.1</a>']).</font></div><div><font face="monospace, monospace">Written in 1.494185 sec, at a rate of 334630.5845661682/sec</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">1> mnesia_test:write(['<a href="mailto:1@127.0.0.1" target="_blank">1@127.0.0.1</a>','<a href="mailto:2@127.0.0.1" target="_blank">2@127.0.0.1</a>']).</font></div><div><font face="monospace, monospace">Written in 2.865135 sec, at a rate of 174511.84673671573/sec</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">1> mnesia_test:write(['<a href="mailto:1@127.0.0.1" target="_blank">1@127.0.0.1</a>','<a href="mailto:2@127.0.0.1" target="_blank">2@127.0.0.1</a>','<a href="mailto:3@127.0.0.1" target="_blank">3@127.0.0.1</a>','<a href="mailto:4@127.0.0.1" target="_blank">4@127.0.0.1</a>']).</font></div><div><font face="monospace, monospace">Written in 77.847125 sec, at a rate of 6422.844774293207/sec</font></div><div><br></div><div><br></div><div><br></div><div><b>mnesia:activity/2 with sync_dirty</b></div><div><br></div><div><font face="monospace, monospace">write_bench(Count) -></font></div><div><font face="monospace, monospace"> F = fun(N) -></font></div><div><font face="monospace, monospace"> mnesia:activity(sync_dirty, fun() -></font></div><div><font face="monospace, monospace"> mnesia:dirty_write(#test_table{key = N, value = N})</font></div><div><font face="monospace, monospace"> end)</font></div><div><font face="monospace, monospace"> end,</font></div><div><font face="monospace, monospace"> lists:foreach(F, lists:seq(1, Count)).</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">1> mnesia_test:write(['<a href="mailto:1@127.0.0.1" target="_blank">1@127.0.0.1</a>']).</font></div><div><font face="monospace, monospace">Written in 1.79734 sec, at a rate of 278188.8791213682/sec</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">1> mnesia_test:write(['<a href="mailto:1@127.0.0.1" target="_blank">1@127.0.0.1</a>','<a href="mailto:2@127.0.0.1" target="_blank">2@127.0.0.1</a>']).</font></div><div><font face="monospace, monospace">Written in 3.570957 sec, at a rate of 140018.48804116095/sec</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">1> mnesia_test:write(['<a href="mailto:1@127.0.0.1" target="_blank">1@127.0.0.1</a>','<a href="mailto:2@127.0.0.1" target="_blank">2@127.0.0.1</a>','<a href="mailto:3@127.0.0.1" target="_blank">3@127.0.0.1</a>','<a href="mailto:4@127.0.0.1" target="_blank">4@127.0.0.1</a>']).</font></div><div><font face="monospace, monospace">Written in 92.945978 sec, at a rate of 5379.468921183443/sec</font></div><div><br></div><div><br></div><div><br></div><div><b>mnesia:activity/2 with async_dirty</b></div><div><br></div><div><font face="monospace, monospace">write_bench(Count) -></font></div><div><font face="monospace, monospace"> F = fun(N) -></font></div><div><font face="monospace, monospace"> mnesia:activity(async_dirty, fun() -></font></div><div><font face="monospace, monospace"> mnesia:dirty_write(#test_table{key = N, value = N})</font></div><div><font face="monospace, monospace"> end)</font></div><div><font face="monospace, monospace"> end,</font></div><div><font face="monospace, monospace"> lists:foreach(F, lists:seq(1, Count)).</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">1> mnesia_test:write(['<a href="mailto:1@127.0.0.1" target="_blank">1@127.0.0.1</a>']).</font></div><div><font face="monospace, monospace">Written in 1.638162 sec, at a rate of 305220.11864516453/sec</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">1> mnesia_test:write(['<a href="mailto:1@127.0.0.1" target="_blank">1@127.0.0.1</a>','<a href="mailto:2@127.0.0.1" target="_blank">2@127.0.0.1</a>']).</font></div><div><font face="monospace, monospace">Written in 3.255289 sec, at a rate of 153596.19376344158/sec</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">1> mnesia_test:write(['<a href="mailto:1@127.0.0.1" target="_blank">1@127.0.0.1</a>','<a href="mailto:2@127.0.0.1" target="_blank">2@127.0.0.1</a>','<a href="mailto:3@127.0.0.1" target="_blank">3@127.0.0.1</a>','<a href="mailto:4@127.0.0.1" target="_blank">4@127.0.0.1</a>']).</font></div><div><font face="monospace, monospace">Written in 98.841335 sec, at a rate of 5058.61237102878/sec</font></div><div><br></div><div><br></div><div><br></div><div><b>mnesia:async_dirty/1<br></b></div><div><br></div><div><font face="monospace, monospace">write_bench(Count) -></font></div><div><font face="monospace, monospace"> F = fun(N) -></font></div><div><font face="monospace, monospace"> mnesia:async_dirty(fun() -></font></div><div><font face="monospace, monospace"> mnesia:dirty_write(#test_table{key = N, value = N})</font></div><div><font face="monospace, monospace"> end)</font></div><div><font face="monospace, monospace"> end,</font></div><div><font face="monospace, monospace"> lists:foreach(F, lists:seq(1, Count)).</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">1> mnesia_test:write(['<a href="mailto:1@127.0.0.1" target="_blank">1@127.0.0.1</a>']).</font></div><div><font face="monospace, monospace">Written in 1.688114 sec, at a rate of 296188.5275520492/sec</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">1> mnesia_test:write(['<a href="mailto:1@127.0.0.1" target="_blank">1@127.0.0.1</a>','<a href="mailto:2@127.0.0.1" target="_blank">2@127.0.0.1</a>']).</font></div><div><font face="monospace, monospace">Written in 3.166962 sec, at a rate of 157880.0124535754/sec</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">1> mnesia_test:write(['<a href="mailto:1@127.0.0.1" target="_blank">1@127.0.0.1</a>','<a href="mailto:2@127.0.0.1" target="_blank">2@127.0.0.1</a>','<a href="mailto:3@127.0.0.1" target="_blank">3@127.0.0.1</a>','<a href="mailto:4@127.0.0.1" target="_blank">4@127.0.0.1</a>']).</font></div><div><font face="monospace, monospace">Written in 93.074646 sec, at a rate of 5372.032250329483/sec</font></div><div><br></div><div><br></div><div><br></div><div>Any ideas?</div></div><div dir="ltr"><div><br></div><div>Best,</div><div>r.</div><div><br></div><div>[1] <a href="http://erlang.org/doc/apps/mnesia/Mnesia_chap4.html" style="font-size:12.8px" target="_blank">http://erlang.org/doc/apps/mnesia/Mnesia_chap4.html</a></div><div><br></div><div><br></div></div><div dir="ltr"><div><font face="monospace, monospace">%%%%%%%%%%%%%%%%%%%%%%% FULL MODULE %%%%%%%%%%%%%%%%%%%%%%%</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">-module(mnesia_test).</font></div><div><font face="monospace, monospace">-compile(export_all).</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">-record(test_table, {</font></div><div><font face="monospace, monospace"> key = undefined :: any(),</font></div><div><font face="monospace, monospace"> value = undefined :: any()</font></div><div><font face="monospace, monospace">}).</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">write(Nodes) -></font></div><div><font face="monospace, monospace"> Count = 500000,</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> connect_nodes(Nodes),</font></div><div><font face="monospace, monospace"> start_mnesia_on(Nodes),</font></div><div><font face="monospace, monospace"> create_table_in(Nodes),</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> {Time, _} = timer:tc(?MODULE, write_bench, [Count]),</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"> io:format("Written in ~p sec, at a rate of ~p/sec~n", [</font></div><div><font face="monospace, monospace"> Time/1000000,</font></div><div><font face="monospace, monospace"> Count/Time*1000000</font></div><div><font face="monospace, monospace"> ]).</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">connect_nodes(Nodes) -></font></div><div><font face="monospace, monospace"> [true = net_kernel:connect_node(Node) || Node <- Nodes].</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">start_mnesia_on(Nodes) -></font></div><div><font face="monospace, monospace"> [rpc:call(Node, application, start, [mnesia]) || Node <- Nodes].</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">create_table_in(Nodes) -></font></div><div><font face="monospace, monospace"> mnesia:change_config(extra_db_nodes, Nodes),</font></div><div><font face="monospace, monospace"> mnesia:create_table(test_table, [</font></div><div><font face="monospace, monospace"> {type, set},</font></div><div><font face="monospace, monospace"> {ram_copies, Nodes},</font></div><div><font face="monospace, monospace"> {attributes, record_info(fields, test_table)},</font></div><div><font face="monospace, monospace"> {storage_properties, [{ets, [{read_concurrency, true}]}]}</font></div><div><font face="monospace, monospace"> ]).</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">write_bench(Count) -></font></div><div><font face="monospace, monospace"> F = fun(N) -></font></div><div><font face="monospace, monospace"> mnesia:activity(ets, fun() -></font></div><div><font face="monospace, monospace"> mnesia:dirty_write(#test_table{key = N, value = N})</font></div><div><font face="monospace, monospace"> end)</font></div><div><font face="monospace, monospace"> end,</font></div><div><font face="monospace, monospace"> lists:foreach(F, lists:seq(1, Count)).</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</font></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Mar 11, 2016 at 12:25 AM, Ryan <span dir="ltr"><<a href="mailto:zzantozz@gmail.com" target="_blank">zzantozz@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div text="#000000" bgcolor="#FFFFFF"><span>
<div>On 03/10/2016 05:10 PM, Roberto
Ostinelli wrote:<br>
</div>
<blockquote type="cite">
<div>Thank you for your input.</div>
<div><br>
</div>
<div>This is interesting since the documentation [1] states:</div>
<div><br>
</div>
<div><span style="background-color:rgba(255,255,255,0)">"By
passing the same "fun" as an argument to the function <span style="font-weight:bold"><a href="http://erlang.org/doc/man/mnesia.html#sync_dirty-2" style="text-decoration:none" target="_blank">mnesia:sync_dirty(Fun [,
Args])</a></span>, it is performed in almost the same
context as the function <span style="font-weight:bold"><a href="http://erlang.org/doc/man/mnesia.html#async_dirty-2" style="text-decoration:none" target="_blank">mnesia:async_dirty/1,2</a></span>.
The difference is that the operations are performed
synchronously. The caller waits for the updates to be
performed on all active replicas."</span></div>
<div><br>
</div>
<div>Maybe I'm not reading this right?</div>
<div><br>
</div>
</blockquote></span>
I agree that those docs imply an async_dirty call doesn't wait for
anything beyond the one node. It specifically says, under
async_dirty, that, "The functions wait for the operation to be
performed on one node but not the others. If the table resides
locally, no waiting occurs."<br>
<br>
I may have misinterpreted the code I was looking at, or the docs
could be misleading. I'll wait eagerly for someone more
well-informed to come along and shed some light on the situation.<br>
</div>
</blockquote></div><br></div>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div>