NAME
Data::Maker - Simple, flexibile and extensible generation of realistic data
SYNOPSIS
An extremely basic example:
use Data::Maker;
my $maker = Data::Maker->new(
record_count => 10_000,
fields => [
{ name => 'phone', format => '(\d\d\d)\d\d\d-\d\d\d\d' }
]
);
while (my $record = $maker->next_record) {
print $record->phone->value . "\n";
}
A more complete example:
use Data::Maker;
use Data::Maker::Field::Person::LastName;
use Data::Maker::Field::Person::FirstName;
my $maker = Data::Maker->new(
record_count => 10_000,
delimiter => "\t",
fields => [
{
name => 'lastname',
class => 'Data::Maker::Field::Person::LastName'
},
{
name => 'firstname',
class => 'Data::Maker::Field::Person::FirstName'
},
{
name => 'phone',
class => 'Data::Maker::Field::Format',
args => {
format => '(\d\d\d)\d\d\d-\d\d\d\d',
}
},
]
);
while (my $record = $maker->next_record) {
print $record->delimited . "\n";
}
DESCRIPTION
Overview
Whatever kind of test or demonstration data you need, Data::Maker will help you make lots of it.
And if you happen to need one of the various types of data that is available as predefined field types, it will be even easier.
Performance
Data::Maker was not specifically designed for performance, though obviously performance is a consideration.
My latest benchmarking has generally been around 200 records per second, for a fairly typical assortment of fields, but obviously this varies with different types of fields and certainly with different quantities of fields.
I think it's a good idea to benchmark each field type. I added most of them to a benchmarking script that creates a certain number of records (in this case 250) with one field at a time, and then that same number of records with all of the fields in it. Obviously the time required to generate an entire record increases with each field that is added.
Here are those results:
Data::Maker::Field::Format 1851.74 records/s
Data::Maker::Field::Person::FirstName 1522.83 records/s
Data::Maker::Field::Person::LastName 1520.74 records/s
Data::Maker::Field::Code 2232.42 records/s
Data::Maker::Field::Person::Gender 2045.09 records/s
Data::Maker::Field::DateTime 389.43 records/s
Data::Maker::Field::Lorem 2054.06 records/s
Data::Maker::Record (with all of the above fields) 203.73 records/s
These benchmarks were run on a 2.66 GHz Intel Core 2 Duo MacBook Pro with 4 GB of memory. In the future I will benchmark additional hardware and put that information in another document.
Related Modules
I recently heard about Data::Faker, which seems to have had similar goals. I had not heard of Data::Faker when I first published Data::Maker and, at the time of this writing, Data::Faker has not been updated in four and a half years.
CONSTRUCTOR
- new PARAMS
-
Returns a new Data::Maker object. Any PARAMS passed to the constructor will be set as properties of the object.
CLASS METHODS
- random LIST
-
Just makes a quick random selection from a list of choices. It's just like calling Perl's `rand()` without having to mention the list more than once, and just like using Data::Maker::Field::Set with less syntax (and it works where using a Field subclass would not be appropriate). A good use of this (and the reason it was written) is when you want a random number of records created. You can set record_count to generate a random number of records between 3 and 19 with this code:
$maker->record_count( Data::Maker->random(3..19) );
OBJECT METHODS
- BUILD
-
The BUILD method is a Moose thing. It is run immediately after the object is created. Currently used in Data::Maker only to seed the randomness, if a seed was provided.
- field_by_name NAME
-
Given the name of a field, this method returns the Field object
- next_record
-
This method not only gets the next record, but it also triggers the generation of the data itself.
- new_or_cached CLASS, FIELD
-
This method is not used yet, though I keep hoping the object_cache() code above (in next_record ) will call this method instead of having the code there. But it is really only used once in this form, so I'm perhaps being too picky.
- in_progress NAME
-
This method is used to get the already-generated value of a field in the list, before the entire record has been created and blessed as a Record object. This was created for, and is mostly useful for, fields that depend upon the values of other fields. For example, the Data::Maker::Field::Person::Gender class uses this, so that the gender of the person will match the first name of the person.
- header
-
Prints out a delimited list of all of the labels, only if a delimiter was provided to the Data::Maker object
- add_field HASHREF
-
Takes a hashref that describes a field attribute and adds it to the field list
ATTRIBUTES
The following Moose attributes are used (the data type of each attribute is also listed):
- fields (ArrayRef[HashRef])
-
A list of hashrefs, each of which describes one field to be generated. Each field needs to define the subclass of Data::Maker::Field that is used to generate that field. The order of the fields has some relevance, particularly in the context of Data::Maker::Record. For example, the delimited method returns the fields in the order in which they are listed here.
Note: It may make more sense in the future for each field to have a "sequence" attribute, so methods such as delimited would return then in a different order than that in which they are generated. The order in which fields are generated matters in the event that one field relies on data from another (for example, the Data::Maker::Field::Person::Gender field class relies on a first name that must have already been generated).
Data::Maker::Field::Code - Use a code reference to generate the data. This is useful for generating a value for a field that is based on the value of another field.
Data::Maker::Field::DateTime - Generates a random DateTime, using DateTime::Event::Random.
Data::Maker::Field::File - Provide your own file of seed data.
Data::Maker::Field::Format - Specify a format for the data to follow. The follow regexp-inspired atoms are supported:
\d: Digit \w: Word character \W: Word character, with all letters uppercase \l: Letter \L: Uppercase letter \x: hex character (00, f2, 97, b4, etc) \X: Uppercase hex character (00, F2, 97, B4, etc)
Data::Maker::Field::Person::FirstName - A built-in field class for generating (mostly Anglo) first (given) names.
Data::Maker::Field::Person::MiddleName - A built-in field class for generating middle initials (I realize it's called MiddleName). It should eventually be able to generate middle names or initials.
Data::Maker::Field::Person::LastName - A built-in field class for generating (mostly Anglo) surnames.
Data::Maker::Field::Person::Gender - Given a field that represents a given name, this class uses Text::GenderFromName to guess the gender (currently returning only "M" or "F"). If it is not able to guess the gender, it returns "U" (unknown).
Data::Maker::Field::Set - A build-in field class for selecting a random member of a given set.
Data::Maker::Field::Lorem - A build-in field class for generating random Latin-looking text, using Text::Lorem.
- record_count (Num)
-
The number of records desired
- object_cache (HashRef)
-
Used internally by Data::Maker to ensure reuse of the field objects for each row. This is important because certain objects have large data sets inside them.
- data_sources (HashRef)
-
Used internally by Data::Maker. It's a hashref to store open file handles.
- record_counts (HashRef)
-
A hashref of record counts. Not sure why this was used. It's mentioned in Data::Maker::Field::File
- delimiter
-
The optional delimiter... could be anything. Usually a comma, tab, pipe, etc
- generated (Num)
-
Returns the number of records that have been generated so far.
- seed (Num)
-
The optional random seed. Provide a seed to ensure that the randomly-generated data comes out the same each time you run it. This is actually super-cool when you need this kind of thing.
CONTRIBUTORS
Thanks to my employer, Informatics Corporation of America, for its commitment to Perl and to giving back to the Perl community. Thanks to Mark Frost for the idea about optionally seeding the randomness to ensure the same output each time a program is run, if that's what you want to do (turns out this is very useful).
AUTHOR
John Ingram (john@funnycow.com)
LICENSE
Copyright 2010 by John Ingram. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.