From 86a23c6beb1d0f8f76021d69054b543aec974e64 Mon Sep 17 00:00:00 2001 From: Will Date: Sun, 7 Apr 2013 14:37:24 +0900 Subject: [PATCH] stop after receiving SIGINT --- Makefile | 2 +- src/wrk.c | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index b63bf0e..19066f9 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ LIBS := -lpthread -lm TARGET := $(shell uname -s | tr [A-Z] [a-z] 2>/dev/null || echo unknown) ifeq ($(TARGET), sunos) - CFLAGS += -D_PTHREADS + CFLAGS += -D_PTHREADS -D_POSIX_C_SOURCE=200112L LIBS += -lsocket endif diff --git a/src/wrk.c b/src/wrk.c index e103b00..90ba3d4 100644 --- a/src/wrk.c +++ b/src/wrk.c @@ -51,6 +51,12 @@ static const struct http_parser_settings parser_settings = { .on_message_complete = request_complete }; +static volatile sig_atomic_t stop = 0; + +static void handler(int sig) { + stop = 1; +} + static void usage() { printf("Usage: wrk \n" " Options: \n" @@ -118,6 +124,7 @@ int main(int argc, char **argv) { } signal(SIGPIPE, SIG_IGN); + signal(SIGINT, SIG_IGN); cfg.addr = *addr; request.buf = format_request(host, port, path, headers); request.size = strlen(request.buf); @@ -142,6 +149,13 @@ int main(int argc, char **argv) { } } + struct sigaction sa = { + .sa_handler = handler, + .sa_flags = 0, + }; + sigfillset(&sa.sa_mask); + sigaction(SIGINT, &sa, NULL); + printf("Making %"PRIu64" requests to %s\n", cfg.requests, url); printf(" %"PRIu64" threads and %"PRIu64" connections\n", cfg.threads, cfg.connections); @@ -323,7 +337,7 @@ static int check_timeouts(aeEventLoop *loop, long long id, void *data) { errors += thread->errors.write; errors += thread->errors.timeout; - if (errors >= cfg.errors) { + if (stop || errors >= cfg.errors) { aeStop(loop); }