/// <summary> /// Yields an Enumerable list of paths to GZTestData files /// </summary> public static IEnumerable<object[]> CompressedFiles() { if (_compressedFiles == null) { PerfUtils utils = new PerfUtils(); _compressedFiles = new List<object[]>(); // Crypto random data byte[] bytes = new byte[100000000]; var rand = RandomNumberGenerator.Create(); rand.GetBytes(bytes); string filePath = utils.GetTestFilePath() + ".gz"; using (FileStream output = File.Create(filePath)) using (GZipStream zip = new GZipStream(output, CompressionMode.Compress)) zip.Write(bytes, 0, bytes.Length); _compressedFiles.Add(new object[] { filePath }); // Create a compressed file with repeated segments bytes = Text.Encoding.UTF8.GetBytes(utils.CreateString(100000)); filePath = utils.GetTestFilePath() + ".gz"; using (FileStream output = File.Create(filePath)) using (GZipStream zip = new GZipStream(output, CompressionMode.Compress)) for (int i = 0; i < 1000; i++) zip.Write(bytes, 0, bytes.Length); _compressedFiles.Add(new object[] { filePath }); } return _compressedFiles; }
public void Compress_Canterbury(int innerIterations, string fileName, CompressionLevel compressLevel) { byte[] bytes = File.ReadAllBytes(Path.Combine("GZTestData", "Canterbury", fileName)); PerfUtils utils = new PerfUtils(); FileStream[] filestreams = new FileStream[innerIterations]; GZipStream[] gzips = new GZipStream[innerIterations]; string[] paths = new string[innerIterations]; foreach (var iteration in Benchmark.Iterations) { for (int i = 0; i < innerIterations; i++) { paths[i] = utils.GetTestFilePath(); filestreams[i] = File.Create(paths[i]); } using (iteration.StartMeasurement()) for (int i = 0; i < innerIterations; i++) { gzips[i] = new GZipStream(filestreams[i], compressLevel); gzips[i].Write(bytes, 0, bytes.Length); gzips[i].Flush(); gzips[i].Dispose(); filestreams[i].Dispose(); } for (int i = 0; i < innerIterations; i++) File.Delete(paths[i]); } }
public void ExpandEnvironmentVariables() { PerfUtils utils = new PerfUtils(); string env = utils.CreateString(15); string inputEnv = "%" + env + "%"; try { // setup the environment variable so we can read it Environment.SetEnvironmentVariable(env, "value"); // read the valid environment variable foreach (var iteration in Benchmark.Iterations) using (iteration.StartMeasurement()) for (int i = 0; i < 40000; i++) { Environment.ExpandEnvironmentVariables(inputEnv); Environment.ExpandEnvironmentVariables(inputEnv); Environment.ExpandEnvironmentVariables(inputEnv); Environment.ExpandEnvironmentVariables(inputEnv); Environment.ExpandEnvironmentVariables(inputEnv); Environment.ExpandEnvironmentVariables(inputEnv); Environment.ExpandEnvironmentVariables(inputEnv); Environment.ExpandEnvironmentVariables(inputEnv); Environment.ExpandEnvironmentVariables(inputEnv); Environment.ExpandEnvironmentVariables(inputEnv); } } finally { // clear the variable that we set Environment.SetEnvironmentVariable(env, null); } }
// Creates byte arrays that contain random data to be compressed public static IEnumerable<object[]> ByteArraysToCompress() { if (_byteArraysToCompress == null) { PerfUtils utils = new PerfUtils(); _byteArraysToCompress = new List<object[]>(); // Regular, semi well formed data _byteArraysToCompress.Add(new object[] { Text.Encoding.UTF8.GetBytes(utils.CreateString(100000000)) }); // Crypto random data { byte[] bytes = new byte[100000000]; var rand = RandomNumberGenerator.Create(); rand.GetBytes(bytes); _byteArraysToCompress.Add(new object[] { bytes }); } // Highly repeated data { byte[] bytes = new byte[101000000]; byte[] small = Text.Encoding.UTF8.GetBytes(utils.CreateString(100000)); for (int i = 0; i < 1000; i++) small.CopyTo(bytes, 100000 * i); _byteArraysToCompress.Add(new object[] { bytes }); } } return _byteArraysToCompress; }
/// <summary> /// Creates a list containing a number of elements equal to the specified size /// </summary> public static List<object> CreateList(PerfUtils utils, int size) { List<object> list = new List<object>(); for (int i = 0; i < size; i++) list.Add(utils.CreateString(100)); return list; }
public static Hashtable CreateHashtable(int size) { Hashtable ht = new Hashtable(); PerfUtils utils = new PerfUtils(); for (int i = 0; i < size; i++) ht.Add(utils.CreateString(50), utils.CreateString(50)); return ht; }
public void GetChars(int size) { PerfUtils utils = new PerfUtils(); string testString = utils.CreateString(size); foreach (var iteration in Benchmark.Iterations) using (iteration.StartMeasurement()) for (int i = 0; i < 10000; i++) testString.ToCharArray(); }
public void Concat_str_str(int size) { PerfUtils utils = new PerfUtils(); string testString1 = utils.CreateString(size); string testString2 = utils.CreateString(size); foreach (var iteration in Benchmark.Iterations) using (iteration.StartMeasurement()) for (int i = 0; i < 10000; i++) string.Concat(testString1, testString2); }
public void Contains(int size) { PerfUtils utils = new PerfUtils(); string testString = utils.CreateString(size); string subString = testString.Substring(testString.Length / 2, testString.Length / 4); foreach (var iteration in Benchmark.Iterations) using (iteration.StartMeasurement()) for (int i = 0; i < 10000; i++) testString.Contains(subString); }
/// <summary> /// Yields several Lists containing increasing amounts of strings /// can be used as MemberData input to performance tests for Dictionary /// </summary> /// <remarks>Any changes made to the returned collections MUST be undone. Collections /// used as MemberData are cached and reused in other perf tests. /// </remarks> public static List<object[]> TestData() { if (_testData == null) { PerfUtils utils = new PerfUtils(); _testData = new List<object[]>(); _testData.Add(new object[] { CreateList(utils, 1000) }); _testData.Add(new object[] { CreateList(utils, 10000) }); _testData.Add(new object[] { CreateList(utils, 100000) }); } return _testData; }
/// <summary> /// Creates a Dictionary of string-string with the specified number of pairs /// </summary> public static Dictionary<string, string> CreateDictionary(PerfUtils utils, int size) { Dictionary<string, string> dict = new Dictionary<string, string>(); while (dict.Count < size) { string key = utils.CreateString(50); while (dict.ContainsKey(key)) key = utils.CreateString(50); dict.Add(key, utils.CreateString(50)); } return dict; }
public void GetDirectoryName() { PerfUtils utils = new PerfUtils(); string testPath = utils.GetTestFilePath(); foreach (var iteration in Benchmark.Iterations) using (iteration.StartMeasurement()) for (int i = 0; i < 20000; i++) { Path.GetDirectoryName(testPath); Path.GetDirectoryName(testPath); Path.GetDirectoryName(testPath); Path.GetDirectoryName(testPath); Path.GetDirectoryName(testPath); Path.GetDirectoryName(testPath); Path.GetDirectoryName(testPath); Path.GetDirectoryName(testPath); Path.GetDirectoryName(testPath); } }
public void GetBytes_str(int size) { Encoding enc = Encoding.UTF8; PerfUtils utils = new PerfUtils(); string toEncode = utils.CreateString(size); foreach (var iteration in Benchmark.Iterations) using (iteration.StartMeasurement()) for (int i = 0; i < 100; i++) { enc.GetBytes(toEncode); enc.GetBytes(toEncode); enc.GetBytes(toEncode); enc.GetBytes(toEncode); enc.GetBytes(toEncode); enc.GetBytes(toEncode); enc.GetBytes(toEncode); enc.GetBytes(toEncode); enc.GetBytes(toEncode); } }
public void ctor_string(int length) { PerfUtils utils = new PerfUtils(); string input = utils.CreateString(length); StringBuilder builder; foreach (var iteration in Benchmark.Iterations) using (iteration.StartMeasurement()) for (int i = 0; i < 10000; i++) { builder = new StringBuilder(input); builder = new StringBuilder(input); builder = new StringBuilder(input); builder = new StringBuilder(input); builder = new StringBuilder(input); builder = new StringBuilder(input); builder = new StringBuilder(input); builder = new StringBuilder(input); builder = new StringBuilder(input); } }
public void GetChars(int size, string encName) { const int innerIterations = 100; Encoding enc = Encoding.GetEncoding(encName); PerfUtils utils = new PerfUtils(); byte[] bytes = enc.GetBytes(utils.CreateString(size)); foreach (var iteration in Benchmark.Iterations) using (iteration.StartMeasurement()) for (int i = 0; i < innerIterations; i++) { enc.GetChars(bytes); enc.GetChars(bytes); enc.GetChars(bytes); enc.GetChars(bytes); enc.GetChars(bytes); enc.GetChars(bytes); enc.GetChars(bytes); enc.GetChars(bytes); enc.GetChars(bytes); } }
public void Append(int length) { PerfUtils utils = new PerfUtils(); foreach (var iteration in Benchmark.Iterations) { // Setup - Create a string of the specified length string builtString = utils.CreateString(length); StringBuilder empty = new StringBuilder(); // Actual perf testing using (iteration.StartMeasurement()) for (int i = 0; i < 10000; i++) empty.Append(builtString); // Appends a string of length "length" to an increasingly large StringBuilder } }
public void Combine(int innerIterations) { PerfUtils utils = new PerfUtils(); string testPath1 = utils.GetTestFilePath(); string testPath2 = utils.CreateString(10); foreach (var iteration in Benchmark.Iterations) using (iteration.StartMeasurement()) for (int i = 0; i < innerIterations; i++) { Path.Combine(testPath1, testPath2); Path.Combine(testPath1, testPath2); Path.Combine(testPath1, testPath2); Path.Combine(testPath1, testPath2); Path.Combine(testPath1, testPath2); Path.Combine(testPath1, testPath2); Path.Combine(testPath1, testPath2); Path.Combine(testPath1, testPath2); Path.Combine(testPath1, testPath2); } }
public void GetItem(Hashtable table) { // Setup - utils needs a specific seed to prevent key collision with TestData object result; PerfUtils utils = new PerfUtils(983452); string key = utils.CreateString(50); table.Add(key, "value"); foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) { for (int i = 0; i < 40000; i++) { result = table[key]; result = table[key]; result = table[key]; result = table[key]; result = table[key]; result = table[key]; result = table[key]; result = table[key]; result = table[key]; result = table[key]; result = table[key]; result = table[key]; result = table[key]; result = table[key]; result = table[key]; result = table[key]; } } } table.Remove(key); }
/// <returns></returns> private static string CreateCompressedFile(CompressionType type) { const int fileSize = 100000000; PerfUtils utils = new PerfUtils(); string filePath = utils.GetTestFilePath() + ".gz"; switch (type) { case CompressionType.CryptoRandom: using (RandomNumberGenerator rand = RandomNumberGenerator.Create()) { byte[] bytes = new byte[fileSize]; rand.GetBytes(bytes); using (FileStream output = File.Create(filePath)) using (GZipStream zip = new GZipStream(output, CompressionMode.Compress)) zip.Write(bytes, 0, bytes.Length); } break; case CompressionType.RepeatedSegments: { byte[] bytes = new byte[fileSize / 1000]; new Random(128453).NextBytes(bytes); using (FileStream output = File.Create(filePath)) using (GZipStream zip = new GZipStream(output, CompressionMode.Compress)) for (int i = 0; i < 1000; i++) zip.Write(bytes, 0, bytes.Length); } break; case CompressionType.NormalData: { byte[] bytes = new byte[fileSize]; new Random(128453).NextBytes(bytes); using (FileStream output = File.Create(filePath)) using (GZipStream zip = new GZipStream(output, CompressionMode.Compress)) zip.Write(bytes, 0, bytes.Length); } break; } return filePath; }
private void Flood(CommandProcessorContext context, string eventStreamId, int clientsCnt, int minPerSecond, int maxPerSecond, int runTimeMinutes) { context.IsAsync(); var clients = new List <TcpTypedConnection <byte[]> >(); var threads = new List <Thread>(); var doneEvent = new ManualResetEvent(false); var done = false; var succ = 0; var fail = 0; var requestsCnt = 0; int sent = 0; int received = 0; var watchLockRoot = new object(); var sw = Stopwatch.StartNew(); for (int i = 0; i < clientsCnt; i++) { var esId = eventStreamId ?? "Stream-" + Thread.CurrentThread.ManagedThreadId % 3; var client = context.Client.CreateTcpConnection( context, (conn, pkg) => { if (pkg.Command != TcpCommand.WriteEventsCompleted) { context.Fail(reason: string.Format("Unexpected TCP package: {0}.", pkg.Command)); return; } var dto = pkg.Data.Deserialize <TcpClientMessageDto.WriteEventsCompleted>(); if (dto.Result == TcpClientMessageDto.OperationResult.Success) { var succDone = Interlocked.Increment(ref succ); if (succDone % maxPerSecond == 0) { Console.Write("."); } Interlocked.Increment(ref requestsCnt); } else { Interlocked.Increment(ref fail); } Interlocked.Increment(ref received); }, connectionClosed: (conn, err) => { if (!done) { context.Fail(reason: "Socket was closed, but not all requests were completed."); } else { context.Success(); } }); clients.Add(client); threads.Add(new Thread(() => { var sentCount = 0; var sleepTime = 0; var dataSizeCoefficient = 1; var currentMinute = -1; while (true) { TimeSpan elapsed; lock (watchLockRoot) elapsed = sw.Elapsed; if (elapsed.TotalMinutes > runTimeMinutes) { done = true; doneEvent.Set(); break; } if (sentCount == 0) { int elapsedMinutesInt = (int)elapsed.TotalMinutes; lock (_randomLockRoot) { sentCount = minPerSecond == maxPerSecond ? maxPerSecond : _random.Next(minPerSecond, maxPerSecond); dataSizeCoefficient = _random.Next(8, 256); } if (currentMinute != elapsedMinutesInt) { currentMinute = elapsedMinutesInt; context.Log.Info("\nElapsed {0} of {1} minutes, sent {2}; next block coef. {3}", elapsedMinutesInt, runTimeMinutes, sent, dataSizeCoefficient); } sleepTime = 1000 / sentCount; } var dataSize = dataSizeCoefficient * 8; var write = new TcpClientMessageDto.WriteEvents( esId, ExpectedVersion.Any, new[] { new TcpClientMessageDto.NewEvent( Guid.NewGuid().ToByteArray(), "TakeSomeSpaceEvent", 0, 0, Helper.UTF8NoBom.GetBytes("DATA" + dataSize.ToString(" 00000 ") + new string('*', dataSize)), Helper.UTF8NoBom.GetBytes("METADATA" + new string('$', 100))) }, false); var package = new TcpPackage(TcpCommand.WriteEvents, Guid.NewGuid(), write.Serialize()); client.EnqueueSend(package.AsByteArray()); Interlocked.Increment(ref sent); Thread.Sleep(sleepTime); sentCount -= 1; while (sent - received > context.Client.Options.WriteWindow / clientsCnt) { Thread.Sleep(1); } } })); } foreach (var thread in threads) { thread.IsBackground = true; thread.Start(); } doneEvent.WaitOne(); sw.Stop(); foreach (var client in clients) { client.Close(); } context.Log.Info("Completed. Successes: {0}, failures: {1}", succ, fail); var reqPerSec = (requestsCnt + 0.0) / sw.ElapsedMilliseconds * 1000; context.Log.Info("{0} requests completed in {1}ms ({2:0.00} reqs per sec).", requestsCnt, sw.ElapsedMilliseconds, reqPerSec); PerfUtils.LogData( Keyword, PerfUtils.Row(PerfUtils.Col("clientsCnt", clientsCnt), PerfUtils.Col("requestsCnt", requestsCnt), PerfUtils.Col("ElapsedMilliseconds", sw.ElapsedMilliseconds)), PerfUtils.Row(PerfUtils.Col("successes", succ), PerfUtils.Col("failures", fail)) ); PerfUtils.LogTeamCityGraphData(string.Format("{0}-{1}-{2}-reqPerSec", Keyword, clientsCnt, requestsCnt), (int)reqPerSec); PerfUtils.LogTeamCityGraphData( string.Format("{0}-{1}-{2}-failureSuccessRate", Keyword, clientsCnt, requestsCnt), 100 * fail / (fail + succ)); context.Success(); }
private void PingFlood(CommandProcessorContext context, int clientsCnt, int requestsCnt) { context.IsAsync(); var threads = new List <Thread>(); var autoResetEvent = new AutoResetEvent(false); var all = 0; for (int i = 0; i < clientsCnt; i++) { var count = requestsCnt / clientsCnt + ((i == clientsCnt - 1) ? requestsCnt % clientsCnt : 0); int sent = 0; int received = 0; threads.Add(new Thread(() => { var client = new HttpAsyncClient(); var url = context.Client.HttpEndpoint.ToHttpUrl("/ping"); Action <HttpResponse> onsuccess = response => { Interlocked.Increment(ref received); var pongs = Interlocked.Increment(ref all); if (pongs % 1000 == 0) { Console.Write('.'); } if (pongs == requestsCnt) { autoResetEvent.Set(); } }; Action <Exception> onException = e => { context.Log.ErrorException(e, "Error during GET"); var pongs = Interlocked.Increment(ref all); if (pongs == requestsCnt) { autoResetEvent.Set(); } }; for (int j = 0; j < count; ++j) { client.Get(url, onsuccess, onException); Interlocked.Increment(ref sent); while (sent - received > context.Client.Options.PingWindow) { Thread.Sleep(1); } } })); } var sw = Stopwatch.StartNew(); foreach (var thread in threads) { thread.IsBackground = true; thread.Start(); } autoResetEvent.WaitOne(); sw.Stop(); var reqPerSec = (requestsCnt + 0.0) / sw.ElapsedMilliseconds * 1000; context.Log.Info("{0} requests completed in {1}ms ({2:0.00} reqs per sec).", requestsCnt, sw.ElapsedMilliseconds, reqPerSec); PerfUtils.LogData( Keyword, PerfUtils.Row(PerfUtils.Col("clientsCnt", clientsCnt), PerfUtils.Col("requestsCnt", requestsCnt), PerfUtils.Col("ElapsedMilliseconds", sw.ElapsedMilliseconds)) ); PerfUtils.LogTeamCityGraphData(string.Format("{0}-{1}-{2}-reqPerSec", Keyword, clientsCnt, requestsCnt), (int)reqPerSec); context.Success(); }
public void GetByteCount(int size, string encName) { const int innerIterations = 100; Encoding enc = Encoding.GetEncoding(encName); PerfUtils utils = new PerfUtils(); char[] chars = utils.CreateString(size).ToCharArray(); foreach (var iteration in Benchmark.Iterations) using (iteration.StartMeasurement()) for (int i = 0; i < innerIterations; i++) { enc.GetByteCount(chars); enc.GetByteCount(chars); enc.GetByteCount(chars); enc.GetByteCount(chars); enc.GetByteCount(chars); enc.GetByteCount(chars); enc.GetByteCount(chars); enc.GetByteCount(chars); enc.GetByteCount(chars); } }
public bool Execute(CommandProcessorContext context, string[] args) { var eventStreamId = "test-stream"; var expectedVersion = ExpectedVersion.Any; if (args.Length > 0) { if (args.Length > 2) { return(false); } eventStreamId = args[0]; if (args.Length == 2) { expectedVersion = args[1].Trim().ToUpper() == "ANY" ? ExpectedVersion.Any : int.Parse(args[1]); } } context.IsAsync(); var corrid = Guid.NewGuid(); var deleteDto = new ClientMessageDto.DeleteStream(corrid, eventStreamId, expectedVersion); var package = new TcpPackage(TcpCommand.DeleteStream, corrid, deleteDto.Serialize()); var sw = new Stopwatch(); var done = false; context.Client.CreateTcpConnection( context, connectionEstablished: conn => { context.Log.Info("[{0}]: Trying to delete event stream '{1}'...", conn.EffectiveEndPoint, eventStreamId); sw.Start(); conn.EnqueueSend(package.AsByteArray()); }, handlePackage: (conn, pkg) => { sw.Stop(); if (pkg.Command != TcpCommand.DeleteStreamCompleted) { context.Fail(reason: string.Format("Unexpected TCP package: {0}.", pkg.Command)); return; } var dto = pkg.Data.Deserialize <ClientMessageDto.DeleteStreamCompleted>(); if (dto.ErrorCode == (int)OperationErrorCode.Success) { context.Log.Info("DELETED event stream {0}.", eventStreamId); } else { context.Log.Info("DELETION FAILED for event stream {0}: {1} ({2}).", eventStreamId, dto.Error, (OperationErrorCode)dto.ErrorCode); } context.Log.Info("Delete request took: {0}.", sw.Elapsed); PerfUtils.LogTeamCityGraphData(string.Format("{0}-latency-ms", Keyword), (int)sw.ElapsedMilliseconds); done = true; conn.Close(); context.Success(); }, connectionClosed: (connection, error) => { if (done && error == SocketError.Success) { context.Success(); } else { context.Fail(); } }); context.WaitForCompletion(); return(true); }
private void WriteFlood(CommandProcessorContext context, int clientsCnt, long requestsCnt, int streamsCnt, int size) { context.IsAsync(); var doneEvent = new ManualResetEventSlim(false); var clients = new List <TcpTypedConnection <byte[]> >(); var threads = new List <Thread>(); long succ = 0; long fail = 0; long prepTimeout = 0; long commitTimeout = 0; long forwardTimeout = 0; long wrongExpVersion = 0; long streamDeleted = 0; long all = 0; var streams = Enumerable.Range(0, streamsCnt).Select(x => Guid.NewGuid().ToString()).ToArray(); //var streams = Enumerable.Range(0, streamsCnt).Select(x => string.Format("stream-{0}", x)).ToArray(); var sw2 = new Stopwatch(); for (int i = 0; i < clientsCnt; i++) { var count = requestsCnt / clientsCnt + ((i == clientsCnt - 1) ? requestsCnt % clientsCnt : 0); long sent = 0; long received = 0; var rnd = new Random(); var client = context.Client.CreateTcpConnection( context, (conn, pkg) => { if (pkg.Command != TcpCommand.WriteEventsCompleted) { context.Fail(reason: string.Format("Unexpected TCP package: {0}.", pkg.Command)); return; } var dto = pkg.Data.Deserialize <TcpClientMessageDto.WriteEventsCompleted>(); switch (dto.Result) { case TcpClientMessageDto.OperationResult.Success: if (Interlocked.Increment(ref succ) % 1000 == 0) { Console.Write('.'); } break; case TcpClientMessageDto.OperationResult.PrepareTimeout: Interlocked.Increment(ref prepTimeout); break; case TcpClientMessageDto.OperationResult.CommitTimeout: Interlocked.Increment(ref commitTimeout); break; case TcpClientMessageDto.OperationResult.ForwardTimeout: Interlocked.Increment(ref forwardTimeout); break; case TcpClientMessageDto.OperationResult.WrongExpectedVersion: Interlocked.Increment(ref wrongExpVersion); break; case TcpClientMessageDto.OperationResult.StreamDeleted: Interlocked.Increment(ref streamDeleted); break; default: throw new ArgumentOutOfRangeException(); } if (dto.Result != TcpClientMessageDto.OperationResult.Success) { if (Interlocked.Increment(ref fail) % 1000 == 0) { Console.Write('#'); } } Interlocked.Increment(ref received); var localAll = Interlocked.Increment(ref all); if (localAll % 100000 == 0) { var elapsed = sw2.Elapsed; sw2.Restart(); context.Log.Trace("\nDONE TOTAL {0} WRITES IN {1} ({2:0.0}/s) [S:{3}, F:{4} (WEV:{5}, P:{6}, C:{7}, F:{8}, D:{9})].", localAll, elapsed, 1000.0 * 100000 / elapsed.TotalMilliseconds, succ, fail, wrongExpVersion, prepTimeout, commitTimeout, forwardTimeout, streamDeleted); } if (localAll == requestsCnt) { context.Success(); doneEvent.Set(); } }, connectionClosed: (conn, err) => context.Fail(reason: "Connection was closed prematurely.")); clients.Add(client); threads.Add(new Thread(() => { for (int j = 0; j < count; ++j) { var write = new TcpClientMessageDto.WriteEvents( streams[rnd.Next(streamsCnt)], ExpectedVersion.Any, new[] { new TcpClientMessageDto.NewEvent(Guid.NewGuid().ToByteArray(), "TakeSomeSpaceEvent", 1, 0, Common.Utils.Helper.UTF8NoBom.GetBytes("{ \"DATA\" : \"" + new string('*', size) + "\"}"), Common.Utils.Helper.UTF8NoBom.GetBytes("{ \"METADATA\" : \"" + new string('$', 100) + "\"}")) }, false); var package = new TcpPackage(TcpCommand.WriteEvents, Guid.NewGuid(), write.Serialize()); client.EnqueueSend(package.AsByteArray()); var localSent = Interlocked.Increment(ref sent); while (localSent - Interlocked.Read(ref received) > context.Client.Options.WriteWindow / clientsCnt) { Thread.Sleep(1); } } }) { IsBackground = true }); } var sw = Stopwatch.StartNew(); sw2.Start(); threads.ForEach(thread => thread.Start()); doneEvent.Wait(); sw.Stop(); clients.ForEach(client => client.Close()); context.Log.Info("Completed. Successes: {0}, failures: {1} (WRONG VERSION: {2}, P: {3}, C: {4}, F: {5}, D: {6})", succ, fail, wrongExpVersion, prepTimeout, commitTimeout, forwardTimeout, streamDeleted); var reqPerSec = (all + 0.0) / sw.ElapsedMilliseconds * 1000; context.Log.Info("{0} requests completed in {1}ms ({2:0.00} reqs per sec).", all, sw.ElapsedMilliseconds, reqPerSec); PerfUtils.LogData( Keyword, PerfUtils.Row(PerfUtils.Col("clientsCnt", clientsCnt), PerfUtils.Col("requestsCnt", requestsCnt), PerfUtils.Col("ElapsedMilliseconds", sw.ElapsedMilliseconds)), PerfUtils.Row(PerfUtils.Col("successes", succ), PerfUtils.Col("failures", fail))); var failuresRate = (int)(100 * fail / (fail + succ)); PerfUtils.LogTeamCityGraphData(string.Format("{0}-{1}-{2}-reqPerSec", Keyword, clientsCnt, requestsCnt), (int)reqPerSec); PerfUtils.LogTeamCityGraphData(string.Format("{0}-{1}-{2}-failureSuccessRate", Keyword, clientsCnt, requestsCnt), failuresRate); PerfUtils.LogTeamCityGraphData(string.Format("{0}-c{1}-r{2}-st{3}-s{4}-reqPerSec", Keyword, clientsCnt, requestsCnt, streamsCnt, size), (int)reqPerSec); PerfUtils.LogTeamCityGraphData(string.Format("{0}-c{1}-r{2}-st{3}-s{4}-failureSuccessRate", Keyword, clientsCnt, requestsCnt, streamsCnt, size), failuresRate); if (Interlocked.Read(ref succ) != requestsCnt) { context.Fail(reason: "There were errors or not all requests completed."); } else { context.Success(); } }
public void Trim_WithWhitespace(int size) { PerfUtils utils = new PerfUtils(); string testString = " " + utils.CreateString(size) + " "; foreach (var iteration in Benchmark.Iterations) using (iteration.StartMeasurement()) for (int i = 0; i < 10000; i++) testString.Trim(); }
public void Substring_int_int(int size) { PerfUtils utils = new PerfUtils(); int startIndex = size / 2; int length = size / 4; string testString = utils.CreateString(size); foreach (var iteration in Benchmark.Iterations) using (iteration.StartMeasurement()) for (int i = 0; i < 10000; i++) testString.Substring(startIndex, length); }
public void Split(int size) { PerfUtils utils = new PerfUtils(); string testString = utils.CreateString(size); string existingValue = testString.Substring(testString.Length / 2, 1); foreach (var iteration in Benchmark.Iterations) using (iteration.StartMeasurement()) for (int i = 0; i < 10000; i++) testString.Split(existingValue); }
public void op_Equality(int size) { PerfUtils utils = new PerfUtils(); bool result; string testString1 = utils.CreateString(size); string testString2 = utils.CreateString(size); foreach (var iteration in Benchmark.Iterations) using (iteration.StartMeasurement()) for (int i = 0; i < 10000; i++) { result = testString1 == testString2; result = testString1 == testString2; result = testString1 == testString2; result = testString1 == testString2; result = testString1 == testString2; result = testString1 == testString2; } }
public void GetLength(int size) { PerfUtils utils = new PerfUtils(); int result; string testString = utils.CreateString(size); foreach (var iteration in Benchmark.Iterations) using (iteration.StartMeasurement()) for (int i = 0; i < 10000; i++) { result = testString.Length; result = testString.Length; result = testString.Length; result = testString.Length; result = testString.Length; result = testString.Length; result = testString.Length; result = testString.Length; result = testString.Length; } }
public void Format(int numberOfObjects) { PerfUtils utils = new PerfUtils(); // Setup the format string and the list of objects to format StringBuilder formatter = new StringBuilder(); List<string> objects = new List<string>(); for (int i = 0; i < numberOfObjects; i++) { formatter.Append("%s, "); objects.Add(utils.CreateString(10)); } string format = formatter.ToString(); string[] objectArr = objects.ToArray(); // Perform the actual formatting foreach (var iteration in Benchmark.Iterations) using (iteration.StartMeasurement()) for (int i = 0; i < 5000; i++) string.Format(format, objectArr); }
public void Compress(CompressionType type) { byte[] bytes = CreateBytesToCompress(type); PerfUtils utils = new PerfUtils(); foreach (var iteration in Benchmark.Iterations) { string filePath = utils.GetTestFilePath(); using (FileStream output = File.Create(filePath)) using (DeflateStream zip = new DeflateStream(output, CompressionMode.Compress)) using (iteration.StartMeasurement()) { zip.Write(bytes, 0, bytes.Length); } File.Delete(filePath); } }
public void Trim_NothingToDo(int size) { PerfUtils utils = new PerfUtils(); string testString = utils.CreateString(size); foreach (var iteration in Benchmark.Iterations) using (iteration.StartMeasurement()) for (int i = 0; i < 10000; i++) testString.Trim(); }
public void TryGetValue(Dictionary<string, string> dict) { // Setup - utils needs a specific seed to prevent key collision with TestData string retrieved; PerfUtils utils = new PerfUtils(56334); string key = utils.CreateString(50); dict.Add(key, "value"); // Actual perf testing foreach (var iteration in Benchmark.Iterations) using (iteration.StartMeasurement()) for (int i = 0; i <= 1000; i++) { dict.TryGetValue(key, out retrieved); dict.TryGetValue(key, out retrieved); dict.TryGetValue(key, out retrieved); dict.TryGetValue(key, out retrieved); dict.TryGetValue(key, out retrieved); dict.TryGetValue(key, out retrieved); dict.TryGetValue(key, out retrieved); dict.TryGetValue(key, out retrieved); } // Teardown dict.Remove(key); }
private void PingFlood(CommandProcessorContext context, int clientsCnt, int requestsCnt) { context.IsAsync(); var autoResetEvent = new AutoResetEvent(false); var clients = new List <TcpTypedConnection <byte[]> >(); var threads = new List <Thread>(); var all = 0; for (int i = 0; i < clientsCnt; i++) { var count = requestsCnt / clientsCnt + ((i == clientsCnt - 1) ? requestsCnt % clientsCnt : 0); int sent = 0; int received = 0; var client = context.Client.CreateTcpConnection( context, (conn, msg) => { Interlocked.Increment(ref received); var pongs = Interlocked.Increment(ref all); if (pongs % 10000 == 0) { Console.Write('.'); } if (pongs == requestsCnt) { autoResetEvent.Set(); } }, connectionClosed: (conn, err) => { if (all < requestsCnt) { context.Fail(null, "Socket was closed, but not all requests were completed."); } else { context.Success(); } }); clients.Add(client); threads.Add(new Thread(() => { for (int j = 0; j < count; ++j) { var package = new TcpPackage(TcpCommand.Ping, Payload); client.EnqueueSend(package.AsByteArray()); Interlocked.Increment(ref sent); while (sent - received > context.Client.Options.PingWindow) { Thread.Sleep(1); } } })); } var sw = Stopwatch.StartNew(); foreach (var thread in threads) { thread.IsBackground = true; thread.Start(); } autoResetEvent.WaitOne(); sw.Stop(); foreach (var client in clients) { client.Close(); } var reqPerSec = (requestsCnt + 0.0) / sw.ElapsedMilliseconds * 1000; context.Log.Info("{0} requests completed in {1}ms ({2:0.00} reqs per sec).", requestsCnt, sw.ElapsedMilliseconds, reqPerSec); PerfUtils.LogData( Keyword, PerfUtils.Row(PerfUtils.Col("clientsCnt", clientsCnt), PerfUtils.Col("requestsCnt", requestsCnt), PerfUtils.Col("ElapsedMilliseconds", sw.ElapsedMilliseconds)) ); PerfUtils.LogTeamCityGraphData(string.Format("{0}-{1}-{2}-reqPerSec", Keyword, clientsCnt, requestsCnt), (int)reqPerSec); context.Success(); }
public void ContainsKey(Dictionary<string, string> dict) { // Setup - utils needs a specific seed to prevent key collision with TestData PerfUtils utils = new PerfUtils(152891); string key = utils.CreateString(50); dict.Add(key, "value"); // Actual perf testing foreach (var iteration in Benchmark.Iterations) using (iteration.StartMeasurement()) for (int i = 0; i <= 10000; i++) { dict.ContainsKey(key); dict.ContainsKey(key); dict.ContainsKey(key); dict.ContainsKey(key); dict.ContainsKey(key); dict.ContainsKey(key); dict.ContainsKey(key); dict.ContainsKey(key); dict.ContainsKey(key); dict.ContainsKey(key); dict.ContainsKey(key); dict.ContainsKey(key); } dict.Remove(key); }