private char[] GetNextLine(Common.Random random, long capacity, ref List <char[]> duplicates) { char[] chars = null; if (duplicates.Count > 0 && random.Next(100) < duplicatesProbabilityPercents) { //get duplicate line chars = duplicates[random.Next(duplicates.Count)]; var remainingBytes = capacity - chars.Length; if (remainingBytes <= MinLineLength) { chars = null; } } // need to generate new line if (null == chars) { // generate new line var rnd = random.Next(MinLineLength, maxLineLength); var length = Math.Min(rnd, capacity); var remainingBytes = capacity - length; if (remainingBytes <= MinLineLength) { length += remainingBytes; } chars = GenerateLine(random, (int)length); // store duplicates to future use if (duplicatePatternsCount > 0) { lock (duplicates) { --duplicatePatternsCount; duplicates.Add(chars); } } } return(chars); }
private static char[] GenerateLine(Common.Random random, int length) { var buffer = new char[length]; var dotIndex = random.Next(1, Math.Min(length - MinLineLength + 1, MaxDigitsConunt)); for (var i = 0; i < buffer.Length - 2; i++) { if (i == dotIndex || i == dotIndex + 1) { continue; } buffer[i] = chars[ i < dotIndex ? random.Next(i == 0 ? 1 : 0, 10) : random.Next(chars.Length)]; } buffer[dotIndex] = '.'; buffer[dotIndex + 1] = ' '; buffer[buffer.Length - 2] = '\r'; buffer[buffer.Length - 1] = '\n'; return(buffer); }
protected override void OnExecute() { Console.WriteLine("========================================================="); Console.WriteLine("System.Security.Cryptography.RNGCryptoServiceProvider takes much more time but ensures pure random!"); Console.WriteLine("To increase performance please provide 'UseSystemRandom = true' in command but it can cause unpredictable duplicates!!!"); // avarage duplicates probabilty. var duplicates = new List <char[]>(); var totalLinesCount = 0; file = LargeFile.Create(fileName, fileSize); var parts = file.Write().ToArray(); if (parts.Length > 1) { Console.WriteLine($"File generation is devided onto {parts.Length} parts."); } var random = new Common.Random(useSystemRandom); for (var i = 0; i < parts.Length; ++i) { if (parts.Length > 1) { Console.WriteLine($"Generating Part({i})..."); } var progress = new Progress(); parts[i].Process((stream, block, cancel) => { var bytesWritten = 0; while (!cancel.IsCancellationRequested && bytesWritten < stream.Length) { var chars = GetNextLine(random, stream.Length - bytesWritten, ref duplicates); // use ASCII encoding for simplification var buffer = Encoding.ASCII.GetBytes(chars); stream.Write(buffer, 0, buffer.Length); bytesWritten += buffer.Length; progress.Report((double)bytesWritten / stream.Length); Interlocked.Increment(ref totalLinesCount); } if (block.Id == 0) { Console.WriteLine("Flushing streams, please wait..."); } }); } Console.WriteLine($"Total lines count: {totalLinesCount}"); if (file.IsCancelled) { // delete file due to process was cacelled if (File.Exists(fileName)) { File.Delete(fileName); Console.WriteLine("Process terminated"); Console.WriteLine("File system has been cleaned."); Console.WriteLine("Press any key..."); Console.ReadLine(); } } }