Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/andytill/oneup
NIF powered global counters for erlang
https://github.com/andytill/oneup
atomics beam erlang nif
Last synced: about 2 months ago
JSON representation
NIF powered global counters for erlang
- Host: GitHub
- URL: https://github.com/andytill/oneup
- Owner: andytill
- License: apache-2.0
- Created: 2015-07-05T17:11:06.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-10-07T14:29:54.000Z (about 6 years ago)
- Last Synced: 2023-08-04T02:52:53.680Z (over 1 year ago)
- Topics: atomics, beam, erlang, nif
- Language: Erlang
- Size: 39.1 KB
- Stars: 25
- Watchers: 6
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# oneup
NIF powered, lock-free global counters for erlang using the c++11 atomic package.
Oneup is an alternative to `ets:update_counter` with improved throughput for multiple writers without the lock contention that can happen with ets.
### Status
oneup is not currently used in production and is subject to API changes.
### Usage
Create a new counter. This returns a reference that is required for further oneup operations. There is no public registry of oneup counters. Oneup counters are garbage collected like any other erlang primitive e.g. binary or tuple.
```erlang
C = oneup:new_counter().
```Increment or set the counter. Any number of processes can safely increment or set a counter. A 64 bit signed long is used to hold the value, not an erlang auto number. The maximum value is (2^63-1) or less depending on architecture.
```erlang
ok = oneup:inc(C). %% value of C becomes 1
ok = oneup:inc2(C, 10). %% value of C becomes 11
11 = oneup:set(C, 200). %% set to 200 and return previous value
201 = oneup:inc_and_get(C). %% value of C becomes 201
401 = oneup:inc_and_get(C,200). %% value of C becomes 401
```Set min or max.
```
300 = oneup:set_max(C, 300). %% set to max of current and new value and return max
300 = oneup:set_max(C, 100).
100 = oneup:set_min(C, 100). %% set to min of current and new value and return min
100 = oneup:set_min(C, 300).
```Retrieve the result. Any number of processes can safely read a counter.
```erlang
200 = oneup:get(C).
```### Performance
The benchnmark suite can be run by executing the following from the oneup directory:
make perfs
This runs a benchmarks of oneup and `ets:update_counter` tests. Testing has shown that oneup can achieve upto 100 times the throughput of ets when multiple processes write to a single key.