Skip to content

Commit 4d3581b

Browse files
committed
doc: rmean
1 parent 5399752 commit 4d3581b

File tree

3 files changed

+249
-174
lines changed

3 files changed

+249
-174
lines changed

README.md

Lines changed: 33 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -146,202 +146,62 @@ local element_count = my_heap:count()
146146

147147
The `rmean` module provides functionality for efficient moving average calculations with specified window sizes.
148148

149-
### Usage
149+
The best use of rmean would be when you want to have Average Calls per second during last 5 seconds.
150150

151-
For the most convenient use of the `rmean` module, the `default` instance is provided with a window size of 5 seconds, similar to what is used in Tarantool. Here are some examples demonstrating the usage of the `default` rmean instance:
151+
Easy to set and use:
152152

153153
```lua
154-
local rmean = require('algo.rmean')
154+
local rmean = require 'algo.rmean'
155155

156-
-- Create a new collector in the default rmean instance with the same window size (5 seconds)
157-
local my_collector = rmean.collector('my_collector')
158-
```
159-
160-
#### Observing Values and Calculating Metrics
161-
162-
```lua
163-
-- Observe values for the collector
164-
my_collector:observe(10)
165-
my_collector:observe(15)
166-
my_collector:observe(20)
167-
168-
-- Calculate the moving average per second for the collector
169-
local avg_per_sec = my_collector:per_second()
170-
```
171-
172-
#### Getting All Registered Collectors in the Default rmean Instance
156+
local calls_storage = rmean.collector('rmean_calls_storage', --[[ [window=(default: 5)] ]])
173157

174-
```lua
175-
local all_collectors = rmean.getall()
176-
```
158+
local function call_storage()
159+
-- increase by one each time, when method is called
160+
calls_storage:observe(1)
161+
end
177162

178-
#### Freeing a Specific Collector in the Default rmean Instance
163+
calls_storage:per_second() -- => gives Moving Average per Second
164+
calls_storage:max() -- => gives Moving Max within Time Window (default 5 sec)
165+
calls_storage:sum() -- => gives Moving Sum within Time Window (default 5 sec)
166+
calls_storage:mean() -- => gives Moving Mean within Time Window (default 5 sec)
167+
calls_storage:min() -- => gives Moving Min within Time Window (default 5 sec)
168+
calls_storage:hits() -- => gives Moving Count within Time Window (default 5 sec)
179169

180-
```lua
181-
-- Free a specific collector
182-
-- Collector will become unusable, though it's data will be preserved.
183-
-- This is a true way to destroy collector
184-
rmean.free(my_collector)
170+
-- rmean can be easily connected to metrics:
171+
rmean.default:set_registry(require 'metrics'.registry)
185172
```
186173

187-
#### Note
188-
189-
- The `default` rmean instance is the most preferred way to use `rmean`, as it has a window size of 5 seconds, aligning with the common practice in Tarantool.
190-
191-
#### Collector methods
192-
193-
The `rmean` module provides methods to efficiently calculate and access various metrics within the moving average collectors.
174+
Rmean can be used to collect statistics of both discreate and continious variables.
194175

195-
##### `sum([depth=window_size])`
196-
197-
- **Usage**: Retrieves the moving sum value within a specified time depth.
198-
- **When to Use**: This method is useful when you need to track the cumulative sum of values observed by the collector over a specific time period.
199-
200-
##### `min([depth=window_size])`
201-
202-
- **Usage**: Returns the moving minimum value within a specified time depth.
203-
- **When to Use**: Use this method when you want to find the minimum value observed by the collector within a specific time window.
204-
205-
##### `max([depth=window_size])`
206-
207-
- **Usage**: Retrieves the moving maximum value within a specified time depth.
208-
- **When to Use**: Utilize this method to determine the maximum value observed by the collector within a particular time frame.
209-
210-
##### `count and total fields`
211-
212-
- **Count Field**: The `count` field represents the monotonic counter of all collected values from the last reset.
213-
- **Total Field**: The `total` field stores the sum of all values collected by the collector from the last reset.
214-
- **When to Use**: You can access these fields to keep track of the total count of observations and the cumulative total sum of values collected by the collector.
176+
Collect Running Mean (Moving Average) of Latency of your calls:
215177

216178
```lua
217-
-- Obtain the moving sum value for the last 4 seconds
218-
local sum_last_4_sec = my_collector:sum(4)
179+
local latency_rmean = rmean.collector('latency')
219180

220-
-- Get the minimum value observed in the last 3 seconds
221-
local min_last_3_sec = my_collector:min(3)
181+
latency_rmean:max() -- Will produce Moving maximum of the latency within specified window
182+
latency_rmean:min() -- Will produce Moving minimum of the latency within specified window
183+
latency_rmean:mean() -- Will produce Moving average of the latency within specified window
222184

223-
-- Retrieve the maximum value in the last 2 seconds
224-
local max_last_2_sec = my_collector:max(2)
185+
-- latency_rmean:per_second() DOES not posses any meaningfull stats
225186

226-
-- Access the total sum and count fields of the collector
227-
local total_sum = my_collector.total
228-
local observation_count = my_collector.count
187+
latency_rmean:hits() -- Will produce Moving count of observed values within specified window
229188
```
230189

231-
**Note:** Ensure that the `depth` parameter does not exceed the `window size` of the collector.
232-
233-
### Integrating with tarantool/metrics
190+
Collect Per Second statistics for your calls or bytes
234191

