private void AllocateObjects(IMethodLogger logger, long numberOfTopLevelDummyObjects) { var startMemory = GC.GetTotalMemory(false); var list = new List<DummyObject>(); for (long l = 0; l < numberOfTopLevelDummyObjects; l++) { list.Add(new DummyObject()); } logger.WriteMethodInfo( string.Format( "Total Number of Objects [{0:n0}]. Max Depth of Object Graph [{1:n0}]", list.Sum(d => d.CalculateTotalNumberOfObjects()), list.Max(d => d.CalculateMaxDepth()))); logger.WriteMethodInfo(""); logger.WriteMethodInfo( string.Format( "Approx difference in Bytes of Managed Heap after allocation " + "[{0:n0}] bytes.", (GC.GetTotalMemory(false) - startMemory))); logger.WriteMethodInfo(""); }
public void Execute(IMethodLogger logger, List<SamplerMethodParameter> parameters) { var startMemory = GC.GetTotalMemory(false); GC.Collect(0, GCCollectionMode.Forced,true); GC.Collect(1, GCCollectionMode.Forced, true); GC.Collect(2, GCCollectionMode.Forced, true); logger.WriteMethodInfo(""); logger.WriteMethodInfo( string.Format( "Approx difference in Bytes of Managed Heap after GC (should be negative) " + "[{0:n0}] bytes.", (GC.GetTotalMemory(false) - startMemory))); logger.WriteMethodInfo(""); }
private void AllocateObjects(IMethodLogger logger, long numberOfFloats) { var startMemory = GC.GetTotalMemory(false); var array = new float[numberOfFloats]; for (long l = 0; l < numberOfFloats; l++) { array[l] = 1; } logger.WriteMethodInfo(""); logger.WriteMethodInfo( string.Format( "Approx difference in Bytes of Managed Heap after allocation. (0 is expected)" + "[{0:n0}] bytes.", (GC.GetTotalMemory(false) - startMemory))); logger.WriteMethodInfo(""); }
private async Task WriteAndReadToDiskAsync( IMethodLogger logger, int numberOfCharactersPerRow, long numberOfRows) { var tempFile = new TempFileCreator().CreateTempFile(); logger.WriteMethodInfo("Temp File: [" + tempFile + "]"); var random = new Random(DateTime.Now.Second); // ReSharper disable once ReturnValueOfPureMethodIsNotUsed random.Next(); var stopwatch = Stopwatch.StartNew(); try { using (var fs = File.OpenWrite(tempFile)) using (var sw = new StreamWriter(fs)) { for (long r = 0; r < numberOfRows; r++) { var row = new StringBuilder(numberOfCharactersPerRow); for (var i = 0; i < numberOfCharactersPerRow; i++) { row.Append(char.ConvertFromUtf32(random.Next(48, 122))); } await sw.WriteLineAsync(row.ToString()); } } } catch (Exception e) { throw new Exception( "Exception writing to temp file [" + tempFile + "]. " + "Try running with Administrator permissions: " + e.Message + Environment.NewLine + e.StackTrace, e); } logger.WriteMethodInfo("Wrote file in [" + stopwatch.ElapsedMilliseconds + "] milliseconds."); try { using (var fs = File.OpenRead(tempFile)) using (var sr = new StreamReader(fs)) { var allText = await sr.ReadToEndAsync(); if (string.IsNullOrEmpty(allText)) throw new Exception( "This exception is here to ensure the compiler does not 'optimize' away" + "the sr.ReadToEnd() call. This exception should never be hit."); } } catch (Exception e) { throw new Exception( "Exception reading from temp file [" + tempFile + "]. " + "Try running with Administrator permissions: " + e.Message + Environment.NewLine + e.StackTrace, e); } try { File.Delete(tempFile); } catch (Exception e) { throw new Exception( "Exception deleting temp file [" + tempFile + "]. " + "Try running with Administrator permissions: " + e.Message + Environment.NewLine + e.StackTrace, e); } }
private void RequestRiverBedHomePageAsync( IMethodLogger logger, int numberOfRequestsToMake) { var requestTimes = new ConcurrentBag<long>(); int htmlLength = 0; try { var tasks = Enumerable.Range(0, numberOfRequestsToMake) .Select(async x => { var requestStopWatch = Stopwatch.StartNew(); var webClient = new WebClient(); using (var stream = await webClient.OpenReadTaskAsync(new Uri("http://www.riverbed.com"))) using (var sr = new StreamReader(stream)) { var html = await sr.ReadToEndAsync(); htmlLength = html.Length; } requestTimes.Add(requestStopWatch.ElapsedMilliseconds); }); Task.WaitAll(tasks.ToArray()); } catch (Exception e) { throw new Exception("Error getting http://www.riverbed.com: " + e.Message + Environment.NewLine + e.StackTrace); } logger.WriteMethodInfo( string.Format("Html Length [{0:n0}] chars. Request Times: Min [{1:n0}] Avg [{2:n0}] Max [{3:n0}]", htmlLength, requestTimes.Min(), requestTimes.Average(), requestTimes.Max())); logger.WriteMethodInfo(""); }
private void SendTcpTraffic( IMethodLogger logger, string address, int port, string message, int messageInflationCount, int numberOfTimes, int simulatedPacketDelayInMilliseconds) { string serverResponse = ""; var requestTimes = new List<long>(numberOfTimes); var sb = new StringBuilder(message, message.Length * messageInflationCount); for (var i = 1; i < messageInflationCount; i++) { sb.Append(message); } message = sb.ToString(); for (var i = 0; i < numberOfTimes; i++) { System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient(); var requestStopWatch = Stopwatch.StartNew(); try { client.Connect(address, port); } catch (Exception e) { throw new Exception( string.Format( "Failed to connect Tcp Client to [{0}:{1}]: {2} {3} {4}", address, port, e.Message, Environment.NewLine, e.StackTrace)); } try { var data = Encoding.UTF8.GetBytes(message); using (var stream = client.GetStream()) { stream.Write(data, 0, data.Length); stream.Flush(); var allData = new List<byte>(); var receiveBuffer = new byte[1024]; while (true) { var chunkLength = stream.Read(receiveBuffer, 0, receiveBuffer.Length); if (chunkLength < receiveBuffer.Length) { //end of message allData.AddRange(receiveBuffer.Take(chunkLength)); break; } allData.AddRange(receiveBuffer); if (simulatedPacketDelayInMilliseconds > 0) Thread.Sleep(simulatedPacketDelayInMilliseconds); } serverResponse = Encoding.UTF8.GetString(allData.ToArray(), 0, allData.Count); } } catch (Exception e) { throw new Exception("Exception talking with server: " + e.Message + Environment.NewLine + e.StackTrace); } var checkMessage = message.Length > 100 ? message.Substring(0, 100) : message; if (!serverResponse.Contains(checkMessage)) throw new Exception( "Server returned junk. Expected it to echo message but it did not." + "Expected it to contain [" + checkMessage + "]. Response: [" + serverResponse + "]"); requestTimes.Add(requestStopWatch.ElapsedMilliseconds); } logger.WriteMethodInfo(""); logger.WriteMethodInfo( string.Format("Server Response [{0}]",serverResponse)); logger.WriteMethodInfo( string.Format("Request Times: Min [{0:n0}] Avg [{1:n0}] Max [{2:n0}]", requestTimes.Min(), requestTimes.Average(), requestTimes.Max())); }
private void RequestRiverBedHomePageMultiThreaded( IMethodLogger logger, int numberOfRequestsToMake, int numberOfThreads) { var requestTimes = new ConcurrentBag<long>(); int htmlLength = 0; try { Parallel.For(0, numberOfRequestsToMake, new ParallelOptions { MaxDegreeOfParallelism = numberOfThreads }, i => { var requestStopWatch = Stopwatch.StartNew(); var webClient = new WebClient(); using (var stream = webClient.OpenRead(new Uri("http://www.riverbed.com"))) // ReSharper disable once AssignNullToNotNullAttribute -- will handle in parent catch using (var sr = new StreamReader(stream)) { var html = sr.ReadToEnd(); htmlLength = html.Length; } requestTimes.Add(requestStopWatch.ElapsedMilliseconds); }); } catch (Exception e) { throw new Exception("Error getting http://www.riverbed.com: " + e.Message + Environment.NewLine + e.StackTrace); } logger.WriteMethodInfo( string.Format("Html Length [{0:n0}] chars. Request Times: Min [{1:n0}] Avg [{2:n0}] Max [{3:n0}]", htmlLength, requestTimes.Min(), requestTimes.Average(), requestTimes.Max())); logger.WriteMethodInfo(""); }
private void SumNumbersInArray(IMethodLogger logger, long numberOfTimesToCount, int numberOfThreadsToUse) { Parallel.For(0, numberOfTimesToCount, new ParallelOptions { MaxDegreeOfParallelism = numberOfThreadsToUse }, l => { var sum = _array.Sum(); if (sum < 0) throw new Exception( "This exception is here to ensure the compiler did not 'optimize' away the call to _array.Sum(). It should never be hit."); }); logger.WriteMethodInfo( string.Format("Sum is [{0:n0}]", _array.Sum())); }
private void SumNumbersInArray(IMethodLogger logger, long numberOfTimesToCount) { for (long l = 0; l < numberOfTimesToCount; l++) { var sum = _array.Sum(); if (sum < 0) throw new Exception( "This exception is here to ensure the compiler did not 'optimize' " + "away the call to _array.Sum(). It should never be hit."); } logger.WriteMethodInfo( string.Format("Sum is [{0:n0}]", _array.Sum())); }
private void RequestRiverBedHomePage(IMethodLogger logger, int numberOfRequestsToMake) { var requestTimes = new List<long>(numberOfRequestsToMake); int htmlLength = 0; try { for (var i = 0; i < numberOfRequestsToMake; i++) { var requestStopWatch = Stopwatch.StartNew(); var webClient = new WebClient(); using (var stream = webClient.OpenRead("http://www.riverbed.com")) // ReSharper disable once AssignNullToNotNullAttribute -- will handle in catch clause using (var sr = new StreamReader(stream)) { var html = sr.ReadToEnd(); htmlLength = html.Length; } requestTimes.Add(requestStopWatch.ElapsedMilliseconds); } } catch (Exception e) { throw new Exception("Error getting http://www.riverbed.com: " + e.Message + Environment.NewLine + e.StackTrace); } logger.WriteMethodInfo( string.Format("Html Length [{0:n0}] chars. Request Times: Min [{1:n0}] Avg [{2:n0}] Max [{3:n0}]", htmlLength, requestTimes.Min(), requestTimes.Average(), requestTimes.Max())); }