Module molly.gen
Module with functions for generators.
One of the key pieces of a Molly test is a generators for
client and nemesis operations. These generators will create a
finite or infinite sequence of operations. It is often test
have its own nemesis generator, but most likely shares a common
client generator with other tests. A nemesis generator, for
instance, might be a sequence of partition, sleep, and restore,
repeated infinitely. A client generator will specify a random,
infinite sequence of client operation, as well as the
associated parameters such as durability level, the document
key, the new value to write or CAS (Compare-And-Set), etc. When
a test starts, the client generator feeds client operations to
the client and the nemesis generator feeds operations to the
nemesis. The test will continue until either the nemesis
generator has completed a specified number of operations, a
time limit is reached, or an error is thrown.
Example of generator that generates two operations w and r:
local w = function() return { f = 'w', v = math.random(1, 10) } end
local r = function() return { f = 'r', v = nil } end
gen.rands(0, 2):map(function(x)
return (x == 0 and r()) or
(x == 1 and w()))
end):take(100)
local w = function(x) return { f = 'w', v = x } end
gen.map(w, gen.rands(1, 10):take(50))
References:
See also:
| filter () |
Return a new iterator of those elements that satisfy the
predicate. |
| remove_if () |
An alias for filter(). |
| grep () |
If regexp_or_predicate is string then the parameter is used
as a regular expression to build filtering predicate. |
| partition () |
The function returns two iterators where elements do and do
not satisfy the predicate. |
| foldl () |
The function reduces the iterator from left to right using the
binary operator accfun and the initial value initval. |
| reduce () |
An alias to foldl(). |
| length () |
Return a number of elements in gen, param, state iterator. |
| totable () |
Return a new table (array) from iterated values. |
| tomap () |
Return a new table (map) from iterated values. |
| zip (...) |
Return a new iterator where i-th return value contains the
i-th element from each of the iterators. |
| cycle (iterator) |
A cycled version of an iterator. |
| chain (...) |
Make an iterator that returns elements from the first iterator
until it is exhausted, then proceeds to the next iterator,
until all of the iterators are exhausted. |
| cycle_times (...) |
Cycles between several generators on a rotating schedule. |
| mix (...) |
A random mixture of a number generators. |
| flip_flop (a, b) |
Emits an operation from generator A, then B, then A again,
then B again, etc. |
| log () |
A generator which, when asked for an operation, logs
a message and yields nil. |
| stagger (dt) |
(TODO) Operations from that generator are scheduled at
uniformly random intervals
between 0 to 2 * (dt seconds). |
| time_limit (duration) |
Stops generating items when time limit is exceeded. |
-
iter (object)
-
Make an iterator from the iterable object.
See fun.iter.
Parameters:
- object
- an iterable object (map, array, string).
Returns:
an iterator
Usage:
> for _it, v in gen.iter({1, 2, 3}) do print(v) end
1
2
3
...
> for _it, v in gen.iter({a = 1, b = 2, c = 3}) do print(v) end
b
a
c
...
> for _it, v in gen.iter("abc") do print(v) end
a
b
c
...
-
each (function, iterator)
-
Execute the function
fun for each iteration value.
See fun.each.
Parameters:
- function
- iterator
- an iterator or iterable object
Returns:
none
Usage:
> gen.each(print, { a = 1, b = 2, c = 3})
b 2
a 1
c 3
...
-
for_each ()
-
An alias for each().
See gen.each.
-
foreach ()
-
An alias for each().
See gen.each.
-
range ([start], stop[, step])
-
The iterator to create arithmetic progressions.
Iteration values are generated within closed interval
[start, stop] (i.e. stop is included). If the start
argument is omitted, it defaults to 1 (stop > 0) or to -1
(stop < 0). If the step argument is omitted, it defaults to
1 (start <= stop) or to -1 (start > stop). If step is
positive, the last element is the largest start + i * step
less than or equal to stop; if step is negative, the last
element is the smallest start + i * step greater than or
equal to stop. step must not be zero (or else an error is
raised). range(0) returns empty iterator.
See fun.range.
Parameters:
- start
number
– an endpoint of the interval.
(optional)
- stop
number
– an endpoint of the interval.
- step
number
– a step.
(optional)
Returns:
an iterator
Usage:
> for _it, v in gen.range(1, 6) do print(v) end
1
2
3
4
5
6
...
> for _it, v in gen.range(1, 6, 2) do print(v) end
1
3
5
...
-
duplicate ()
-
The iterator returns values over and over again indefinitely.
All values that passed to the iterator are returned as-is
during the iteration.
See fun.duplicate.
Usage:
> gen.each(print, gen.take(3, gen.duplicate('a', 'b', 'c')))
a b c
a b c
a b c
...
-
xrepeat ()
-
An alias for duplicate().
See gen.duplicate.
-
replicate ()
-
An alias for duplicate().
See gen.duplicate.
-
tabulate ()
-
Return
fun(0), fun(1), fun(2), ... values indefinitely.
See fun.tabulate.
-
rands ()
-
See fun.rands.
-
take_n ()
-
See fun.take_n.
-
take_while ()
-
See fun.take_while.
-
take ()
-
See fun.take.
-
drop_n ()
-
See fun.drop_n.
-
drop_while ()
-
See fun.drop_while.
-
drop ()
-
See fun.drop.
-
span ()
-
See fun.span.
-
split ()
-
An alias for span().
See gen.span.
-
split_at ()
-
An alias for span().
See gen.span.
-
index ()
-
See fun.index.
-
index_of ()
-
An alias for index().
See gen.index.
-
elem_index ()
-
An alias for index().
See gen.index.
-
indexes ()
-
See fun.indexes.
-
indices ()
-
An alias for indexes().
See gen.indexes.
-
elem_indexes ()
-
An alias for indexes().
See gen.indexes.
-
elem_indices ()
-
An alias for indexes().
See gen.indexes.
-
filter ()
-
Return a new iterator of those elements that satisfy the
predicate.
See fun.filter.
-
remove_if ()
-
An alias for filter().
See gen.filter.
-
grep ()
-
If
regexp_or_predicate is string then the parameter is used
as a regular expression to build filtering predicate. Otherwise
the function is just an alias for gen.filter.
See fun.grep.
-
partition ()
-
The function returns two iterators where elements do and do
not satisfy the predicate.
See fun.partition.
-
foldl ()
-
The function reduces the iterator from left to right using the
binary operator
accfun and the initial value initval.
See fun.foldl.
-
reduce ()
-
An alias to foldl().
See gen.foldl.
-
length ()
-
Return a number of elements in
gen, param, state iterator.
See fun.length.
-
totable ()
-
Return a new table (array) from iterated values.
See fun.totable.
-
tomap ()
-
Return a new table (map) from iterated values.
See fun.tomap.
-
is_prefix_of ()
-
See fun.isprefixof.
-
is_null ()
-
See fun.is_null.
-
all ()
-
See fun.all.
-
every ()
-
An alias for all().
See gen.all.
-
any ()
-
See fun.any.
-
some ()
-
An alias for any().
See gen.any.
-
map ()
-
See fun.map.
-
enumerate ()
-
See fun.enumerate.
-
intersperse ()
-
See fun.intersperse.
-
zip (...)
-
Return a new iterator where i-th return value contains the
i-th element from each of the iterators. The returned iterator
is truncated in length to the length of the shortest iterator.
For multi-return iterators only the first variable is used.
See fun.zip.
Parameters:
Returns:
an iterator
-
cycle (iterator)
-
A cycled version of an iterator.
Make a new iterator that returns elements from
{gen, param, state} iterator until the end and then "restart"
iteration using a saved clone of {gen, param, state}. The
returned iterator is constant space and no return values are
buffered. Instead of that the function make a clone of the
source {gen, param, state} iterator. Therefore, the source
iterator must be pure functional to make an indentical clone.
Infinity iterators are supported, but are not recommended.
See fun.cycle.
Parameters:
Returns:
an iterator
-
chain (...)
-
Make an iterator that returns elements from the first iterator
until it is exhausted, then proceeds to the next iterator,
until all of the iterators are exhausted. Used for treating
consecutive iterators as a single iterator. Infinity iterators
are supported, but are not recommended.
See fun.chain.
from right).
Parameters:
Returns:
an iterator, a consecutive iterator from sources (left
Usage:
> fun.each(print, fun.chain(
> fun.range(5, 1, -1), fun.range(1, 5)))
5
4
3
2
1
1
2
3
4
5
...
-
cycle_times (...)
-
Cycles between several generators on a rotating schedule.
Takes a flat series of [time, generator] pairs. Emits from the
first generator for
time seconds, then from the second
generator for time seconds, and so on, restarting from the
first generator after all generators were used. State of every
generator is preserved from cycle to cycle. The iterator stops
when one of the generators is exhausted.
Parameters:
- ...
a flat series of [time, generator] pairs.
Returns:
an iterator
Usage:
> gen.each(print, gen.cycle_times(
> 0.1, gen.duplicate('a'),
> 0.1, gen.duplicate('b'))
> )
a
a
a
b
b
...
-
mix (...)
-
A random mixture of a number generators. Takes a collection of
generators and chooses between them uniformly.
Parameters:
Returns:
an iterator
Usage:
> molly.gen.range(1, 5):mix(molly.gen.range(5, 10)):totable()
- - 1
- 5
- 2
- 3
- 6
- 7
- 4
- 8
- 9
- 5
- 10
-
flip_flop (a, b)
-
Emits an operation from generator A, then B, then A again,
then B again, etc. Stops as soon as any generator is exhausted.
Parameters:
- a
a generator.
- b
a generator.
Returns:
an iterator
Usage:
> gen.each(print, gen.flip_flop(
> gen.range(1, 2), gen.duplicate('x')))
1
x
2
x
-
log ()
-
A generator which, when asked for an operation, logs
a message and yields
nil. Occurs only once; use repeat to
repeat.
Returns:
an iterator
Usage:
> molly.gen.iter({1, 2, 3}):log()
[INFO 2024-07-07 11:38:47:119933]: 1
- null
...
-
stagger (dt)
-
(TODO) Operations from that generator are scheduled at
uniformly random intervals
between
0 to 2 * (dt seconds).
Parameters:
- dt
number
Number of seconds.
Returns:
an iterator
-
time_limit (duration)
-
Stops generating items when time limit is exceeded.
Parameters:
- duration
number
Number of seconds.
Returns:
an iterator
Usage:
> for _it, v in gen.time_limit(gen.range(1, 100), 0.0001) do
> print(v)
> end
1
2
3
4
5
6
7
8
9
10
11
12
...