235192
```lua
236-
local metrics = require('metrics')
237-
metrics.registry:register(rmean)
238-
```
239-
240-
After registering `rmean` in `tarantool/metrics`, you can seamlessly collect metrics from all registered named rmean collectors.
241-
242-
### Setting Labels for `rmean` Collectors
193+
local tuple_size_rmean = rmean.collector('tuple_size')
243194

244-
Each collector within the `rmean` module allows you to set custom labels to provide additional context or categorization for the collected metrics.
195+
-- Let's assume you measure bsize of tuples you save into Database
196+
tuple_size_rmean:observe(tuple:bsize())
245197

246-
```lua
247-
-- Set custom labels for a collector
248-
my_collector:set_labels({ name = 'example_collector', environment = 'production' })
198+
tuple_size_rmean:per_second() -- will produce Moving average of bytes per second
199+
tuple_size_rmean:max()
200+
tuple_size_rmean:min()
201+
tuple_size_rmean:hits()
249202
```
250203

251-
Each collector within the `rmean` module provides metrics suitable for export to Prometheus via the `tarantool/metrics` module. The metrics available for export are as follows:
252-
253-
- **rmean_per_second**: Represents the running average of the collected values.
254-
- **rmean_sum**: Represents the running sum of the collected values.
255-
- **rmean_min**: Represents the minimum value observed within the collector's window.
256-
- **rmean_max**: Represents the maximum value observed within the collector's window.
257-
- **rmean_count**: Represents the number of observations made by the collector.
258-
- **rmean_total**: Represents the total sum of all collected values.
259-
260-
### Advanced Usage
261-
262-
1. **Creating a New `rmean` Instance**:
263-
264-
```lua
265-
local rmean = require('algo.rmean')
266-
267-
-- Create a new rmean instance with a specified name, resolution, and window size
268-
local my_rmean = rmean.new('my_rmean_instance', 1, 5)
269-
```
270-
271-
1. **Creating a New Collector**:
272-
273-
```lua
274-
-- Create a new collector within the rmean instance
275-
local new_collector = my_rmean:collector('my_collector', 5)
276-
```
277-
278-
2. **Getting All Collectors**:
279-
280-
```lua
281-
-- Get all registered collectors within the rmean instance
282-
local all_collectors = my_rmean:getall()
283-
```
284-
285-
3. **Getting a Specific Collector**:
286-
287-
```lua
288-
-- Get a specific collector by name
289-
local specific_collector = my_rmean:get('my_collector')
290-
```
291-
292-
4. **Observing Values and Calculating Metrics**:
293-
294-
```lua
295-
-- Observe a value for a collector
296-
specific_collector:observe(10)
297-
298-
-- Calculate the moving average per second for a collector
299-
local avg_per_sec = specific_collector:per_second()
300-
```
301-
302-
5. **Reloading a Collector**:
303-
304-
```lua
305-
-- Reload a collector from an existing one
306-
-- specific_collector will be unsusable after executing this call
307-
local reloaded_collector = my_rmean:reload(specific_collector)
308-
```
309-
310-
6. **Starting and Stopping the System**:
311-
312-
```lua
313-
-- Stop the system and disable creating new collectors
314-
my_rmean:stop()
315-
316-
-- Start the system to begin calculating averages
317-
my_rmean:start()
318-
```
319-
320-
7. **Freeing Collectors**:
321-
322-
```lua
323-
-- Free a specific collector
324-
my_rmean:free(specific_collector)
325-
```
326-
327-
8. **Metrics Collection**:
328-
329-
```lua
330-
-- Collect metrics from all registered collectors
331-
local metrics_data = my_rmean:collect()
332-
```
333-
334-
9. **Setting Metrics Registry**:
335-
336-
```lua
337-
-- Set a metrics registry for the rmean instance
338-
my_rmean:set_registry(metrics_registry)
339-
```
340-
341-
### Notes
342-
343-
- The system is automatically started when the rmean instance is created. Manual starting is only required if it was previously stopped.
344-
- The module efficiently handles moving average calculations even with a large number of parallel running collectors and provides high-performance metrics collection capabilities.
204+
Read more at [rmean](./doc/rmean.md)
345205

346206
## Ordered Dictionary (algo.odict)
347207

algo/rmean.lua

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,22 @@ function collector:sum(depth)
376376
return sum
377377
end
378378

379+
--- Returns moving count value
380+
---
381+
--- Equivalent to COUNT(VALUE[0:depth])
382+
---
383+
---@param depth integer? depth in seconds (default=window size, [1,window size])
384+
---@return number count
385+
function collector:hits(depth)
386+
depth = _get_depth(depth, self.window)
387+
388+
local count = 0
389+
for i = 1, depth/self._resolution do
390+
count = count + self.hit_value[i]
391+
end
392+
return count
393+
end
394+
379395
---Calculates and returns moving average value with given depth
380396
---
381397
---Equivalent to SUM(VALUE[0:depth]) / COUNT(VALUE[0:depth])
@@ -474,12 +490,13 @@ function collector:roll(dt)
474490
if j > 0 then
475491
sum[j], min[j], max[j], hit[j] = sum[j-1], min[j-1], max[j-1], hit[j-1]
476492
else
493+
-- j == 0
477494
sum[j] = avg
478495
end
479496
j = j - 1
480497
end
481498
for i = j, 1, -1 do
482-
sum[i], min[i], max[i], hit[i] = avg, min[0], max[0], hit[i]
499+
sum[i], min[i], max[i], hit[i] = avg, min[0], max[0], hit[0]
483500
end
484501
sum[0] = 0
485502
hit[0] = 0

0 commit comments

Comments
 (0)