Exemplo n.º 1
0
 public virtual IList <T> GenerateList(int count)
 {
     using (StopwatchLoggerFactory.LogDispose(
                LogHelper.DefaultLogger,
                LogLevel.Trace,
                nameof(FakerDataGenerator <T>),
                "GenerateList completed, Count = {count}",
                count))
     {
         return(Faker.Generate(count));
     }
 }
Exemplo n.º 2
0
        public async Task <AllBatchesResult> GenerateAll(
            GeneratorConfig config,
            CancellationToken ct = default)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            try
            {
                config.Validate();
            }
            catch (ValidationException ve)
            {
                _logger.Fatal(ve, "Cannot start generation, Config is invalid");
                throw;
            }

            config.ServicePointSettings?.ApplySettings();

            var batches = config.GetAllBatches(out var unqiueCount).ToList();

            var results = new List <AllGenerationsResult>(batches.Count);

            using (StopwatchLoggerFactory.ForceLogStartDispose(
                       _logger,
                       "Start generation all settings for all batches, " +
                       "Lunches count {count}, Config = {@config}",
                       "Generation for all batches completed",
                       Params.ToArray <object>(batches.Count, config)))
            {
                foreach (var batch in batches)
                {
                    results.Add(await Generate(config, batch, ct));
                }
            }
            return(new AllBatchesResult(results));
        }
Exemplo n.º 3
0
        protected async Task <AllGenerationsResult> Generate(
            GeneratorConfig config,
            BatchSettings batchSettings,
            CancellationToken ct = default)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }
            if (batchSettings == null)
            {
                throw new ArgumentNullException(nameof(batchSettings));
            }

            try
            {
                config.Validate();
                batchSettings.Validate();
            }
            catch (ValidationException ve)
            {
                _logger.Fatal(ve, "Cannot start generation, configuration is invalid");
                throw;
            }

            if (batchSettings.RestartIisBeforeBatch)
            {
                try
                {
                    using (StopwatchLoggerFactory.ForceLogDispose(_logger, LogLevel.Debug, "iisreset completed"))
                    {
                        IisManager.Instance.RestartIis();
                    }
                }
                catch (Exception e)
                {
                    _logger.Error(e, "Cannot restart IIS. Perhaps application was batched without administrator rights");
                }
            }

            if (batchSettings.CollectGarbageBeforeBatch)
            {
                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug("Memory before GC.Collect: {byte:N0}", GC.GetTotalMemory(false));
                    GC.Collect();
                    _logger.Debug("Memory after GC.Collect: {byte:N0}", GC.GetTotalMemory(true));
                }
                else
                {
                    GC.Collect();
                }
            }

            var generatationResults = new List <GenerationResult>();
            var batch = batchSettings.CompileBatch();

            bool stopProcessing = false;

            using (StopwatchLoggerFactory.ForceLogStartDispose(
                       _logger,
                       "Start generation all settings for batch, Settings count = {count}, Id = {id}",
                       "Generation all settings for batch completed, Settings count = {count}, Id = {id}",
                       Params.ToArray <object>(batch.AvailableCount, batchSettings.Id, batchSettings),
                       callback: time => LogHelper.ResultsLogger.Info("Batch, Time: {time} ({time-sec} sec)", time, time.TotalSeconds)))
            {
                foreach (var settings in batch)
                {
                    GenerationResult result;
                    try
                    {
                        ct.ThrowIfCancellationRequested();
                        if (settings is IBatchDependent bd)
                        {
                            bd.Inject(batch);
                        }

                        var runner = settings.GetGenerationRunner(config.ApiConnectionConfig);
                        config.SubscriptionManager?.SubscribeGenerationRunner(runner);
                        await runner.RunGeneration(ct).ConfigureAwait(false);

                        result = new GenerationResult(settings);
                    }
                    catch (OperationCanceledException oce)
                    {
                        _logger.Fatal(oce, "Operation was canceled");
                        throw;
                    }
                    catch (GenerationException ge)
                    {
                        _logger.Error(ge, "Generation failed");
                        result = new GenerationResult(settings, ge);
                        if (batchSettings.StopProcessingAtException)
                        {
                            stopProcessing = true;
                        }
                    }
                    catch (ValidationException ve)
                    {
                        _logger.Error(ve, "Generation not started because of invalid configuration");
                        result = new GenerationResult(settings, ve);
                    }
                    // this should not happen
                    catch (Exception e)
                    {
                        _logger.Fatal(e, "Generation failed with unexpected error");
                        result = new GenerationResult(settings, e);
                        if (batchSettings.StopProcessingAtException)
                        {
                            stopProcessing = true;
                        }
                    }
                    generatationResults.Add(result);
                    if (stopProcessing)
                    {
                        break;
                    }
                }
            }
            return(new AllGenerationsResult(generatationResults, stopProcessing));
        }