Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Taking Azure Redis Cache for a ride – testing a simple Producer/Consumer program - Producer
using System.Diagnostics;
using System.Net;
using System.Threading;
using Microsoft.WindowsAzure.ServiceRuntime;
using StackExchange.Redis;
namespace Producer
{
public class WorkerRole : RoleEntryPoint
{
Stopwatch watch;
ISubscriber subscriber;
int count = 0;
int ITERATIONS = 1000;
public override void Run()
{
Trace.TraceInformation("Starting Test");
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect("");
IDatabase cache = connection.GetDatabase();
subscriber = connection.GetSubscriber();
watch = new Stopwatch();
subscriber.Subscribe("pong", (channel, message) => getPong(message));
watch.Start();
subscriber.Publish("ping", count);
while (true)
{
Thread.Sleep(1000);
}
}
private void getPong(RedisValue message)
{
watch.Stop();
if (!count.ToString().Equals(message))
{
Trace.TraceInformation("Got wrong message. Sent:" + count + ", Received:" + message);
}
count++;
if (count == ITERATIONS)
{
Trace.TraceInformation("Finished Test. Running " + count + " iterations. Total time:" + watch.ElapsedMilliseconds + ". Average: " + watch.ElapsedMilliseconds / count);
finished = true;
return;
}
watch.Start();
subscriber.Publish("ping",count);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment