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+ "\n Press 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+ }
0 commit comments