目前正在将erlang应用程序从版本17转换为18。可伸缩性和性能是设计中的主要指令。该程序需要一种方法来区分和排序新输入,或者使用许多独特的单调递增的数字(连续的流),或者其他一些机制。当前版本(17)没有为此使用now(),因为它是一个可伸缩性瓶颈(全局锁),因此它是通过读取时钟和为输入的数据生成标记来实现的。我试着在18岁的时候找出最好的方法,并从我运行的测试中得到一些有趣的结果。
我预计erlang:unique_integer(单调的)结果会很差,因为我希望它有一个像now()这样的全局锁。假设时钟可以并行读取,我预计其中一个时钟功能会得到最好的结果。相反,erlang:unique_integer(单调)在我所标出的所有函数中得到了最好的结果,而时钟函数的效果更差。
有人能解释一下结果吗,告诉我哪个erlang函数应该给出最好的结果,以及哪些东西(时钟、数字生成器等)被全局锁定在18?此外,如果您看到我的测试方法存在任何问题,请务必指出它们。
测试平台/方法
windows 7 64 bit
erlang otp 18 (x64)
2 intel cores (celeron 1.8GHz)
2 erlang processes spawned to run each test function concurrently 500000 times
for a total of 1000000 times, timed with timer:tc
each test run 10 times in succession and all results recorded
基线测试
erlang:unique_integer([monotonic])
47000-94000
平行次数
erlang:unique_integer([monotonic])
~94000
ets:update_counter
450000-480000
erlang:monotonic_time
202000-218000
erlang:system_time
218000-234000
os:system_time
124000-141000
calendar:universal_time
453000-530000
发布于 2015-08-18 07:31:45
如果您询问测试方法,我希望您也包括您的代码,因为基准代码中可能有一个小错误,可能会破坏结果。因此,我编写了一个要旨,这样我们就可以使用相同的代码来比较结果。YMMV,特别是因为我使用Linux和计时器,非常依赖底层操作系统。有地雷的结果:
$ uname -a
Linux hynek-notebook 4.1.0-1-amd64 #1 SMP Debian 4.1.3-1 (2015-08-03) x86_64 GNU/Linux
$ grep 'model name' /proc/cpuinfo
model name : Intel(R) Core(TM) i5 CPU M 520 @ 2.40GHz
model name : Intel(R) Core(TM) i5 CPU M 520 @ 2.40GHz
model name : Intel(R) Core(TM) i5 CPU M 520 @ 2.40GHz
model name : Intel(R) Core(TM) i5 CPU M 520 @ 2.40GHz
$ erl
Erlang/OTP 18 [erts-7.0] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V7.0 (abort with ^G)
1> c(test).
{ok,test}
2> test:bench_all(1).
[{unique_monotonic_integer,{38341,39804}},
{update_counter,{158248,159319}},
{monotonic_time,{217531,218272}},
{system_time,{224630,226960}},
{os_system_time,{53489,53691}},
{universal_time,{114125,116324}}]
3> test:bench_all(2).
[{unique_monotonic_integer,{40109,40238}},
{update_counter,{307393,338993}},
{monotonic_time,{120024,121612}},
{system_time,{123634,124928}},
{os_system_time,{29606,29992}},
{universal_time,{177544,178820}}]
4> test:bench_all(20).
[{unique_monotonic_integer,{23796,26364}},
{update_counter,{514835,527087}},
{monotonic_time,{91916,93662}},
{system_time,{94615,96249}},
{os_system_time,{27194,27598}},
{universal_time,{317353,340187}}]
5>
首先要注意的是,只有erlang:unique_integer/0,1
和ets:update_counter/3,4,5
生成唯一的值。甚至erlang:monotonic_time/0
也可以生成两个相同的时间戳!因此,如果您想要唯一的编号,那么除了使用erlang:unique_integer/0,1
之外,您没有其他选择。如果您想要唯一的单调时间戳,可以使用{erlang:monotonic_time(), erlang:unique_integer()}
,或者如果不需要时间部分,则可以使用erlang:unique_integer([monotonic])
。如果您不需要单调和独特,您可以使用其他选项。因此,如果您需要唯一的单调数,那么只有一个很好的选择,那就是erlang:unique_integer([monotonic])
。
第二次我要指出的是,产生两个进程不足以测试可伸缩性。正如您所看到的,当我在20个进程中使用os:timestamp/0
时,它们会开始追赶erlang:unique_integer/0,1
。还有另一个问题。我们都用只有两个CPU的HW。它太少,无法测试可伸缩性。想象一下,有64颗或更多核心的HW会有什么样的效果。
编辑:使用{write_concurrency, true}
将改进ets:update_counter
,但仍然远远超出了erlang:unique_integer/0,1
。
2> test:bench(test:update_counter(),1).
{203830,213657}
3> test:bench(test:update_counter(),2).
{129148,140627}
4> test:bench(test:update_counter(),20).
{471858,501198}
发布于 2015-08-19 01:02:36
根据erlang码基的说法,erlang:unique_integer([monotonic])
只是在增加原子整数。这个动作很快。虽然这仍然会造成内存障碍,但与传统的全局锁方法相比,原子操作仍然很便宜。
https://stackoverflow.com/questions/32071752
复制相似问题