[erlang-questions] ANN: gproc_pool + some performance tidbits

Ulf Wiger ulf@REDACTED
Tue Jun 4 11:18:41 CEST 2013


I pushed a new gproc component called gproc_pool the other day.

The main idea, apart from wanting to see how well it would work, was that I wanted to be able to register servers with gproc and then have an efficient way of pooling between them. A benefit of using gproc throughout is that the registration objects serve as a 'footprint' for each process - by listing the gproc entities for each process, you can tell a lot about its purpose.

The way gproc_pool works is that:
1. You define a pool, by naming it, and optionally specifying its size
     (gproc_pool:new(Pool) | gproc_pool:new(Pool, Type, Options))
2. You add worker names to the pool
    (gproc_pool:add_worker(Pool, Name))
3. Your servers each connect to a given name
    (gproc_pool:connect_worker(Pool, Name))
4. Users pick a worker for each request (gproc_pool:pick(Pool))

My little test code indicates that the different load-balancing strategies perform a bit differently:

(https://github.com/uwiger/gproc/blob/master/src/gproc_pool.erl#L843)

(Create a pool, add 6 workers and iterate 100k times, 
incrementing a gproc counter for each iteration.)

3> gproc_pool:test(100000,round_robin,[]).
worker stats (848):
[{a,16667},{b,16667},{c,16667},{d,16667},{e,16666},{f,16666}]
{2801884,ok}
4> gproc_pool:test(100000,hash,[]).       
worker stats (848):
[{a,16744},{b,16716},{c,16548},{d,16594},{e,16749},{f,16649}]
{1891517,ok}
5> gproc_pool:test(100000,random,[]).
worker stats (848):
[{a,16565},{b,16542},{c,16613},{d,16872},{e,16727},{f,16681}]
{3701011,ok}
6> gproc_pool:test(100000,direct,[]).
worker stats (848):
[{a,16667},{b,16667},{c,16667},{d,16667},{e,16666},{f,16666}]
{1766639,ok}
11> gproc_pool:test(100000,claim,[]).
worker stats (848):
[{a,100000},{b,0},{c,0},{d,0},{e,0},{f,0}]
{7569425,ok}


The worker stats show how evenly the workers were selected,
and the {Time, ok} comes from timer:tc/3, i.e. Time/100000 is the per-iteration cost:

round_robin: 28 usec (maintain a 'current' counter, modulo Size)
hash:  19 usec (gproc_pool:pick(Pool, Val), hash on Val)
random: 37 usec (pick a random worker, using crypto:rand_uniform/2)
direct: 18 usec (gproc_pool:pick(Pool, N), where N modulo Size selects worker)
claim: 76 usec (claim the first available worker, apply a fun, then release)

I think the per-selection cost is acceptable as-is, but could perhaps be improved (esp. the 'random' strategy is surprisingly expensive). All the selection work is done in the caller's process, BTW - no communication with the gproc or gproc_pool servers (except for admin tasks).

The 'claim' strategy is also surprisingly expensive. I believe it's because I'm using gproc:select/3 to find the first free worker. Note also that it results in an extremely uneven distribution. That's obviously because the test run claims the first available worker and then releases it before iterating - it's always going to select the first worker.)

https://github.com/uwiger/gproc/blob/master/doc/gproc_pool.md

Feedback welcome, be it with performance tips, usability tips, or other.

BR,
Ulf W

Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.
http://feuerlabs.com






More information about the erlang-questions mailing list