Avoid random data in benchmarking, as it can result in performance differences. If you must have random data, make sure to use a seed value.
You very rarely need to create a custom generator class. In almost all usages, you will use for-comprehensions on generators to create custom data values for the benchmark. See documentation link below.
Generators in ScalaMeter are different than those in ScalaCheck. They do not just generate some random values. They generate a well defined set of values that are fed to the benchmark. Usually, these values follow a certain pattern, such as the size of the data. For example, if you are benchmarking List
operations, a generator would typically generate lists of different sizes.
You don't say how to create a User
, but only say that you do it randomly.
So, let's assume that there is a function newUser
that takes an integer, and uses it to create a User
:
def newUser(seed: Int): User
If there is any concept of a size of the User
object, you can use the seed to impact that size. For example, if your User
object has a name
field, you can generate a name field of the size seed
. This is particularly useful if the size impacts the running time of the operations, as you will later see that dependency on a plot.
The generator of the User
objects has the type Gen[User]
. We create it by starting from a seed generator:
val seeds = Gen.range("seed")(0, 10, 1)
This generator contains seed integers from 0
until 10
. We use them to create a user generator:
val users: Gen[User] = for (seed <- seeds) yield newUser(seed)
This is discussed in the documentation:
http://scalameter.github.io/home/gettingstarted/0.7/
Section on generators:
http://scalameter.github.io/home/gettingstarted/0.7/generators/index.html
Parameters are summarized in the ScalaDoc
, but you don't really need them -- instead, use generator comprehensions as the tutorial shows,