Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit a9a9cb7

Browse files
committed
Add HashStressTest to Console Integration test
1 parent 38c192d commit a9a9cb7

File tree

4 files changed

+243
-102
lines changed

4 files changed

+243
-102
lines changed

tests/Console.Tests/Console.Tests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
<Reference Include="ServiceStack.Client">
3939
<HintPath>..\..\lib\tests\ServiceStack.Client.dll</HintPath>
4040
</Reference>
41+
<Reference Include="ServiceStack.Common, Version=4.0.0.0, Culture=neutral, processorArchitecture=MSIL">
42+
<SpecificVersion>False</SpecificVersion>
43+
<HintPath>..\..\..\Test\lib\ServiceStack.Common.dll</HintPath>
44+
</Reference>
4145
<Reference Include="ServiceStack.Interfaces">
4246
<HintPath>..\..\lib\ServiceStack.Interfaces.dll</HintPath>
4347
</Reference>
@@ -56,6 +60,8 @@
5660
<Reference Include="System.Xml" />
5761
</ItemGroup>
5862
<ItemGroup>
63+
<Compile Include="HashStressTest.cs" />
64+
<Compile Include="LongRunningRedisPubSubServer.cs" />
5965
<Compile Include="Program.cs" />
6066
<Compile Include="Properties\AssemblyInfo.cs" />
6167
</ItemGroup>

