Commit a8cf6036 authored by captainwong's avatar captainwong

isMachineStatusEvent & bench_client

parent 14606367
...@@ -53,6 +53,7 @@ int thread_count = 0; ...@@ -53,6 +53,7 @@ int thread_count = 0;
int session_count = 0; int session_count = 0;
int session_connected = 0; int session_connected = 0;
int timeout = 0; int timeout = 0;
int load_test = 0;
std::mutex mutex = {}; std::mutex mutex = {};
int64_t totalBytesRead = 0; int64_t totalBytesRead = 0;
...@@ -66,7 +67,7 @@ struct Session { ...@@ -66,7 +67,7 @@ struct Session {
int fd = 0; int fd = 0;
int thread_id = 0; int thread_id = 0;
bufferevent* bev = nullptr; bufferevent* bev = nullptr;
event* timer = nullptr; event* timer = nullptr; // life or heartbeat timer
int id = 0; int id = 0;
std::string acct = {}; std::string acct = {};
AdemcoId ademco_id = 0; AdemcoId ademco_id = 0;
...@@ -105,15 +106,10 @@ void handle_ademco_msg(Session* session, bufferevent* bev) ...@@ -105,15 +106,10 @@ void handle_ademco_msg(Session* session, bufferevent* bev)
auto output = bufferevent_get_output(bev); auto output = bufferevent_get_output(bev);
switch (session->packet.id_.eid_) { switch (session->packet.id_.eid_) {
case AdemcoMsgId::id_ack: case AdemcoMsgId::id_ack:
if (session->packet.seq_.value_ == session->seq) { if (load_test) {
if (++session->seq == 10000) {
session->seq = 1;
}
}
{
auto now = std::chrono::steady_clock::now(); auto now = std::chrono::steady_clock::now();
char buf[1024]; char buf[1024];
session->lastTimePacketSize = session->packet.make_null(buf, sizeof(buf), session->seq, session->acct, session->ademco_id); session->lastTimePacketSize = session->packet.make_null(buf, sizeof(buf), session->nextSeq(), session->acct, session->ademco_id);
evbuffer_add(output, buf, session->lastTimePacketSize); evbuffer_add(output, buf, session->lastTimePacketSize);
auto us = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - now).count(); auto us = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - now).count();
session->encodeTime += us; session->encodeTime += us;
...@@ -168,24 +164,33 @@ void writecb(struct bufferevent* bev, void* user_data) ...@@ -168,24 +164,33 @@ void writecb(struct bufferevent* bev, void* user_data)
session->bytesWritten += session->lastTimePacketSize; session->bytesWritten += session->lastTimePacketSize;
session->msgWritten++; session->msgWritten++;
} }
} }
void timer_cb(evutil_socket_t fd, short what, void* arg) void timer_cb(evutil_socket_t, short, void* arg)
{ {
auto session = (Session*)arg; auto session = (Session*)arg;
//Session* session = nullptr;
//bufferevent_getcb(bev, nullptr, nullptr, nullptr, (void**)&session); if (load_test) {
printf("session #%d timeout\n", session->id); printf("session #%d timeout\n", session->id);
session->timer = nullptr;
//auto bev = (bufferevent*)arg; bufferevent_disable(session->bev, EV_WRITE);
//bufferevent_free(bev); // SHUT_WR
//evutil_closesocket(fd); shutdown(session->fd, 1);
} else {
session->timer = nullptr; printf("session #%d heartbeat\n", session->id);
bufferevent_disable(session->bev, EV_WRITE); session->timer = event_new(threadContexts[session->thread_id]->base, -1, 0, timer_cb, session);
// SHUT_WR if (!session->timer) {
shutdown(session->fd, 1); fprintf(stderr, "create heartbeat_timer failed\n");
event_base_loopbreak(threadContexts[session->thread_id]->base);
return;
}
struct timeval heartbeat_tv = { timeout, 0 };
event_add(session->timer, &heartbeat_tv);
char buf[1024];
session->lastTimePacketSize = session->packet.make_null(buf, sizeof(buf), session->nextSeq(), session->acct, session->ademco_id);
evbuffer_add(bufferevent_get_output(session->bev), buf, session->lastTimePacketSize);
}
} }
void eventcb(struct bufferevent* bev, short events, void* user_data) void eventcb(struct bufferevent* bev, short events, void* user_data)
...@@ -203,9 +208,10 @@ void eventcb(struct bufferevent* bev, short events, void* user_data) ...@@ -203,9 +208,10 @@ void eventcb(struct bufferevent* bev, short events, void* user_data)
} }
} }
char buf[1024]; char buf[1024];
session->lastTimePacketSize = session->packet.make_hb(buf, sizeof(buf), session->nextSeq(), session->acct, session->ademco_id, 0, session->status, 0); session->lastTimePacketSize = session->packet.make_hb(buf, sizeof(buf),
evbuffer_add(bufferevent_get_output(bev), buf, session->lastTimePacketSize); session->nextSeq(), session->acct, session->ademco_id, 0, session->status, 0);
session->lastTimePacketSize += session->packet.make_hb(buf, sizeof(buf), session->nextSeq(), session->acct, session->ademco_id, 0, session->type, 0); session->lastTimePacketSize += session->packet.make_hb(buf + session->lastTimePacketSize, sizeof(buf) - session->lastTimePacketSize,
session->nextSeq(), session->acct, session->ademco_id, 0, session->type, 0);
evbuffer_add(bufferevent_get_output(bev), buf, session->lastTimePacketSize); evbuffer_add(bufferevent_get_output(bev), buf, session->lastTimePacketSize);
auto base = bufferevent_get_base(bev); auto base = bufferevent_get_base(bev);
...@@ -324,8 +330,11 @@ int main(int argc, char** argv) ...@@ -324,8 +330,11 @@ int main(int argc, char** argv)
} }
#endif #endif
if (argc < 6) { if (argc < 7) {
printf("Usage: %s ip port thread_count session_count timeout\ntimeout is in seconds\n", argv[0]); printf("Usage: %s ip port thread_count session_count load_test timeout\n"
" load_test: 0 or 1:\n"
" 0 to disable load test mode, timeout is client's heartbeat timeout in seconds\n"
" 1 to enable load test mode, timeout is client's lifecycle timeout in seconds\n", argv[0]);
return 1; return 1;
} }
...@@ -353,8 +362,8 @@ int main(int argc, char** argv) ...@@ -353,8 +362,8 @@ int main(int argc, char** argv)
puts("session_count must times thread_count"); puts("session_count must times thread_count");
return 1; return 1;
} }
load_test = atoi(argv[5]) == 1;
timeout = atoi(argv[5]); timeout = atoi(argv[6]);
if (timeout <= 0) { if (timeout <= 0) {
puts("Invalid timeout"); puts("Invalid timeout");
return 1; return 1;
...@@ -362,8 +371,8 @@ int main(int argc, char** argv) ...@@ -362,8 +371,8 @@ int main(int argc, char** argv)
printf("using libevent %s\n", event_get_version()); printf("using libevent %s\n", event_get_version());
int session_per_thread = session_count / thread_count; int session_per_thread = session_count / thread_count;
printf("starting %s to %s:%d with %d threads, %d sessions, %d sessions per thread, timeout=%ds\n", printf("starting %s to %s:%d with %d threads, %d sessions, %d sessions per thread, load_test is %s, timeout=%ds\n",
argv[0], ip, port, thread_count, session_count, session_per_thread, timeout); argv[0], ip, port, thread_count, session_count, session_per_thread, load_test ? "on" : "off", timeout);
sockaddr_in sin = { 0 }; sockaddr_in sin = { 0 };
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LocalDebuggerCommandArguments>192.168.1.90 12345 1 20 30</LocalDebuggerCommandArguments> <LocalDebuggerCommandArguments>192.168.1.90 12345 1 20 0 30 </LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor> <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
......
...@@ -359,6 +359,7 @@ static inline bool isMachineStatusEvent(ADEMCO_EVENT ademco_event) ...@@ -359,6 +359,7 @@ static inline bool isMachineStatusEvent(ADEMCO_EVENT ademco_event)
{ {
return ademco_event == EVENT_ARM return ademco_event == EVENT_ARM
|| ademco_event == EVENT_HALFARM || ademco_event == EVENT_HALFARM
|| ademco_event == EVENT_HALFARM_1456
|| ademco_event == EVENT_DISARM; || ademco_event == EVENT_DISARM;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment