<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Ets on cnDenis的笔记</title><link>https://blog.cndenis.com/tags/ets.html</link><description>Recent content in Ets on cnDenis的笔记</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Sun, 08 Jun 2014 00:00:00 +0000</lastBuildDate><atom:link href="https://blog.cndenis.com/tags/ets/index.xml" rel="self" type="application/rss+xml"/><item><title>【Erlang】ETS的并发性能调节选项</title><link>https://blog.cndenis.com/Erlang/2014/06/ets_read_write_concurrency.html</link><pubDate>Sun, 08 Jun 2014 00:00:00 +0000</pubDate><guid>https://blog.cndenis.com/Erlang/2014/06/ets_read_write_concurrency.html</guid><description>&lt;p&gt;ETS在创建表时, 有 &lt;code&gt;write_concurrency&lt;/code&gt; 和 &lt;code&gt;read_concurrency&lt;/code&gt; 这两个选项,
可以用以优化ETS的并发性能.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;write_concurrency&lt;/code&gt; 默认设置是 &lt;code&gt;false&lt;/code&gt;, 当表进行写操作时,
整个表会被锁起来, 直到写完为止. 把这个值设置为 &lt;code&gt;true&lt;/code&gt;, 可以优化并发写性能,
表中的不同项可以同时进行读写, 代价是会消耗一些内存, 并且会降低并发读的性能.
但是对于 &lt;code&gt;order_set&lt;/code&gt; 类型的表, 目前的Erlang版本下是不受这个选项影响的.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;read_concurrency&lt;/code&gt; 默认设置也是 &lt;code&gt;false&lt;/code&gt;, 当设置为 &lt;code&gt;true&lt;/code&gt;,
并且Erlang运行在多核机器上且开启SMP支持时, 并发读的性能会提升.
代价是读写切换变得更慢.&lt;/p&gt;
&lt;p&gt;这两个选项是不会对操作的原子性和独立性有影响的,
也就是说这两项设置&lt;strong&gt;只改变性能, 不影响逻辑&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;显然, &lt;code&gt;write_concurrency&lt;/code&gt; 适用于读少写多的表,
&lt;code&gt;read_concurrency&lt;/code&gt; 适用于读多写少的表, 以及读写交替少的表
(即读一大堆数据后再写一大堆, 读取操作较少被写入操作打断).&lt;/p&gt;
&lt;p&gt;这两个选项是可以同时开启的, 但额外的内存消耗会更大.&lt;/p&gt;
&lt;p&gt;测试一下, 分别对默认, 开启&lt;code&gt;read_concurrency&lt;/code&gt;, 开启&lt;code&gt;write_concurrency&lt;/code&gt; 这三种设置,
测试 &lt;code&gt;insert&lt;/code&gt;, &lt;code&gt;lookup&lt;/code&gt;, &lt;code&gt;update_counter&lt;/code&gt; 三种操作&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;bench for default ets
ets:insert/2
Single Process: 5664151 call per sec, 8388608 times in 1481 ms
 1000 Process: 2448296 call per sec, 4096000 times in 1673 ms
ets:lookup/2
Single Process: 6096372 call per sec, 8388608 times in 1376 ms
 1000 Process: 9481481 call per sec, 16384000 times in 1728 ms
ets:update_counter/3
Single Process: 5821379 call per sec, 8388608 times in 1441 ms
 1000 Process: 105872 call per sec, 128000 times in 1209 ms

bench for read_concurrency ets
ets:insert/2
Single Process: 5418997 call per sec, 8388608 times in 1548 ms
 1000 Process: 2493000 call per sec, 4096000 times in 1643 ms
ets:lookup/2
Single Process: 4981358 call per sec, 8388608 times in 1684 ms
 1000 Process: 10944555 call per sec, 16384000 times in 1497 ms
ets:update_counter/3
Single Process: 5769331 call per sec, 8388608 times in 1454 ms
 1000 Process: 1935727 call per sec, 2048000 times in 1058 ms

bench for write_concurrency ets
ets:insert/2
Single Process: 4371343 call per sec, 8388608 times in 1919 ms
 1000 Process: 2617252 call per sec, 4096000 times in 1565 ms
ets:lookup/2
Single Process: 4507580 call per sec, 8388608 times in 1861 ms
 1000 Process: 8062992 call per sec, 8192000 times in 1016 ms
ets:update_counter/3
Single Process: 4412734 call per sec, 8388608 times in 1901 ms
 1000 Process: 1889298 call per sec, 2048000 times in 1084 ms
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;可以看出, 开启并发性能优化的选项, 对于多进程的&lt;code&gt;update_counter&lt;/code&gt;的性能有很大帮助.&lt;/p&gt;</description></item></channel></rss>