Skip to content

Commit

Permalink
Added a few performance test variations
Browse files Browse the repository at this point in the history
  • Loading branch information
Redth committed Dec 20, 2017
1 parent 6a6c45d commit 3604edf
Showing 1 changed file with 72 additions and 30 deletions.
102 changes: 72 additions & 30 deletions src/MonkeyCache.TestsShared/BarrelTests.cs
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;

Expand Down Expand Up @@ -293,50 +294,91 @@ public void DoesNotExistsTest()

#endregion

#region Performance Tests
#region Performance Tests
[TestMethod]
public void PerformanceTests()
{
PerformanceTestRunner(1, true, 1000);
}

[TestMethod]
public void PerformanceTestsMultiThreaded()
{
PerformanceTestRunner(4, false, 1000);
}

[TestMethod]
public void PerformanceTestsMultiThreadedWithDuplicates()
{
PerformanceTestRunner(4, true, 1000);
}

var stopwatch = new Stopwatch();
stopwatch.Start();
void PerformanceTestRunner (int threads, bool allowDuplicateKeys, int keysPerThread)
{
var tasks = new List<Task>();

var keys = Enumerable.Range(0, 1000).Select(x => $"key-{x}").ToArray();
var mainStopwatch = new Stopwatch();
mainStopwatch.Start();

// Add a lot of items
foreach (var key in keys)
barrel.Add(key: key, data: monkeys, expireIn: TimeSpan.FromDays(1));

stopwatch.Stop();
Debug.WriteLine($"Add took {stopwatch.ElapsedMilliseconds} ms");
stopwatch.Restart();
for (int i = 0; i < threads; i++) {
var i2 = i;

foreach (var key in keys) {
var content = barrel.Get(key);
Assert.IsNotNull(content);
}
var task = Task.Factory.StartNew(() => {
var tId = i2;
stopwatch.Stop();
Debug.WriteLine($"Gets took {stopwatch.ElapsedMilliseconds} ms");
stopwatch.Restart();
var keyModifier = allowDuplicateKeys ? string.Empty : tId.ToString();
foreach (var key in keys) {
var content = barrel.GetETag(key);
Assert.IsNotNull(content);
}
var keys = Enumerable.Range(0, keysPerThread).Select(x => $"key-{keyModifier}-{x}").ToArray();
stopwatch.Stop();
Debug.WriteLine($"Get eTags took {stopwatch.ElapsedMilliseconds} ms");
stopwatch.Restart();
var stopwatch = new Stopwatch();
stopwatch.Start();
// Delete all
barrel.Empty(keys);
// Add a lot of items
foreach (var key in keys)
barrel.Add(key: key, data: monkeys, expireIn: TimeSpan.FromDays(1));
stopwatch.Stop();
Debug.WriteLine($"Add ({tId}) took {stopwatch.ElapsedMilliseconds} ms");
stopwatch.Restart();
foreach (var key in keys) {
var content = barrel.Get(key);
}
stopwatch.Stop();
Debug.WriteLine($"Gets ({tId}) took {stopwatch.ElapsedMilliseconds} ms");
stopwatch.Restart();
foreach (var key in keys) {
var content = barrel.GetETag(key);
}
stopwatch.Stop();
Debug.WriteLine($"Get ({tId}) eTags took {stopwatch.ElapsedMilliseconds} ms");
stopwatch.Restart();
// Delete all
barrel.Empty(keys);
stopwatch.Stop();
Debug.WriteLine($"Empty ({tId}) took {stopwatch.ElapsedMilliseconds} ms");
Assert.IsTrue(stopwatch.ElapsedMilliseconds > 1);
});

task.ContinueWith(t => {
if (t.Exception?.InnerException != null)
Debug.WriteLine(t.Exception.InnerException);
Assert.IsNull(t.Exception);
}, TaskContinuationOptions.OnlyOnFaulted);
tasks.Add(task);
}

stopwatch.Stop();
Debug.WriteLine($"Empty took {stopwatch.ElapsedMilliseconds} ms");
Task.WaitAll(tasks.ToArray());

Assert.IsTrue(stopwatch.ElapsedMilliseconds > 1);
mainStopwatch.Stop();
Debug.WriteLine($"Entire Test took {mainStopwatch.ElapsedMilliseconds} ms");
}

#endregion
Expand Down

0 comments on commit 3604edf

Please sign in to comment.