-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
147 lines (133 loc) · 4.64 KB
/
Program.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using ClusterBenchmark;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
namespace ClusterBenchmark
{
public class Program
{
private long _requests = 0;
private long _responses = 0;
private readonly Random _rand = new Random();
static void Main(string[] args)
{
Program prog = new Program();
if(File.Exists("args.txt"))
{
//args = File.ReadAllText("args.txt").Split(null);
}
var options = Options.Parse(args);
Socket rpsserver = null;
if (options == null) return;
if (options.verbose)
{
DisplayConfigSettings(options);
}
if (options.servermode)
{
int listeningport = options.RPSport;
BenchmarkServer.StartListening(options.RPSport);
return;
}
else
{
if (options.RPSServer != null)
{
rpsserver = BenchmarkClient.StartClient(options.RPSServer, options.RPSport);
if (rpsserver == null || rpsserver.Connected == false)
{
return;
}
}
}
IClient client = new StackExchangeClient(options);
if (!client.CreateClients())
{
return;
}
string[] keyNames;
if ((keyNames = client.SetupCluster()) == null)
{
Console.Write("Setting up cluster failed");
}
prog.SendRequests(client, keyNames, options);
if (!prog.ReportRPS(options, rpsserver))
{
Console.WriteLine("There was an error");
}
}
private static void DisplayConfigSettings(Options options)
{
Console.WriteLine("Executing with following parameters:");
foreach (PropertyInfo prop in typeof(Options).GetProperties())
{
Console.WriteLine("{0,-15} = {1}", prop.Name, prop.GetValue(options, null));
}
}
public void SendRequests(IClient client, string[] keysarr, Options option)
{
for (int i = 0; i < option.asyncRequests; i++)
{
SendRequestAsync(client, i % option.clients, keysarr[i % keysarr.Length]);
}
}
public void SendRequestAsync(IClient client, int clientID, string val)
{
Interlocked.Increment(ref _requests);
client.StringGetAsync(clientID, val).ContinueWith(t =>
{
Interlocked.Increment(ref _responses);
SendRequestAsync(client, clientID, val);
});
}
public bool ReportRPS(Options option, Socket rpsServer)
{
Console.WriteLine("Warmup for {0}ms...", option.warmup);
Thread.Sleep(option.warmup);
DateTime start = DateTime.Now;
long _responsesAfterWarmup = _responses;
long lastRequests = 0;
long lastResponses = 0;
double rps;
while (true)
{
var elapsed = (DateTime.Now - start).TotalSeconds;
var requests = _requests;
var responses = _responses;
lastRequests = requests;
lastResponses = responses;
try
{
if (elapsed > 0)
{
rps = Math.Round((responses - _responsesAfterWarmup) / elapsed);
if (option.verbose)
{
Console.WriteLine("[{0,8}] Requests:{1,8} Responses:{2,8} RPS:{3,8}", DateTime.Now.ToString("HH:mm:ss.fff"), requests, responses, rps);
}
else
{
Console.WriteLine("[{0,8}] RPS:{1,8}", DateTime.Now.ToString("HH:mm:ss.fff"), rps);
}
if (rpsServer != null && rpsServer.Connected)
{
BenchmarkClient.Send(rpsServer, rps);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Thread.Sleep(1000);
}
}
}
}