Skip to content

Instantly share code, notes, and snippets.

@nickheppleston
Created November 14, 2014 11:27
Show Gist options
  • Select an option

  • Save nickheppleston/35a71bdd4682e01a0099 to your computer and use it in GitHub Desktop.

Select an option

Save nickheppleston/35a71bdd4682e01a0099 to your computer and use it in GitHub Desktop.

Revisions

  1. nickheppleston created this gist Nov 14, 2014.
    64 changes: 64 additions & 0 deletions RedisPerfTest.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using StackExchange.Redis;

    namespace RedisPerfTest
    {
    class Program
    {
    const string AZURE_REDIS_CONNECTION_STRING = "[HOSTNAME].redis.cache.windows.net,ssl=false,password=[PASSWORD],allowAdmin=true";
    const string LOCAL_REDIS_CONNECTION_STRING = "localhost:6379,ssl=false,allowAdmin=true";

    static void Main(string[] args)
    {
    //var cacheConnection = ConnectionMultiplexer.Connect(AZURE_REDIS_CONNECTION_STRING);
    var cacheConnection = ConnectionMultiplexer.Connect(LOCAL_REDIS_CONNECTION_STRING);

    Console.WriteLine("Starting Redis Perf Test");

    for (int c = 0; c < 3; c++)
    {
    FlushCacheDb(cacheConnection); // Flush the cache databases before we start the test.
    var cache = cacheConnection.GetDatabase();

    var stopwatch = Stopwatch.StartNew();

    // Set Cache Items
    for (int i = 0; i < 1000; i++)
    {
    var cacheKey = i.ToString();
    var cacheItem = i;
    cache.StringSet(cacheKey, (int)cacheItem, TimeSpan.FromMinutes(90));
    }

    // Retrieve Cache Items
    for (int i = 0; i < 1000; i++)
    {
    var cacheKey = i.ToString();
    var cacheItem = (int)cache.StringGet(cacheKey);
    }

    stopwatch.Stop();

    Console.WriteLine(String.Format("Iteration {0} - Elapsed Seconds/Milliseconds: {1} / {2}", c, stopwatch.Elapsed.TotalSeconds, stopwatch.ElapsedMilliseconds));
    }

    Console.WriteLine("Finished Redis Perf Test");
    Console.ReadLine();
    }

    static void FlushCacheDb(ConnectionMultiplexer cacheConnection)
    {
    var endpoints = cacheConnection.GetEndPoints(true);
    foreach (var endpoint in endpoints)
    {
    var server = cacheConnection.GetServer(endpoint);
    server.FlushAllDatabases();
    }
    }
    }
    }