Skip to content

Instantly share code, notes, and snippets.

@josh-cooley
Created November 26, 2014 17:12
Show Gist options
  • Save josh-cooley/ce96165dc64a65486f8b to your computer and use it in GitHub Desktop.
Save josh-cooley/ce96165dc64a65486f8b to your computer and use it in GitHub Desktop.
Time the FileCache.Find operation in the BruTile library.
using BruTile;
using BruTile.Cache;
using System;
using System.Diagnostics;
using System.Threading;
namespace TimeFileCache
{
// == using lock/Monitor ==
// == 20 tiles fetched ==
// Average FileCache.Find time is 0.6ms
// Max time is 2ms
// Min time is 0ms
//
// == 500 tiles fetched ==
// Average FileCache.Find time is 0.926ms
// Max time is 17ms
// Min time is 0ms
//
// == Change to ReaderWriterLockSlim ==
// == 20 tiles fetched ==
// Average FileCache.Find time is 0ms
// Max time is 0ms
// Min time is 0ms
//
// == 500 tiles fetched ==
// Average FileCache.Find time is 0.004ms
// Max time is 1ms
// Min time is 0ms
class Program
{
private static long cacheTime = 0;
private static long minTime = long.MaxValue;
private static long maxTime = long.MinValue;
private static object timeLock = new object();
private static int threadCompleteCount = 0;
static void Main(string[] args)
{
var cache = new FileCache(@"c:\FileCache", "png");
int testCount = 500;
for (int i = 0; i < testCount; i++)
{
cache.Add(new TileIndex(i % 10, i / 10, "1"), new byte[] { (byte)i });
}
for (int i = 0; i < testCount; i++)
{
var tileIndex = new TileIndex(i % 10, i / 10, "1");
ThreadPool.QueueUserWorkItem(o => TimeCache(cache, tileIndex));
}
while (Interlocked.CompareExchange(ref threadCompleteCount, 0, testCount) != testCount)
{
Thread.Sleep(50);
}
Console.WriteLine("Average FileCache.Find time is {0}ms", (double)cacheTime / testCount);
Console.WriteLine("Max time is {0}ms", maxTime);
Console.WriteLine("Min time is {0}ms", minTime);
}
static void TimeCache(IPersistentCache<byte[]> cache, TileIndex index)
{
var stopwatch = Stopwatch.StartNew();
var bytes = cache.Find(index);
stopwatch.Stop();
lock(timeLock)
{
cacheTime += stopwatch.ElapsedMilliseconds;
minTime = Math.Min(minTime, stopwatch.ElapsedMilliseconds);
maxTime = Math.Max(maxTime, stopwatch.ElapsedMilliseconds);
}
Interlocked.Increment(ref threadCompleteCount);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment