Skip to content

Commit

Permalink
Exposed locale parameter for locale-dependent generators
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriyIvon committed Feb 28, 2024
1 parent cf2efed commit 1863869
Show file tree
Hide file tree
Showing 43 changed files with 201 additions and 258 deletions.
3 changes: 0 additions & 3 deletions src/DatabaseBenchmark/Core/RandomValueProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace DatabaseBenchmark.Core
{
public class RandomValueProvider : IRandomValueProvider
{
//TODO: what to do with the faker
private readonly Faker _faker = new();
private readonly Dictionary<(ValueRandomizationRule, bool), IGenerator> _generators = [];

private readonly IGeneratorFactory _generatorFactory;
Expand Down Expand Up @@ -74,7 +72,6 @@ private object GetValue(string tableName, string columnName, ValueRandomizationR
if (collection)
{
generator = new CollectionGenerator(
_faker,
generator,
new CollectionGeneratorOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public GeneratorDataSource(string filePath, IDataSourceFactory dataSourceFactory
var optionsDirectory = Path.GetDirectoryName(Path.GetFullPath(filePath));
Directory.SetCurrentDirectory(optionsDirectory);

//var faker = !string.IsNullOrEmpty(_options.Locale) ? new Faker(_options.Locale) : new Faker();

var generatorFactory = new GeneratorFactory(/*faker, */dataSourceFactory, database);
var generatorFactory = new GeneratorFactory(dataSourceFactory, database);
_generators = _options.Columns
.Select(c => generatorFactory.Create(c.GeneratorOptions))
.ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
{
public class GeneratorDataSourceOptions
{
public string Locale { get; set; }

public GeneratorDataSourceColumn[] Columns { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/DatabaseBenchmark/Databases/Common/RandomPrimitives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace DatabaseBenchmark.Databases.Common
{
public class RandomPrimitives : IRandomPrimitives
{
private readonly Faker _faker = new();
private readonly Randomizer _randomizer = new();

public bool GetRandomBoolean() => _faker.Random.Bool();
public bool GetRandomBoolean() => _randomizer.Bool();
}
}
34 changes: 17 additions & 17 deletions src/DatabaseBenchmark/Generators/AddressGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Bogus;
using Bogus.DataSets;
using DatabaseBenchmark.Common;
using DatabaseBenchmark.Generators.Interfaces;
using DatabaseBenchmark.Generators.Options;
Expand All @@ -8,34 +8,34 @@ namespace DatabaseBenchmark.Generators
{
public class AddressGenerator : IGenerator
{
private readonly Faker _faker;
private readonly Address _addressFaker;
private readonly AddressGeneratorOptions _options;

public object Current { get; private set; }

public AddressGenerator(Faker faker, AddressGeneratorOptions options)
public AddressGenerator(AddressGeneratorOptions options)
{
_faker = faker;
_options = options;
_addressFaker = string.IsNullOrEmpty(options.Locale) ? new Address() : new Address(locale: options.Locale);
}

public bool Next()
{
Current = _options.Kind switch
{
GeneratorKind.BuildingNumber => _faker.Address.BuildingNumber(),
GeneratorKind.City => _faker.Address.City(),
GeneratorKind.Country => _faker.Address.Country(),
GeneratorKind.CountryCode => _faker.Address.CountryCode(),
GeneratorKind.County => _faker.Address.County(),
GeneratorKind.FullAddress => _faker.Address.FullAddress(),
GeneratorKind.Latitude => _faker.Address.Latitude(),
GeneratorKind.Longitude => _faker.Address.Longitude(),
GeneratorKind.SecondaryAddress => _faker.Address.SecondaryAddress(),
GeneratorKind.State => _faker.Address.State(),
GeneratorKind.StreetAddress => _faker.Address.StreetAddress(),
GeneratorKind.StreetName => _faker.Address.StreetName(),
GeneratorKind.ZipCode => _faker.Address.ZipCode(),
GeneratorKind.BuildingNumber => _addressFaker.BuildingNumber(),
GeneratorKind.City => _addressFaker.City(),
GeneratorKind.Country => _addressFaker.Country(),
GeneratorKind.CountryCode => _addressFaker.CountryCode(),
GeneratorKind.County => _addressFaker.County(),
GeneratorKind.FullAddress => _addressFaker.FullAddress(),
GeneratorKind.Latitude => _addressFaker.Latitude(),
GeneratorKind.Longitude => _addressFaker.Longitude(),
GeneratorKind.SecondaryAddress => _addressFaker.SecondaryAddress(),
GeneratorKind.State => _addressFaker.State(),
GeneratorKind.StreetAddress => _addressFaker.StreetAddress(),
GeneratorKind.StreetName => _addressFaker.StreetName(),
GeneratorKind.ZipCode => _addressFaker.ZipCode(),
_ => throw new InputArgumentException($"Unknown address generator kind \"{_options.Kind}\"")
};

Expand Down
9 changes: 4 additions & 5 deletions src/DatabaseBenchmark/Generators/BooleanGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ namespace DatabaseBenchmark.Generators
{
public class BooleanGenerator : IGenerator
{
private readonly Faker _faker;
private readonly Randomizer _randomizer = new();
private readonly BooleanGeneratorOptions _options;

public object Current { get; private set; }

public BooleanGenerator(Faker faker, BooleanGeneratorOptions options)
public BooleanGenerator(BooleanGeneratorOptions options)
{
_faker = faker;
_options = options;
}

public bool Next()
{
Current = _options.Weight != null
? _faker.Random.Bool(_options.Weight.Value)
: _faker.Random.Bool();
? _randomizer.Bool(_options.Weight.Value)
: _randomizer.Bool();

return true;
}
Expand Down
6 changes: 2 additions & 4 deletions src/DatabaseBenchmark/Generators/CollectionGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,25 @@ namespace DatabaseBenchmark.Generators
{
public class CollectionGenerator : IGenerator
{
private readonly Faker _faker;
private readonly Randomizer _randomizer = new();
private readonly CollectionGeneratorOptions _options;
private readonly IGenerator _sourceGenerator;
private readonly ICollectionGenerator _sourceCollectionGenerator;

public object Current { get; private set; }

public CollectionGenerator(
Faker faker,
IGenerator sourceGenerator,
CollectionGeneratorOptions options)
{
_faker = faker;
_options = options;
_sourceGenerator = sourceGenerator;
_sourceCollectionGenerator = sourceGenerator as ICollectionGenerator;
}

public bool Next()
{
var length = _faker.Random.Int(_options.MinLength, _options.MaxLength);
var length = _randomizer.Int(_options.MinLength, _options.MaxLength);

if (_sourceCollectionGenerator != null)
{
Expand Down
9 changes: 3 additions & 6 deletions src/DatabaseBenchmark/Generators/ColumnItemGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Bogus;
using DatabaseBenchmark.Databases.Common.Interfaces;
using DatabaseBenchmark.Databases.Common.Interfaces;
using DatabaseBenchmark.Generators.Interfaces;
using DatabaseBenchmark.Generators.Options;
using DatabaseBenchmark.Model;
Expand All @@ -8,7 +7,6 @@ namespace DatabaseBenchmark.Generators
{
public class ColumnItemGenerator : IGenerator, ICollectionGenerator
{
private readonly Faker _faker;
private readonly ColumnItemGeneratorOptions _options;
private readonly IDatabase _database;

Expand All @@ -18,9 +16,8 @@ public class ColumnItemGenerator : IGenerator, ICollectionGenerator

public IEnumerable<object> CurrentCollection => _itemGenerator.CurrentCollection;

public ColumnItemGenerator(Faker faker, ColumnItemGeneratorOptions options, IDatabase database)
public ColumnItemGenerator(ColumnItemGeneratorOptions options, IDatabase database)
{
_faker = faker;
_options = options;
_database = database;
}
Expand Down Expand Up @@ -53,7 +50,7 @@ private void Initialize()
WeightedItems = _options.WeightedItems
};

_itemGenerator = new ListItemGenerator(_faker, listItemGeneratorOptions);
_itemGenerator = new ListItemGenerator(listItemGeneratorOptions);
}

//TODO: Make shared between two generators
Expand Down
12 changes: 6 additions & 6 deletions src/DatabaseBenchmark/Generators/CompanyGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Bogus;
using Bogus.DataSets;
using DatabaseBenchmark.Common;
using DatabaseBenchmark.Generators.Interfaces;
using DatabaseBenchmark.Generators.Options;
Expand All @@ -8,23 +8,23 @@ namespace DatabaseBenchmark.Generators
{
public class CompanyGenerator : IGenerator
{
private readonly Faker _faker;
private readonly Company _companyFaker;
private readonly CompanyGeneratorOptions _options;

public object Current { get; private set; }

public CompanyGenerator(Faker faker, CompanyGeneratorOptions options)
public CompanyGenerator(CompanyGeneratorOptions options)
{
_faker = faker;
_options = options;
_companyFaker = string.IsNullOrEmpty(options.Locale) ? new Company() : new Company(locale: _options.Locale);
}

public bool Next()
{
Current = _options.Kind switch
{
GeneratorKind.CompanySuffix => _faker.Company.CompanySuffix(),
GeneratorKind.CompanyName => _faker.Company.CompanyName(),
GeneratorKind.CompanySuffix => _companyFaker.CompanySuffix(),
GeneratorKind.CompanyName => _companyFaker.CompanyName(),
_ => throw new InputArgumentException($"Unknown company generator kind \"{_options.Kind}\"")
};

Expand Down
13 changes: 7 additions & 6 deletions src/DatabaseBenchmark/Generators/DateTimeGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bogus;
using Bogus.DataSets;
using DatabaseBenchmark.Common;
using DatabaseBenchmark.Generators.Interfaces;
using DatabaseBenchmark.Generators.Options;
Expand All @@ -7,16 +8,16 @@ namespace DatabaseBenchmark.Generators
{
public class DateTimeGenerator : IGenerator
{
private readonly Faker _faker;
private readonly Randomizer _randomizer = new();
private readonly Date _dateFaker = new();
private readonly DateTimeGeneratorOptions _options;

private DateTime? _lastValue;

public object Current { get; private set; }

public DateTimeGenerator(Faker faker, DateTimeGeneratorOptions options)
public DateTimeGenerator(DateTimeGeneratorOptions options)
{
_faker = faker;
_options = options;
}

Expand All @@ -42,7 +43,7 @@ public bool Next()
if (_options.RandomizeDelta)
{
var milliseconds = _options.Delta.TotalMilliseconds;
var randomMilliseconds = _faker.Random.Long(1, (long)milliseconds);
var randomMilliseconds = _randomizer.Long(1, (long)milliseconds);
delta = TimeSpan.FromMilliseconds(randomMilliseconds);
}

Expand All @@ -67,15 +68,15 @@ public bool Next()
var deltaMilliseconds = _options.Delta.TotalMilliseconds;
var rangeMilliseconds = (_options.MaxValue - _options.MinValue).TotalMilliseconds;
var totalSegments = (long)(rangeMilliseconds / deltaMilliseconds);
var randomSegment = _faker.Random.Long(0, totalSegments);
var randomSegment = _randomizer.Long(0, totalSegments);

Current = _options.MinValue.AddMilliseconds(deltaMilliseconds * randomSegment);

return true;
}
else
{
Current = _faker.Date.Between(_options.MinValue, _options.MaxValue);
Current = _dateFaker.Between(_options.MinValue, _options.MaxValue);

return true;
}
Expand Down
21 changes: 10 additions & 11 deletions src/DatabaseBenchmark/Generators/FinanceGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Bogus;
using Bogus.DataSets;
using DatabaseBenchmark.Common;
using DatabaseBenchmark.Generators.Interfaces;
using DatabaseBenchmark.Generators.Options;
Expand All @@ -8,28 +8,27 @@ namespace DatabaseBenchmark.Generators
{
public class FinanceGenerator : IGenerator
{
private readonly Faker _faker;
private readonly Finance _financeFaker = new();
private readonly FinanceGeneratorOptions _options;

public object Current { get; private set; }

public FinanceGenerator(Faker faker, FinanceGeneratorOptions options)
public FinanceGenerator(FinanceGeneratorOptions options)
{
_faker = faker;
_options = options;
}

public bool Next()
{
Current = _options.Kind switch
{
GeneratorKind.Bic => _faker.Finance.Bic(),
GeneratorKind.Iban => _faker.Finance.Iban(),
GeneratorKind.CreditCardCvv => _faker.Finance.CreditCardCvv(),
GeneratorKind.CreditCardNumber => _faker.Finance.CreditCardNumber(),
GeneratorKind.Currency => _faker.Finance.Currency(),
GeneratorKind.BitcoinAddress => _faker.Finance.BitcoinAddress(),
GeneratorKind.EthereumAddress => _faker.Finance.EthereumAddress(),
GeneratorKind.Bic => _financeFaker.Bic(),
GeneratorKind.Iban => _financeFaker.Iban(),
GeneratorKind.CreditCardCvv => _financeFaker.CreditCardCvv(),
GeneratorKind.CreditCardNumber => _financeFaker.CreditCardNumber(),
GeneratorKind.Currency => _financeFaker.Currency(),
GeneratorKind.BitcoinAddress => _financeFaker.BitcoinAddress(),
GeneratorKind.EthereumAddress => _financeFaker.EthereumAddress(),
_ => throw new InputArgumentException($"Unknown finance generator kind \"{_options.Kind}\"")
};

Expand Down
11 changes: 5 additions & 6 deletions src/DatabaseBenchmark/Generators/FloatGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ namespace DatabaseBenchmark.Generators
{
public class FloatGenerator : IGenerator
{
private readonly Faker _faker;
private readonly Randomizer _randomizer = new();
private readonly FloatGeneratorOptions _options;

private double? _lastValue;

public object Current { get; private set; }

public FloatGenerator(Faker faker, FloatGeneratorOptions options)
public FloatGenerator(FloatGeneratorOptions options)
{
_faker = faker;
_options = options;
}

Expand All @@ -41,7 +40,7 @@ public bool Next()

if (_options.RandomizeDelta)
{
delta = _faker.Random.Double(0, delta); //assuming the result will never be equal to zero
delta = _randomizer.Double(0, delta); //assuming the result will never be equal to zero
}

var isAscending = _options.Direction == Direction.Ascending;
Expand All @@ -63,15 +62,15 @@ public bool Next()
else if (_options.Delta != 0)
{
var totalSegments = (int)((_options.MaxValue - _options.MinValue) / _options.Delta);
var randomSegment = _faker.Random.Int(0, totalSegments);
var randomSegment = _randomizer.Int(0, totalSegments);

Current = _options.MinValue + (_options.Delta * randomSegment);

return true;
}
else
{
Current = _faker.Random.Double(_options.MinValue, _options.MaxValue);
Current = _randomizer.Double(_options.MinValue, _options.MaxValue);

return true;
}
Expand Down
Loading

0 comments on commit 1863869

Please sign in to comment.