Last active
August 29, 2015 14:23
-
-
Save PureKrome/be8967ebc2bc43ac8154 to your computer and use it in GitHub Desktop.
Load testing Redis servers via IIS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Configuration; | |
using System.Text; | |
using System.Threading; | |
using System.Web.Mvc; | |
using StackExchange.Redis; | |
namespace WebApplication2.Controllers | |
{ | |
public class HomeController : Controller | |
{ | |
private static int _errorCount; | |
private static long _lastErrorOn; | |
private static readonly Lazy<ConnectionMultiplexer> LazyConnection = | |
new Lazy<ConnectionMultiplexer>(() => | |
{ | |
var redisConnectionString = ConfigurationManager.ConnectionStrings["Redis"].ConnectionString | |
?? "-no connection string found-"; | |
return ConnectionMultiplexer.Connect(redisConnectionString); | |
}); | |
private readonly string _redisConnectionString; | |
public HomeController() | |
{ | |
_redisConnectionString = ConfigurationManager.ConnectionStrings["Redis"].ConnectionString | |
?? "-no connection string found-"; | |
} | |
public static ConnectionMultiplexer Connection | |
{ | |
get { return LazyConnection.Value; } | |
} | |
public string Index() | |
{ | |
var result = new StringBuilder(); | |
try | |
{ | |
Connection.GetDatabase(); | |
result.AppendFormat("SUCCESSFULLY connected to Redis @: {0}<br/><br/>{1}", | |
_redisConnectionString, | |
DateTime.UtcNow.ToString("T")); | |
} | |
catch (Exception exc) | |
{ | |
var erroredOn = DateTime.UtcNow; | |
Interlocked.Increment(ref _errorCount); | |
Interlocked.Exchange(ref _lastErrorOn, erroredOn.Ticks); | |
result.Append(string.Format("FAILED to connect to Redis @: {0}<br/>", _redisConnectionString)); | |
result.Append(string.Format("Exception: {0}<br/>", exc.Message)); | |
} | |
result.Append("<hr/>"); | |
result.Append("<br/>"); | |
result.AppendFormat("Error Count: {0}<br/>", _errorCount); | |
result.AppendFormat("Last Error On (UTC): {0}<br/>", _lastErrorOn > 0 | |
? new DateTime(_lastErrorOn).ToString("o") | |
: "none"); | |
return result.ToString(); | |
} | |
public ActionResult About() | |
{ | |
ViewBag.Message = "Your application description page."; | |
return View(); | |
} | |
public ActionResult Contact() | |
{ | |
ViewBag.Message = "Your contact page."; | |
return View(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment