Skip to content

Instantly share code, notes, and snippets.

@vainolo
Created October 29, 2018 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vainolo/0cd733d638d4c6cbecdd316fb8584b3b to your computer and use it in GitHub Desktop.
Save vainolo/0cd733d638d4c6cbecdd316fb8584b3b to your computer and use it in GitHub Desktop.
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