tests/Console.Tests/HashStressTest.cs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using System;
2+
using System.Threading;
3+
using ServiceStack.Redis;
4+
using ServiceStack.Redis.Generic;
5+
using ServiceStack;
6+
using ServiceStack.Text;
7+
8+
namespace TestRedisConnection
9+
{
10+
class DeviceInfo
11+
{
12+
public Guid PlayerID { get; set; }
13+
public DateTime? LastErrTime { get; set; }
14+
public DateTime? LastWarnTime { get; set; }
15+
16+
protected bool Equals(DeviceInfo other)
17+
{
18+
return PlayerID.Equals(other.PlayerID)
19+
&& LastErrTime.Equals(other.LastErrTime)
20+
&& LastWarnTime.Equals(other.LastWarnTime);
21+
}
22+
23+
public override bool Equals(object obj)
24+
{
25+
if (ReferenceEquals(null, obj)) return false;
26+
if (ReferenceEquals(this, obj)) return true;
27+
if (obj.GetType() != this.GetType()) return false;
28+
return Equals((DeviceInfo) obj);
29+
}
30+
}
31+
32+
public class HashStressTest
33+
{
34+
public RedisManagerPool redisManager;
35+
private DeviceInfo data = new DeviceInfo
36+
{
37+
PlayerID = new Guid("560531b06bc945b688f3a6a8ade65354"),
38+
LastErrTime = new DateTime(2000, 1, 1),
39+
LastWarnTime = new DateTime(2001, 1, 1),
40+
};
41+
42+
private int running = 0;
43+
private string _collectionKey = typeof (HashStressTest).Name;
44+
private TimeSpan? waitBeforeRetry = null;
45+
//private TimeSpan? waitBeforeRetry = TimeSpan.FromMilliseconds(1);
46+
47+
private long writeCount = 0;
48+
private long readCount = 0;
49+
50+
public void Execute(string ipAddress, int noOfThreads = 64)
51+
{
52+
redisManager = new RedisManagerPool(new[]{ ipAddress }, new RedisPoolConfig {
53+
MaxPoolSize = noOfThreads
54+
});
55+
56+
var StartedAt = DateTime.UtcNow;
57+
Interlocked.Increment(ref running);
58+
59+
"Starting HashStressTest with {0} threads".Print(noOfThreads);
60+
var threads = noOfThreads.Times(i => new Thread(WorkerLoop));
61+
threads.Each(t => t.Start());
62+
63+
"Press Enter to Stop...".Print();
64+
Console.ReadLine();
65+
66+
Interlocked.Decrement(ref running);
67+
68+
"Writes: {0}, Reads: {1}".Print(writeCount, readCount);
69+
"{0} EndedAt: {1}".Print(GetType().Name, DateTime.UtcNow.ToLongTimeString());
70+
"{0} TimeTaken: {1}s".Print(GetType().Name, (DateTime.UtcNow - StartedAt).TotalSeconds);
71+
72+
//Uncomment to wait for all threads to finish
73+
//threads.Each(t => t.Join());
74+
75+
"\nPress Enter to Quit...".Print();
76+
Console.ReadLine();
77+
}
78+
79+
public void WorkerLoop()
80+
{
81+
while (Interlocked.CompareExchange(ref running, 0, 0) > 0)
82+
{
83+
using (var client = redisManager.GetClient())
84+
{
85+
try
86+
{
87+
GetCollection<Guid, DeviceInfo>(client)[data.PlayerID] = data;
88+
Interlocked.Increment(ref writeCount);
89+
}
90+
catch (Exception ex)
91+
{
92+
Console.WriteLine("WRITE ERROR: " + ex.Message);
93+
}
94+
95+
try
96+
{
97+
var readData = GetCollection<Guid, DeviceInfo>(client)[data.PlayerID];
98+
Interlocked.Increment(ref readCount);
99+
100+
if (!readData.Equals(data))
101+
{
102+
Console.WriteLine("Data Error: " + readData.Dump());
103+
}
104+
}
105+
catch (Exception ex)
106+
{
107+
Console.WriteLine("READ ERROR: " + ex.Message);
108+
}
109+
}
110+
111+
if (waitBeforeRetry != null)
112+
Thread.Sleep(waitBeforeRetry.Value);
113+
}
114+
}
115+
116+
private IRedisHash<TKey, TValue> GetCollection<TKey, TValue>(IRedisClient redis)
117+
{
118+
var _redisTypedClient = redis.As<TValue>();
119+
return _redisTypedClient.GetHash<TKey>(_collectionKey);
120+
}
121+
}
122+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using System.Timers;
5+
using ServiceStack.Redis;
6+
using ServiceStack.Text;
7+
using Timer = System.Timers.Timer;
8+
9+
namespace TestRedisConnection
10+
{
11+
public class LongRunningRedisPubSubServer
12+
{
13+
private const string Channel = "longrunningtest";
14+
private static DateTime StartedAt;
15+
16+
private static long MessagesSent = 0;
17+
private static long HeartbeatsSent = 0;
18+
private static long HeartbeatsReceived = 0;
19+
private static long StartCount = 0;
20+
private static long StopCount = 0;
21+
private static long DisposeCount = 0;
22+
private static long ErrorCount = 0;
23+
private static long FailoverCount = 0;
24+
private static long UnSubscribeCount = 0;
25+
26+
public static RedisManagerPool Manager { get; set; }
27+
public static RedisPubSubServer PubSubServer { get; set; }
28+
29+
public void Execute(string ipAddress)
30+
{
31+
Manager = new RedisManagerPool(ipAddress);
32+
StartedAt = DateTime.UtcNow;
33+
34+
var q = new Timer { Interval = 1000 };
35+
q.Elapsed += OnInterval;
36+
q.Enabled = true;
37+
38+
using (PubSubServer = new RedisPubSubServer(Manager, Channel)
39+
{
40+
OnStart = () =>
41+
{
42+
Console.WriteLine("OnStart: #" + Interlocked.Increment(ref StartCount));
43+
},
44+
OnHeartbeatSent = () =>
45+
{
46+
Console.WriteLine("OnHeartbeatSent: #" + Interlocked.Increment(ref HeartbeatsSent));
47+
},
48+
OnHeartbeatReceived = () =>
49+
{
50+
Console.WriteLine("OnHeartbeatReceived: #" + Interlocked.Increment(ref HeartbeatsReceived));
51+
},
52+
OnMessage = (channel, msg) =>
53+
{
54+
Console.WriteLine("OnMessage: @" + channel + ": " + msg);
55+
},
56+
OnStop = () =>
57+
{
58+
Console.WriteLine("OnStop: #" + Interlocked.Increment(ref StopCount));
59+
},
60+
OnError = ex =>
61+
{
62+
Console.WriteLine("OnError: #" + Interlocked.Increment(ref ErrorCount) + " ERROR: " + ex);
63+
},
64+
OnFailover = server =>
65+
{
66+
Console.WriteLine("OnFailover: #" + Interlocked.Increment(ref FailoverCount));
67+
},
68+
OnDispose = () =>
69+
{
70+
Console.WriteLine("OnDispose: #" + Interlocked.Increment(ref DisposeCount));
71+
},
72+
OnUnSubscribe = channel =>
73+
{
74+
Console.WriteLine("OnUnSubscribe: #" + Interlocked.Increment(ref UnSubscribeCount) + " channel: " + channel);
75+
},
76+
})
77+
{
78+
Console.WriteLine("PubSubServer StartedAt: " + StartedAt.ToLongTimeString());
79+
PubSubServer.Start();
80+
81+
"Press Enter to Quit...".Print();
82+
Console.ReadLine();
83+
Console.WriteLine("PubSubServer EndedAt: " + DateTime.UtcNow.ToLongTimeString());
84+
Console.WriteLine("PubSubServer TimeTaken: " + (DateTime.UtcNow - StartedAt).TotalSeconds + "s");
85+
}
86+
}
87+
88+
private static void OnInterval(object sender, ElapsedEventArgs e)
89+
{
90+
Task.Factory.StartNew(PublishMessage);
91+
}
92+
93+
private static void PublishMessage()
94+
{
95+
try
96+
{
97+
var message = "MSG: #" + Interlocked.Increment(ref MessagesSent);
98+
Console.WriteLine("PublishMessage(): " + message);
99+
using (var redis = Manager.GetClient())
100+
{
101+
redis.PublishMessage(Channel, message);
102+
}
103+
}
104+
catch (Exception ex)
105+
{
106+
Console.WriteLine("ERROR PublishMessage: " + ex);
107+
}
108+
}
109+
110+
}
111+
}

tests/Console.Tests/Program.cs

Lines changed: 4 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-

2-
using System;
3-
using System.Threading;
4-
using System.Threading.Tasks;
5-
using System.Timers;
6-
using ServiceStack.Redis;
7-
using ServiceStack.Text;
8-
using Timer = System.Timers.Timer;
9-
10-
namespace TestRedisConnection
1+
namespace TestRedisConnection
112
{
123
public class Incr
134
{
@@ -21,101 +12,12 @@ public class IncrResponse
2112

2213
class Program
2314
{
24-
private const string Channel = "longrunningtest";
25-
private static DateTime StartedAt;
26-
27-
private static long MessagesSent = 0;
28-
private static long HeartbeatsSent = 0;
29-
private static long HeartbeatsReceived = 0;
30-
private static long StartCount = 0;
31-
private static long StopCount = 0;
32-
private static long DisposeCount = 0;
33-
private static long ErrorCount = 0;
34-
private static long FailoverCount = 0;
35-
private static long UnSubscribeCount = 0;
36-
37-
public static RedisManagerPool Manager { get; set; }
38-
public static RedisPubSubServer PubSubServer { get; set; }
39-
4015
static void Main(string[] args)
4116
{
42-
Manager = new RedisManagerPool("10.0.0.9");
43-
StartedAt = DateTime.UtcNow;
44-
45-
var q = new Timer { Interval = 1000 };
46-
q.Elapsed += OnInterval;
47-
q.Enabled = true;
48-
49-
using (PubSubServer = new RedisPubSubServer(Manager, Channel)
50-
{
51-
OnStart = () =>
52-
{
53-
Console.WriteLine("OnStart: #" + Interlocked.Increment(ref StartCount));
54-
},
55-
OnHeartbeatSent = () =>
56-
{
57-
Console.WriteLine("OnHeartbeatSent: #" + Interlocked.Increment(ref HeartbeatsSent));
58-
},
59-
OnHeartbeatReceived = () =>
60-
{
61-
Console.WriteLine("OnHeartbeatReceived: #" + Interlocked.Increment(ref HeartbeatsReceived));
62-
},
63-
OnMessage = (channel, msg) =>
64-
{
65-
Console.WriteLine("OnMessage: @" + channel + ": " + msg);
66-
},
67-
OnStop = () =>
68-
{
69-
Console.WriteLine("OnStop: #" + Interlocked.Increment(ref StopCount));
70-
},
71-
OnError = ex =>
72-
{
73-
Console.WriteLine("OnError: #" + Interlocked.Increment(ref ErrorCount) + " ERROR: " + ex);
74-
},
75-
OnFailover = server =>
76-
{
77-
Console.WriteLine("OnFailover: #" + Interlocked.Increment(ref FailoverCount));
78-
},
79-
OnDispose = () =>
80-
{
81-
Console.WriteLine("OnDispose: #" + Interlocked.Increment(ref DisposeCount));
82-
},
83-
OnUnSubscribe = channel =>
84-
{
85-
Console.WriteLine("OnUnSubscribe: #" + Interlocked.Increment(ref UnSubscribeCount) + " channel: " + channel);
86-
},
87-
})
88-
{
89-
Console.WriteLine("PubSubServer StartedAt: " + StartedAt.ToLongTimeString());
90-
PubSubServer.Start();
91-
92-
"Press Enter to Quit...".Print();
93-
Console.ReadLine();
94-
Console.WriteLine("PubSubServer EndedAt: " + DateTime.UtcNow.ToLongTimeString());
95-
Console.WriteLine("PubSubServer TimeTaken: " + (DateTime.UtcNow - StartedAt).TotalSeconds + "s");
96-
}
97-
}
98-
99-
private static void OnInterval(object sender, ElapsedEventArgs e)
100-
{
101-
Task.Factory.StartNew(PublishMessage);
102-
}
17+
//new LongRunningRedisPubSubServer().Execute("10.0.0.9");
10318

104-
private static void PublishMessage()
105-
{
106-
try
107-
{
108-
var message = "MSG: #" + Interlocked.Increment(ref MessagesSent);
109-
Console.WriteLine("PublishMessage(): " + message);
110-
using (var redis = Manager.GetClient())
111-
{
112-
redis.PublishMessage(Channel, message);
113-
}
114-
}
115-
catch (Exception ex)
116-
{
117-
Console.WriteLine("ERROR PublishMessage: " + ex);
118-
}
19+
new HashStressTest().Execute("127.0.0.1");
20+
//new HashStressTest().Execute("10.0.0.9");
11921
}
12022
}
12123
}

0 commit comments

Comments
 (0)