public static IEnumerable <SomeEvent> GenerateEvents(SeederConfig config, int seriesId) { var eventIntervalSec = 1.0d / config.EventFrequencyHz; var eventTime = config.SeriesStartDate; var endTime = config.SeriesStartDate.AddMonths(config.SeriesLengthMonths); do { var ev = new SomeEvent() { SeriesId = seriesId, Time = eventTime, Value = 1.0f, }; eventTime = eventTime.AddSeconds(eventIntervalSec); yield return(ev); } while (eventTime < endTime); }
static void Main(string[] args) { Console.WriteLine("Database seed test"); var conf = ReadConfigfuration(); var pgConf = new PostgresStorageConfig(conf); var seedSettings = new SeederConfig(conf); Console.WriteLine("Seed settings:" + seedSettings); Console.WriteLine("Y to create schema"); var createSchema = Console.ReadLine().ToUpper() == "Y"; var connFactory = new ConnectionFactory(pgConf); if (createSchema) { Console.WriteLine("Creating db"); var createUtil = new DatabaseCreationUtil(connFactory); createUtil.CreateDatabase(true); } Console.WriteLine("Batch generating"); var eventBatches = EventDataGenerator.BatchGenerateEventsChronological(seedSettings); var batchCount = eventBatches.Count(); Console.WriteLine("Seeding events"); var mainSeedTimer = Stopwatch.StartNew(); var opt = new ParallelOptions() { MaxDegreeOfParallelism = seedSettings.MaxParallelThreads }; Parallel.ForEach(eventBatches, opt, (evts, loopState, batchNo) => { var batchSw = Stopwatch.StartNew(); var repo = new EventRepo(connFactory); repo.Insert(evts, CancellationToken.None).GetAwaiter().GetResult(); batchSw.Stop(); Console.WriteLine($"Wrote batch {batchNo}/{batchCount}. Batch took {batchSw.Elapsed:ss\\.ffffff} sec"); }); mainSeedTimer.Stop(); Console.WriteLine($"Finished in {mainSeedTimer.Elapsed}. Press enter"); Console.ReadLine(); }
public static IEnumerable <SomeEvent> GenerateEventsChronological(SeederConfig config) { var eventIntervalSec = 1.0d / config.EventFrequencyHz; var eventTime = config.SeriesStartDate; var endTime = config.SeriesStartDate.AddMonths(config.SeriesLengthMonths); var startId = config.InitialSeriesId; var nSeries = config.NbrSeriesToSeed; do { for (var i = startId; i < startId + nSeries; i++) { var ev = new SomeEvent() { SeriesId = i, Time = eventTime, Value = 1.0f, }; yield return(ev); } eventTime = eventTime.AddSeconds(eventIntervalSec); } while (eventTime < endTime); }
public static IEnumerable <IEnumerable <SomeEvent> > BatchGenerateEventsChronological(SeederConfig config) { var eventsSource = GenerateEventsChronological(config); return(eventsSource.Batch(config.WriteBatchSize)); }