Created
October 29, 2018 21:01
-
-
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
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.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