-
Notifications
You must be signed in to change notification settings - Fork 0
/
cchess_bench.cc
549 lines (499 loc) · 13.9 KB
/
cchess_bench.cc
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#include "muduo/base/Logging.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/EventLoopThreadPool.h"
#include "muduo/base/ThreadPool.h"
#include "websocket/WebsocketClient.h"
#include "client_codec/codec.h"
#include "client_codec/dispatcher.h"
#include "cchess.pb.h"
#include <mutex>
#include <atomic>
#include <algorithm>
#include <random>
#include <tuple>
#include "cchess_engine/board.h"
#include "cchess_engine/search_engine.h"
using namespace muduo;
using namespace muduo::net;
using namespace muduo::net::websocket;
typedef std::shared_ptr<wsun::LoginSuccessResponse> LoginSuccessResponsePtr;
typedef std::shared_ptr<wsun::LoginFailedResponse> LoginFailedResponsePtr;
typedef std::shared_ptr<wsun::MatchResponse> MatchResponsePtr;
typedef std::shared_ptr<wsun::MoveStep> MoveStepPtr;
typedef std::shared_ptr<wsun::ForgiveRequest> ForgiveRequestPtr;
typedef std::shared_ptr<wsun::ForgiveResponse> ForgiveResponsePtr;
typedef std::shared_ptr<wsun::DrawRequest> DrawRequestPtr;
typedef std::shared_ptr<wsun::DrawResponse> DrawResponsePtr;
typedef std::shared_ptr<wsun::OppReconnected> OppReconnectedPtr;
typedef std::shared_ptr<wsun::OppDisconnected> OppDisconnectedPtr;
typedef std::shared_ptr<wsun::GameOver> GameOverPtr;
typedef std::shared_ptr<wsun::GameSituation> GameSituationPtr;
using ::wsun::cchess::Board;
using ::wsun::cchess::SearchEngine;
class Engine
{
public:
Engine() : board_(new Board), innerEngine_(new SearchEngine(board_.get()))
{
}
std::tuple<bool,int,std::string> play(const std::string& fen = ::wsun::cchess::INIT_FEN_STRING)
{
board_->resetFromFen(fen.c_str());
int mv = innerEngine_->search(kSearchTime);
board_->play(mv);
std::string fenString = board_->toFen();
bool over = board_->noWayToMove();
return std::make_tuple(over, mv, fenString);
}
private:
std::unique_ptr<Board> board_;
std::unique_ptr<SearchEngine> innerEngine_;
static const int kSearchTime;
};
const int Engine::kSearchTime = 1;
// 分配一个线程局部对象, 便于线程池中做engine search 任务
__thread Engine* gEngine;
int getRandomNumber(int min, int max)
{
static std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(min, max);
return distribution(generator);
}
struct RobotData
{
RobotData()
{
reset();
quicklyPatternTimes = getRandomNumber(10, 20);
slowlyPatternTimes = getRandomNumber(2, 10);
}
void reset()
{
waitTimeToLogin = getRandomNumber(3, 5);
waitTimeToMatch = getRandomNumber(2, 10);
intervalToMove = getRandomNumber(1, 10);
stepsForDisconnect = getRandomNumber(10, 15);
stepsForGiveup = getRandomNumber(20, 40);
intervalToMatchAgain = getRandomNumber(2, 5);
}
//建立连接后多少秒发登录请求
int waitTimeToLogin;
//登录成功之后多少秒发匹配请求
int waitTimeToMatch;
//匹配成功后,每隔多少秒走一步
int intervalToMove;
//走多少步断线,断线之后自动重新连接
int stepsForDisconnect;
//每场下多少步棋直接认输
int stepsForGiveup;
//每结束一场,间隔多少秒开始匹配
int intervalToMatchAgain;
//玩多少场十分钟的,多少场二十分钟的
int quicklyPatternTimes;
int slowlyPatternTimes;
};
class UserStorage
{
public:
typedef std::array<int,60000> Array;
UserStorage()
{
nameIdArray_.fill(1);
curIdx_ = nameIdArray_.begin();
}
std::string getVaildUsername()
{
std::ostringstream oss;
int idx = -1;
{
std::lock_guard<std::mutex> lock(mut_);
while (*curIdx_ == 0)
{
advanceIterator();
}
idx = std::distance(nameIdArray_.begin(), curIdx_);
*curIdx_ = 0;
advanceIterator();
}
oss << "wintersun" << idx;
return oss.str();
}
void recycleUsername(const std::string& name)
{
int idx = atoi(name.substr(9).c_str());
Array::iterator it;
std::advance(it, idx);
*it = 1;
}
private:
void advanceIterator()
{
if (++curIdx_ == nameIdArray_.end())
{
curIdx_ = nameIdArray_.begin();
}
}
std::mutex mut_;
Array nameIdArray_;
Array::iterator curIdx_;
};
UserStorage gUserStorage;
class Client : noncopyable
{
public:
enum State
{
kConnected,
kOnline,
kMatching,
kPlaying,
kDisconnectedInPlaying,
kDisconnected
};
Client(EventLoop* loop, const InetAddress& addr, ThreadPool* taskPool)
: loop_(loop),
client_(loop, addr, "CChessClient"),
dispatcher_(std::bind(&Client::onUnknownMessage, this, _1, _2)),
codec_(std::bind(&ProtobufDispatcher::onProtobufMessage, &dispatcher_, _1, _2)),
data_(new RobotData),
playedTimes_(0),
state_(kDisconnected),
pool_(taskPool),
inTaskQueue_(false)
{
client_.setConnectionCallback(
std::bind(&Client::onConnection, this, _1));
client_.setBinaryMessageCallback(
std::bind(&ProtobufCodec::onMessage, &codec_, _1, _2, _3));
dispatcher_.registerMessageCallback<wsun::LoginSuccessResponse>(
std::bind(&Client::onLoginSuccessResponse, this, _1, _2));
dispatcher_.registerMessageCallback<wsun::LoginFailedResponse>(
std::bind(&Client::onLoginFailedResponse, this, _1, _2));
dispatcher_.registerMessageCallback<wsun::MatchResponse>(
std::bind(&Client::onMatchResponse, this, _1, _2));
dispatcher_.registerMessageCallback<wsun::MoveStep>(
std::bind(&Client::onMoveStep, this, _1, _2));
dispatcher_.registerMessageCallback<wsun::OppDisconnected>(
std::bind(&Client::onOppDisconnected, this, _1, _2));
dispatcher_.registerMessageCallback<wsun::OppReconnected>(
std::bind(&Client::onOppReconnected, this, _1, _2));
dispatcher_.registerMessageCallback<wsun::GameSituation>(
std::bind(&Client::onGameSituation, this, _1, _2));
dispatcher_.registerMessageCallback<wsun::GameOver>(
std::bind(&Client::onGameOver, this, _1, _2));
dispatcher_.registerMessageCallback<wsun::ForgiveRequest>(
std::bind(&Client::onForgiveRequest, this, _1, _2));
dispatcher_.registerMessageCallback<wsun::ForgiveResponse>(
std::bind(&Client::onForgiveResponse, this, _1, _2));
dispatcher_.registerMessageCallback<wsun::DrawRequest>(
std::bind(&Client::onDrawRequest, this, _1, _2));
dispatcher_.registerMessageCallback<wsun::DrawResponse>(
std::bind(&Client::onDrawResponse, this, _1, _2));
}
int getPlayTimes() const
{
return data_->quicklyPatternTimes + data_->slowlyPatternTimes;
}
int getPlayedTimes() const
{
return playedTimes_;
}
State getState() const
{
return state_;
}
void connect()
{
client_.connect();
}
private:
void onUnknownMessage(const WebsocketConnectionPtr& conn,
const MessagePtr& message)
{
LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
conn->forceClose();
}
void onConnection(const WebsocketConnectionPtr& conn)
{
if (conn->connected())
{
conn_ = conn;
if (state_ == kDisconnected)
state_ = kConnected;
if (getPlayTimes() > 0)
loginTimer_ = loop_->runAfter(data_->waitTimeToLogin, std::bind(&Client::toLogin, this));
}
else
{
loop_->cancel(loginTimer_);
loop_->cancel(matchTimer_);
conn_.reset();
if (state_ == kPlaying)
{
state_ = kDisconnectedInPlaying;
}
else
{
state_ = kDisconnected;
}
client_.connect();
}
}
void toLogin()
{
assert(state_ == kConnected || state_ == kDisconnectedInPlaying);
if (username_.empty())
username_ = gUserStorage.getVaildUsername();
::wsun::LoginRequest req;
req.set_username(username_);
req.set_passwd(kPasswd);
if (conn_)
codec_.send(conn_, req);
}
::wsun::RoomType getRoomType()
{
using ::wsun::QUICKLY_PATTERN;
using ::wsun::SLOWLY_PATTERN;
return (playedTimes_ % 2 == 0 ?
(data_->quicklyPatternTimes == 0 ? SLOWLY_PATTERN : QUICKLY_PATTERN) :
(data_->slowlyPatternTimes == 0 ? QUICKLY_PATTERN : SLOWLY_PATTERN));
}
void toMatch()
{
assert(state_ == kOnline);
state_ = kMatching;
::wsun::MatchRequest req;
req.set_type(getRoomType());
if (conn_)
codec_.send(conn_, req);
}
void sendMoveStep(const MoveStepPtr& req)
{
inTaskQueue_ = false;
if (state_ != kPlaying) return ;
if (conn_)
{
--data_->stepsForDisconnect;
--data_->stepsForGiveup;
codec_.send(conn_, *req);
}
}
void doMoveTaskRoutine(const std::string& fen)
{
std::tuple<bool,int,std::string> res;
if (fen.empty())
res = gEngine->play();
else
res = gEngine->play(fen);
MoveStepPtr req(new ::wsun::MoveStep);
req->set_over(std::get<0>(res));
req->set_mv(std::get<1>(res));
req->set_fen(std::get<2>(res));
loop_->runInLoop(std::bind(&Client::sendMoveStep, this, req));
}
void toPlay(const std::string& fen)
{
if (data_->stepsForGiveup == 0)
{
toGiveup();
}
else
{
pool_->run(std::bind(&Client::doMoveTaskRoutine, this, fen));
inTaskQueue_ = true;
}
}
void toGiveup()
{
::wsun::Giveup req;
codec_.send(conn_, req);
}
void onLoginSuccessResponse(const WebsocketConnectionPtr& conn, const LoginSuccessResponsePtr& msg)
{
state_ = kOnline;
matchTimer_ = loop_->runAfter(data_->waitTimeToMatch, std::bind(&Client::toMatch, this));
}
void onLoginFailedResponse(const WebsocketConnectionPtr& conn, const LoginFailedResponsePtr& msg)
{
loginTimer_ = loop_->runAfter(data_->waitTimeToLogin, std::bind(&Client::toLogin, this));
}
void onMatchResponse(const WebsocketConnectionPtr& conn, const MatchResponsePtr& msg)
{
state_ = kPlaying;
if (msg->selftype() == ::wsun::MatchResponse::RED)
toPlay("");
}
void onMoveStep(const WebsocketConnectionPtr& conn, const MoveStepPtr& msg)
{
if (state_ != kPlaying || msg->over()) return ;
if (inTaskQueue_)
{
LOG_INFO << "state in onMoveStep: " << state_;
return ;
}
//assert(!inTaskQueue_);
toPlay(msg->fen());
}
void onOppDisconnected(const WebsocketConnectionPtr& conn, const OppDisconnectedPtr&)
{
// do nothing
}
void onOppReconnected(const WebsocketConnectionPtr& conn, const OppReconnectedPtr&)
{
// do nothing
}
void onGameSituation(const WebsocketConnectionPtr& conn, const GameSituationPtr& msg)
{
if (state_ != kDisconnectedInPlaying)
{
LOG_INFO << "state in onGameSituation: " << state_;
}
//assert(state_ == kDisconnectedInPlaying);
state_ = kPlaying;
if (msg->turntome() && !inTaskQueue_)
{
toPlay(msg->fen());
}
}
void onGameOver(const WebsocketConnectionPtr& conn, const GameOverPtr&)
{
int* curPlayPatternTimes = (playedTimes_ % 2 == 0 ?
(data_->quicklyPatternTimes == 0 ?
&data_->slowlyPatternTimes : &data_->quicklyPatternTimes) :
(data_->slowlyPatternTimes == 0 ?
&data_->quicklyPatternTimes : &data_->slowlyPatternTimes));
*curPlayPatternTimes = *curPlayPatternTimes - 1;
++playedTimes_;
state_ = kOnline;
if (getPlayTimes() > 0)
{
data_->reset();
matchTimer_ = loop_->runAfter(data_->intervalToMatchAgain, std::bind(&Client::toMatch, this));
}
else
{
gUserStorage.recycleUsername(username_);
username_.clear();
}
}
void onForgiveRequest(const WebsocketConnectionPtr& conn, const ForgiveRequestPtr&)
{
// do nothing
}
void onForgiveResponse(const WebsocketConnectionPtr& conn, const ForgiveResponsePtr&)
{
// do nothing
}
void onDrawRequest(const WebsocketConnectionPtr& conn, const DrawRequestPtr&)
{
// do nothing
}
void onDrawResponse(const WebsocketConnectionPtr& conn, const DrawResponsePtr&)
{
// do nothing
}
private:
EventLoop* loop_;
WebsocketClient client_;
ProtobufDispatcher dispatcher_;
ProtobufCodec codec_;
std::unique_ptr<RobotData> data_;
int playedTimes_;
State state_;
WebsocketConnectionPtr conn_;
std::string username_;
ThreadPool* pool_;
bool inTaskQueue_;
TimerId loginTimer_;
TimerId matchTimer_;
static const std::string kPasswd;
};
const std::string Client::kPasswd = "123456";
class BenchMark
{
public:
BenchMark(EventLoop* loop, const InetAddress& serverAddr, int nclients)
: loop_(loop),
loopPool_(loop, "BenchMarkPool"),
taskPool_("SearchEngineTaskPool"),
clients_(nclients),
totalPlayTimes_(0)
{
loopPool_.setThreadNum(kThreadNum);
taskPool_.setThreadInitCallback([]
{
gEngine = new Engine;
});
for (int i = 0; i < nclients; ++i)
{
clients_[i].reset(new Client(loopPool_.getNextLoop(), serverAddr, &taskPool_));
totalPlayTimes_ += clients_[i]->getPlayTimes();
}
}
void start()
{
loop_->runEvery(3.0, std::bind(&BenchMark::printer, this));
taskPool_.start(kTaskThreadNum);
loopPool_.start();
std::for_each(clients_.begin(), clients_.end(),
[](const std::unique_ptr<Client>& client)
{
client->connect();
usleep(10);
});
}
private:
void printer()
{
int playedTimes = 0;
int disconnected = 0;
int connected = 0;
int online = 0;
int matching = 0;
int playing = 0;
int disconnectedInPlaying = 0;
std::for_each(clients_.begin(), clients_.end(),
[&](const std::unique_ptr<Client>& client)
{
playedTimes += client->getPlayedTimes();
Client::State state = client->getState();
if (state == Client::kDisconnected)
++disconnected;
else if (state == Client::kConnected)
++connected;
else if (state == Client::kOnline)
++online;
else if (state == Client::kMatching)
++matching;
else if (state == Client::kPlaying)
++playing;
else if (state == Client::kDisconnectedInPlaying)
++disconnectedInPlaying;
});
LOG_INFO << "playedTimes: (" << playedTimes << " / " << totalPlayTimes_
<< "), Disconnected: " << disconnected
<< ", DisconnectedInPlaying: " << disconnectedInPlaying
<< ", Connected: " << connected
<< ", Online: " << online << ", Matching: " << matching
<< ", Playing: " << playing;
}
private:
EventLoop* loop_;
EventLoopThreadPool loopPool_;
ThreadPool taskPool_;
std::vector<std::unique_ptr<Client>> clients_;
int totalPlayTimes_;
static const int kThreadNum;
static const int kTaskThreadNum;
};
const int BenchMark::kThreadNum = 4;
const int BenchMark::kTaskThreadNum = 2;
int main(int argc, char** argv)
{
int clientNum = atoi(argv[1]);
const InetAddress serverAddr("127.0.0.1", 8888);
EventLoop loop;
BenchMark bench(&loop, serverAddr, clientNum);
bench.start();
loop.loop();
}