-
Notifications
You must be signed in to change notification settings - Fork 0
/
StackExchangeClient.cs
103 lines (93 loc) · 3.53 KB
/
StackExchangeClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace ClusterBenchmark
{
class StackExchangeClient : IClient
{
private IDatabase[] dbs;
public ConfigurationOptions ConfigOptions { get; private set; }
public Options Options { get; private set; }
public StackExchangeClient(Options options)
{
dbs = new IDatabase[options.clients];
ConfigOptions = InitConfigurationOptions(options);
Options = options;
}
private ConfigurationOptions InitConfigurationOptions(Options options)
{
var config = new ConfigurationOptions();
config.EndPoints.Add(options.redisserver, options.redisport);
config.Ssl = !options.nonssl;
config.Password = options.password;
config.AllowAdmin = true;
return config;
}
public bool CreateClients()
{
for (int i = 0; i < Options.clients; i++)
{
try
{
var cm = ConnectionMultiplexer.Connect(ConfigOptions);
var db = cm.GetDatabase();
dbs[i] = db;
}
catch (Exception e)
{
Console.WriteLine(e.Message + e.StackTrace);
return false;
}
}
return true;
}
public Task<bool> StringSetAsync(long clientid, string key, string value)
{
return dbs[clientid].StringSetAsync(key, value);
}
public Task<RedisValue> StringGetAsync(long clientid, string key)
{
return dbs[clientid].StringGetAsync(key);
}
public string[] SetupCluster()
{
string[] keysarr = null;
Random rand = new Random();
byte[] val = new byte[Options.valueSize];
rand.NextBytes(val);
using (var cm = ConnectionMultiplexer.Connect(ConfigOptions))
{
var db = cm.GetDatabase();
EndPoint e = db.Multiplexer.GetEndPoints()[0];
var serverType = db.Multiplexer.GetServer(e);
if (serverType.ServerType == ServerType.Cluster)
{
Console.WriteLine("Setting up cluster with keyname prefix {0}, value size {1}", Options.keyPrefix, Options.valueSize);
var cn = serverType.ClusterNodes();
Dictionary<int, string> keys = new Dictionary<int, string>();
int count = 0;
int totalmasters = db.Multiplexer.GetEndPoints().Length / 2;
do
{
string keyname = Options.keyPrefix + count;
keys[Int32.Parse(cn.GetBySlot(db.Multiplexer.HashSlot(keyname)).EndPoint.ToString().Split(':')[1])] = keyname;
db.StringSet(keyname, val);
count++;
} while (keys.Count < totalmasters);
keysarr = keys.Values.ToArray();
}
else
{
Console.WriteLine("Setting up cache with test keys, value size {0}", Options.valueSize);
db.StringSet(Options.keyPrefix, val);
keysarr = new string[] { Options.keyPrefix };
}
}
return keysarr;
}
}
}