X

Generating data in dotnet core with GenFu

dotnet-core-with-GenFu

dotnet core with GenFu

Couple of weeks ago I had to test out the performance of a third party product but in order to test we had to use a lot of logs that would feed into the system. The requirements was to pump out around 1000 logs per seconds and feed into the system.

As you know I am a .NET fan wanted to write this small program in dotnet core C# and was looking for a library that would generate me some fake data but something realistic at the same time. I found GenFu developed by James and Dave, 2 fellow Canadian Microsoft MVP.

The description of GenFu is provided below:

GenFu is a library you can use to generate realistic test data. It is composed of several property fillers that can populate commonly named properties through reflection using an internal database of values or randomly created data. You can override any of the fillers, give GenFu hints on how to fill them.

Installation of GenFu

GenFu comes in NuGet package thus the installation process is as simple as

Install-Package GenFu

Usage

My usage was quite simple to generate log files. Thus I started by creating an Object that would contain all my data.

class LogActivity
{
        public Guid UserId { get; set;}
        public int TenantId { get; set; }
        public string TenantUsed { get; set;}        
        public Guid Serial { get; set;}
        public int Action { get; set;}
        public int Result { get; set;}
        public string SourceIp { get; set;}
        public int AgentId { get; set;}
        public string Message { get; set;}
        public DateTime ActDate { get; set;}
        public string SiteHost { get; set;}
        public string UsedName { get; set;}
        public string CredentialType { get; set;}
}

GenFu provides an easy way to fill the data like below

var singleLog = A.New();
//or generate a list of logs
var thousandLogs = A.ListOf(1000);

But my requirements had certain criteria and GenFu allows you to use Fillers (PropertyFiller) to fill in data to your needs i.e a way to override its default values. In my case I wanted to override multiple fields, an example below of override of TenantName

//First call FillerManager
A.Default().FillerManager.RegisterFiller(new TenantNameFiller());

//the class is TenantNameFiller
public class TenantNameFiller: PropertyFiller
{
         protected static Random _random = new Random(Environment.TickCount);
        private string[] Company = new string[] {"AGT", "KIO", "GGR", "Smith", "Orion","GMC", "Ford", "Hundai", "Pacific Atlantic", "Mother's", "Frank's", "Berkshire Hathaway", "Pepsi", "Coca-cola"};
        //second value is the property name to fill
        public TenantNameFiller(): base(new[] {"object"}, new [] {"TenantName", "TenantNameUsed"})
        {            
        }

        public override object GetValue(object instance)
        {
            //random value from array
            return Company[_random.Next(0, Company.Length)];
        }
}

With the code above and the setup I was able to generate data that was very realistic to my needs. GenFu also provides Fill to add Ranges to your data or Email addresss like below:

A.Configure().            
            Fill(x => x.Result).WithinRange(0, 8).
            Fill(x => x.AgentId).WithinRange(1, 23).
            Fill(x => x.UsedName).AsEmailAddress().
            Fill(x => x.ActDate, () => { return DateTime.Now;});

var activities = A.ListOf(1000);

Thanks to GenFu I was able to create some interesting and realistic data to feed into the system. Do check out the library 🙂

Categories: .NET
Taswar Bhatti:
Related Post