private void WriteDataToFile(string filePath, int entitySize, long collectionSize) { var fileSize = entitySize * collectionSize; int currentPercent = 0; using (var fs = new FileStream(filePath, FileMode.CreateNew)) using (var mmf = MemoryMappedFile.CreateFromFile(fs, "Events", fileSize, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false)) { var batchCount = Math.Ceiling((double)collectionSize / BATCH_SIZE); for (int b = 0; b < batchCount; b++) { var currentBatchSize = Math.Min(BATCH_SIZE, collectionSize - b * BATCH_SIZE); using (var accessor = mmf.CreateViewAccessor(b * BATCH_SIZE * entitySize, currentBatchSize * entitySize)) { for (long i = 0; i < currentBatchSize; i++) { var pEVent = GetNextEvent(); accessor.Write(i * entitySize, ref pEVent); var tempPercent = (int)(100 * (i + b * BATCH_SIZE) / collectionSize); if (currentPercent != tempPercent) { currentPercent = tempPercent; EventGenerateProgressChanged?.Invoke(currentPercent); } } } } } }
public async Task <IDataProvider> GenerateDataProviderAsync(long collectionSize, object[] parameters) { if (collectionSize % 2 != 0) { _logger.Error("Requested size for generation is not even."); throw new ArgumentException("Collection size should be even number", nameof(collectionSize)); } try { _logger.Info($"Generate new ArrayDataProvider:{collectionSize}"); PayloadEvent[] eventArray = null; int currentPercent = 0; await Task.Run(() => { eventArray = new PayloadEvent[collectionSize]; for (int i = 0; i < collectionSize; i++) { var newEvent = GetNextEvent(); eventArray[i] = newEvent; var percent = (int)(100 * i / collectionSize); if (currentPercent != percent) { currentPercent = percent; EventGenerateProgressChanged?.Invoke(percent); } } }).ConfigureAwait(false); _logger.Info($"ArrayDataProvider successfully generated"); return(new ArrayDataProvider(eventArray)); } catch (Exception e) { _logger.Error(e); throw; } }