Example #1
0
int main(int argc,char**argv)
{
    int i,listen_fd;
    struct sigaction sig_action;

    ParseArgs(argc,argv);

    //daemonize();

    sig_action.sa_handler=SignalHandler;
    sig_action.sa_flags=SA_RESTART;
    sigemptyset(&sig_action.sa_mask);
    sigaction(SIGINT,&sig_action,NULL);
    sigaction(SIGTERM,&sig_action,NULL);
    sigaction(SIGPIPE,&sig_action,NULL);


    if((policy->listen.addr==NULL)||!strcmp(policy->listen.addr,"any")){
	free(policy->listen.addr);
	policy->listen.addr=strdup("0.0.0.0");
    }else if (!strcmp(policy->listen.addr,"localhost")){
	free(policy->listen.addr);
	policy->listen.addr=strdup("127.0.0.1");
    }

    listen_fd=anetTcpServer(anet_error,policy->listen.port,policy->listen.addr);
    el=aeCreateEventLoop(1024);

    if(listen_fd<0||aeCreateFileEvent(el,listen_fd,AE_READABLE,Proxy_AcceptTcpHandler,NULL)==AE_ERR){
	LogFatal("listen failed:%s",strerror(errno));
    }

    LogInfo("listening on:%s:%d",(policy->listen.addr?policy->listen.addr:"any"),policy->listen.port);
    for(i=0;i<policy->nhost;i++){
	LogInfo("proxy to %s:%d",policy->hosts[i].addr,policy->hosts[i].port);
    }

    aeMain(el);
    aeDeleteEventLoop(el);
    FreePolicy(policy);
    return 0;
}
Example #2
0
int main(int argc, char **argv) {
	signal(SIGPIPE, SIG_IGN);

	redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
	if (c->err) {
		/* Let *c leak for now... */
		printf("Error: %s\n", c->errstr);
		return 1;
	}

	loop = aeCreateEventLoop(64);
	redisAeAttach(loop, c);
	redisAsyncSetConnectCallback(c, connectCallback);
	redisAsyncSetDisconnectCallback(c, disconnectCallback);
	redisAsyncCommand(c, NULL, NULL, "SET key %b", argv[argc - 1],
			strlen(argv[argc - 1]));
	redisAsyncCommand(c, getCallback, (char*) "end-1", "GET key");
	aeMain(loop);
	return 0;
}
Example #3
0
void initServer( appnetServer *serv )
{
	
	serv->connlist = shm_calloc( serv->connect_max , sizeof(appnetConnection) );
	
	serv->reactor_threads =
			zmalloc( serv->reactor_num * sizeof(appnetReactorThread) );
	
	serv->workers = zmalloc( ( serv->worker_num + serv->task_worker_num ) *
			sizeof(appnetWorkerProcess) );
	
	serv->worker_pipes = zmalloc( ( serv->worker_num + serv->task_worker_num ) *
			serv->reactor_num * sizeof(appnetWorkerPipes) );
	
	srand( (unsigned) time( NULL ) );
	serv->main_reactor = zmalloc( sizeof(appnetReactor) );
	serv->main_reactor->event_loop = aeCreateEventLoop( 10 );
	
	aeSetBeforeSleepProc( serv->main_reactor->event_loop , initOnLoopStart );
	installMasterSignal( serv );
}
Example #4
0
int main(int argc, char **argv)
{
	(void) argc;
	(void) argv;

	_svr.bindaddr = HOST;
	_svr.port = PORT;

	_svr.db = silokatana_open();
	_svr.el = aeCreateEventLoop(11024);
	_svr.fd = anetTcpServer(_svr.neterr, _svr.port, _svr.bindaddr);
	if (_svr.fd == ANET_ERR) {
		__PANIC("openning port error #%d:%s", _svr.port, _svr.neterr);
		exit(1);
	}

	if (anetNonBlock(_svr.neterr, _svr.fd) == ANET_ERR) {
		__ERROR("set nonblock #%s",_svr.neterr);
		exit(1);
	}

	aeCreateTimeEvent(_svr.el, 3000, server_cron, NULL, NULL);

	if (aeCreateFileEvent(_svr.el, _svr.fd, AE_READABLE, accept_handler, NULL) == AE_ERR) 
		__ERROR("creating file event");

	__INFO("siloserver starting, port:%d, pid:%d", PORT, (long)getpid());
	printf("%s", _ascii_logo);

	aeMain(_svr.el);

	__ERROR("oops,exit");
	aeDeleteEventLoop(_svr.el);
	silokatana_close(_svr.db);

	return 1;
}
Example #5
0
    int GatewayDataMgr::startRedisCliProcLoop() {
        int iRet = 0;
        
        //init redis script sha1
        sha1sumScript();

        mPtRedisAC = redisAsyncConnect(mRedisSvrIPAddr.c_str(), mPort);
        if (mPtRedisAC->err) {
            /* Let *c leak for now... */
            LOG(ERROR)<<"Error: "<<mPtRedisAC->errstr;
            iRet = -1;
            return iRet;
        }

        mPtEventLoop = aeCreateEventLoop(64);
        mPtRedisAC->data = mPtEventLoop;//used on connect/disconnect callback
        redisAeAttach(mPtEventLoop, mPtRedisAC);
        redisAsyncSetConnectCallback(mPtRedisAC, connectCallback);
        redisAsyncSetDisconnectCallback(mPtRedisAC,disconnectCallback);

        std::thread loopThread(std::mem_fn(&GatewayDataMgr::loopThread), this);
        loopThread.detach();
        return iRet;
    } 
int main(int argc, const char **argv) {
    int i;
    char *data, *cmd;
    int len;

    client c;

    signal(SIGHUP, SIG_IGN);
    signal(SIGPIPE, SIG_IGN);

    config.numclients = 50;
    config.requests = 10000;
    config.liveclients = 0;
    config.el = aeCreateEventLoop(1024*10);
    aeCreateTimeEvent(config.el,1,showThroughput,NULL,NULL);
    config.keepalive = 1;
    config.datasize = 3;
    config.pipeline = 1;
    config.randomkeys = 0;
    config.randomkeys_keyspacelen = 0;
    config.quiet = 0;
    config.csv = 0;
    config.loop = 0;
    config.idlemode = 0;
    config.latency = NULL;
    config.clients = listCreate();
    config.hostip = "127.0.0.1";
    config.hostport = 6379;
    config.hostsocket = NULL;
    config.tests = NULL;
    config.dbnum = 0;

    i = parseOptions(argc,argv);
    argc -= i;
    argv += i;

//    printf ("SET PAGE POOL SIZE\n");
//    set_page_pool_size(config.requests);
    config.latency = zmalloc(sizeof(long long)*config.requests);

    if (config.keepalive == 0) {
        printf("WARNING: keepalive disabled, you probably need 'echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse' for Linux and 'sudo sysctl -w net.inet.tcp.msl=1000' for Mac OS X in order to use a lot of clients/requests\n");
    }

    if (config.idlemode) {
        printf("Creating %d idle connections and waiting forever (Ctrl+C when done)\n", config.numclients);
        c = createClient("",0,NULL); /* will never receive a reply */
        createMissingClients(c);
        aeMain(config.el);
        /* and will wait for every */
    }

    /* Run benchmark with command in the remainder of the arguments. */
    if (argc) {
        sds title = sdsnew(argv[0]);
        for (i = 1; i < argc; i++) {
            title = sdscatlen(title, " ", 1);
            title = sdscatlen(title, (char*)argv[i], strlen(argv[i]));
        }

        do {
            len = redisFormatCommandArgv(&cmd,argc,argv,NULL);
            benchmark(title,cmd,len);
            free(cmd);
        } while(config.loop);

        return 0;
    }

    /* Run default benchmark suite. */
    do {
        data = zmalloc(config.datasize+1);
        memset(data,'x',config.datasize);
        data[config.datasize] = '\0';

        if (test_is_selected("ping_inline") || test_is_selected("ping"))
            benchmark("PING_INLINE","PING\r\n",6);

        if (test_is_selected("ping_mbulk") || test_is_selected("ping")) {
            len = redisFormatCommand(&cmd,"PING");
            benchmark("PING_BULK",cmd,len);
            free(cmd);
        }

        if (test_is_selected("set")) {
            len = redisFormatCommand(&cmd,"SET key:__rand_int__ %s",data);
            benchmark("SET",cmd,len);
            free(cmd);
        }

        if (test_is_selected("get")) {
            len = redisFormatCommand(&cmd,"GET key:__rand_int__");
            benchmark("GET",cmd,len);
            free(cmd);
        }

        if (test_is_selected("incr")) {
            len = redisFormatCommand(&cmd,"INCR counter:__rand_int__");
            benchmark("INCR",cmd,len);
            free(cmd);
        }

        if (test_is_selected("lpush")) {
            len = redisFormatCommand(&cmd,"LPUSH mylist %s",data);
            benchmark("LPUSH",cmd,len);
            free(cmd);
        }

        if (test_is_selected("lpop")) {
            len = redisFormatCommand(&cmd,"LPOP mylist");
            benchmark("LPOP",cmd,len);
            free(cmd);
        }

        if (test_is_selected("sadd")) {
            len = redisFormatCommand(&cmd,
                "SADD myset element:__rand_int__");
            benchmark("SADD",cmd,len);
            free(cmd);
        }

        if (test_is_selected("spop")) {
            len = redisFormatCommand(&cmd,"SPOP myset");
            benchmark("SPOP",cmd,len);
            free(cmd);
        }

        if (test_is_selected("lrange") ||
            test_is_selected("lrange_100") ||
            test_is_selected("lrange_300") ||
            test_is_selected("lrange_500") ||
            test_is_selected("lrange_600"))
        {
            len = redisFormatCommand(&cmd,"LPUSH mylist %s",data);
            benchmark("LPUSH (needed to benchmark LRANGE)",cmd,len);
            free(cmd);
        }

        if (test_is_selected("lrange") || test_is_selected("lrange_100")) {
            len = redisFormatCommand(&cmd,"LRANGE mylist 0 99");
            benchmark("LRANGE_100 (first 100 elements)",cmd,len);
            free(cmd);
        }

        if (test_is_selected("lrange") || test_is_selected("lrange_300")) {
            len = redisFormatCommand(&cmd,"LRANGE mylist 0 299");
            benchmark("LRANGE_300 (first 300 elements)",cmd,len);
            free(cmd);
        }

        if (test_is_selected("lrange") || test_is_selected("lrange_500")) {
            len = redisFormatCommand(&cmd,"LRANGE mylist 0 449");
            benchmark("LRANGE_500 (first 450 elements)",cmd,len);
            free(cmd);
        }

        if (test_is_selected("lrange") || test_is_selected("lrange_600")) {
            len = redisFormatCommand(&cmd,"LRANGE mylist 0 599");
            benchmark("LRANGE_600 (first 600 elements)",cmd,len);
            free(cmd);
        }

        if (test_is_selected("mset")) {
            const char *argv[21];
            argv[0] = "MSET";
            for (i = 1; i < 21; i += 2) {
                argv[i] = "key:__rand_int__";
                argv[i+1] = data;
            }
            len = redisFormatCommandArgv(&cmd,21,argv,NULL);
            benchmark("MSET (10 keys)",cmd,len);
            free(cmd);
        }

        if (!config.csv) printf("\n");
    } while(config.loop);

    return 0;
}
Example #7
0
int main(int argc, char **argv)
{
    config.daemonize = 0;
    config.logserver = "minard";
    config.dataserver = "192.168.80.100";//"192.168.80.1";
    config.logfile = "";
    config.loglevel = NOTICE;

    parseOptions(argc, argv);

    strcpy(logfile, config.logfile);
    verbosity = config.loglevel;

    if (config.daemonize) daemonize();

    signal(SIGPIPE, SIG_IGN);
    signal(SIGINT, sigint_handler);

    Log(NOTICE, "tubii server started");

    el = aeCreateEventLoop(100);

    if ((aeCreateTimeEvent(el, 0, printSkipped, NULL, NULL)) == AE_ERR) {
        LogRaw(WARNING, "failed to set up printSkipped()");
    }

    startLogServer(config.logserver, "tubii");

    initServer(el, 4001, commandTable, sizeof(commandTable)/sizeof(struct command));

    /* set up the dispatch_connect event which will try to connect to the
     * data stream server. If it can't connect, it will retry every 10
     * seconds. */
    if (data_connect(config.dataserver)) {
        Log(WARNING, "failed to set up data stream");
        return 1;
    }

    auto_init();

    /* start tubii readout */
    if (start_tubii_readout(1000)) {
        //Log(WARNING, tubii_err);
        return 1;
    }

    /* set up status event */
    if (aeCreateTimeEvent(el, 0, tubii_status, NULL, NULL) == AE_ERR) {
        Log(WARNING, "failed to set up status tubii");
        return 1;
    }

    /* enter the main event loop */
    el->stop = 0;
    while (!el->stop) {
        if (el->beforesleep != NULL)
            el->beforesleep(el);
        aeProcessEvents(el, AE_ALL_EVENTS);
    }

    Log(NOTICE, "ctrl-c caught. flushing buffers...");

    time_t now = time(NULL);
    while (time(NULL) < now + 1) {
        if (aeProcessEvents(el, AE_FILE_EVENTS | AE_DONT_WAIT) == 0) break;
    }

    aeDeleteEventLoop(el);

    return 0;
}
Example #8
0
void mx_init_daemon()
{
    struct linger ling = {0, 0};
    struct sockaddr_in addr;
    int flags = 1;

    mx_daemon->log_fd = fopen(mx_daemon->log_file, "a+");
    if (!mx_daemon->log_file) {
        fprintf(stderr, "[failed] failed to open log file\n");
        exit(-1);
    }

    mx_daemon->fd = socket(AF_INET, SOCK_STREAM, 0);
    if (mx_daemon->fd == -1) {
        mx_write_log(mx_log_error, "Unable create listening server socket");
        exit(-1);
    }

    if (mx_set_nonblocking(mx_daemon->fd) == -1) {
        mx_write_log(mx_log_error, "Unable set socket to non-blocking");
        exit(-1);
    }

    setsockopt(mx_daemon->fd, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags));
    setsockopt(mx_daemon->fd, SOL_SOCKET, SO_KEEPALIVE, &flags, sizeof(flags));
    setsockopt(mx_daemon->fd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling));
#if !defined(TCP_NOPUSH)
    setsockopt(mx_daemon->fd, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof(flags));
#endif

    addr.sin_family = AF_INET;
    addr.sin_port = htons(mx_daemon->port);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);

    if (bind(mx_daemon->fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
        mx_write_log(mx_log_error, "Unable bind socket");
        close(mx_daemon->fd);
        exit(-1);
    }

    if (listen(mx_daemon->fd, 1024) == -1) {
        mx_write_log(mx_log_error, "Unable listen socket");
        close(mx_daemon->fd);
        exit(-1);
    }

    mx_daemon->event = aeCreateEventLoop();
    if (!mx_daemon->event) {
        mx_write_log(mx_log_error, "Unable create EventLoop");
        exit(-1);
    }

    mx_daemon->table = hash_alloc(32);
    if (!mx_daemon->table) {
        mx_write_log(mx_log_error, "Unable create HashTable");
        exit(-1);
    }

    mx_daemon->delay_queue = mx_queue_create("__delay__", sizeof("__delay__") - 1);
    if (!mx_daemon->table) {
        mx_write_log(mx_log_error, "Unable create delay queue");
        exit(-1);
    }
    
    mx_daemon->recycle = mx_recycle_create();
    if (!mx_daemon->recycle) {
        mx_write_log(mx_log_error, "Unable create recycle");
        exit(-1);
    }

    if (aeCreateFileEvent(mx_daemon->event, mx_daemon->fd,
            AE_READABLE, mx_accept_connection, NULL) == -1) {
        mx_write_log(mx_log_error, "Unable create accpet file event");
        exit(-1);
    }

    aeCreateTimeEvent(mx_daemon->event, 1, mx_core_timer, NULL, NULL);

    time(&mx_current_time);

    return;
}
Example #9
0
int eventloopInit(int setsize) {
	M = (struct eventloop *)zmalloc(sizeof(*M));
	M->eventloop = aeCreateEventLoop(setsize);
	M->thread = threadCreate(run, "eventloop");
	return 0;
}
Example #10
0
int main(int argc, const char **argv) {
    int i;
    char *data, *cmd;
    int len;

    client c;

#ifdef _WIN32
    w32initWinSock();
#endif

    signal(SIGHUP, SIG_IGN);
    signal(SIGPIPE, SIG_IGN);

    config.numclients = 50;
    config.requests = 10000;
    config.liveclients = 0;
    config.el = aeCreateEventLoop();
    aeCreateTimeEvent(config.el,1,showThroughput,NULL,NULL);
    config.keepalive = 1;
    config.datasize = 3;
    config.randomkeys = 0;
    config.randomkeys_keyspacelen = 0;
    config.quiet = 0;
    config.loop = 0;
    config.idlemode = 0;
    config.latency = NULL;
    config.clients = listCreate();
    config.hostip = "127.0.0.1";
    config.hostport = 6379;
    config.hostsocket = NULL;

    i = parseOptions(argc,argv);
    argc -= i;
    argv += i;

    config.latency = zmalloc(sizeof(long long)*config.requests);

    if (config.keepalive == 0) {
        printf("WARNING: keepalive disabled, you probably need 'echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse' for Linux and 'sudo sysctl -w net.inet.tcp.msl=1000' for Mac OS X in order to use a lot of clients/requests\n");
    }

    if (config.idlemode) {
        printf("Creating %d idle connections and waiting forever (Ctrl+C when done)\n", config.numclients);
        c = createClient("",0); /* will never receive a reply */
        createMissingClients(c);
        aeMain(config.el);
        /* and will wait for every */
    }

    /* Run benchmark with command in the remainder of the arguments. */
    if (argc) {
        sds title = sdsnew(argv[0]);
        for (i = 1; i < argc; i++) {
            title = sdscatlen(title, " ", 1);
            title = sdscatlen(title, (char*)argv[i], strlen(argv[i]));
        }

        do {
            len = redisFormatCommandArgv(&cmd,argc,argv,NULL);
            benchmark(title,cmd,len);
            free(cmd);
        } while(config.loop);

        return 0;
    }

    /* Run default benchmark suite. */
    do {
        data = zmalloc(config.datasize+1);
        memset(data,'x',config.datasize);
        data[config.datasize] = '\0';

        benchmark("PING (inline)","PING\r\n",6);

        len = redisFormatCommand(&cmd,"PING");
        benchmark("PING",cmd,len);
        free(cmd);

        const char *argv[21];
        argv[0] = "MSET";
        for (i = 1; i < 21; i += 2) {
            argv[i] = "foo:rand:000000000000";
            argv[i+1] = data;
        }
        len = redisFormatCommandArgv(&cmd,21,argv,NULL);
        benchmark("MSET (10 keys)",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"SET foo:rand:000000000000 %s",data);
        benchmark("SET",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"GET foo:rand:000000000000");
        benchmark("GET",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"INCR counter:rand:000000000000");
        benchmark("INCR",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"LPUSH mylist %s",data);
        benchmark("LPUSH",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"LPOP mylist");
        benchmark("LPOP",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"SADD myset counter:rand:000000000000");
        benchmark("SADD",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"SPOP myset");
        benchmark("SPOP",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"LPUSH mylist %s",data);
        benchmark("LPUSH (again, in order to bench LRANGE)",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"LRANGE mylist 0 99");
        benchmark("LRANGE (first 100 elements)",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"LRANGE mylist 0 299");
        benchmark("LRANGE (first 300 elements)",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"LRANGE mylist 0 449");
        benchmark("LRANGE (first 450 elements)",cmd,len);
        free(cmd);

        len = redisFormatCommand(&cmd,"LRANGE mylist 0 599");
        benchmark("LRANGE (first 600 elements)",cmd,len);
        free(cmd);

        printf("\n");
    } while(config.loop);

#ifdef _WIN32
    WSACleanup();
#endif
    return 0;
}
Example #11
0
int main(int argc, char **argv) {
    struct addrinfo *addrs, *addr;
    char *host = "127.0.0.1";
    char *port = "1337";
    int rc;
    // for checking that the server's up
    char poke[SHA_LENGTH * 2];
    tinymt64_t rando;

    if (parse_args(&cfg, &host, &port, argc, argv)) {
        usage();
        exit(1);
    }

    struct addrinfo hints = {
        .ai_family   = AF_UNSPEC,
        .ai_socktype = SOCK_STREAM
    };

    if ((rc = getaddrinfo(host, port, &hints, &addrs)) != 0) {
        const char *msg = gai_strerror(rc);
        fprintf(stderr, "unable to resolve %s:%s: %s\n", host, port, msg);
        exit(1);
    }

    for (addr = addrs; addr != NULL; addr = addr->ai_next) {
        int fd = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
        if (fd == -1) continue;
        rc = connect(fd, addr->ai_addr, addr->ai_addrlen);
        tinymt64_init(&rando, time_us());
        random_hash(&rando, poke);
        if (rc == 0 && write(fd, poke, SHA_LENGTH * 2) == SHA_LENGTH * 2) {
            read(fd, poke, SHA_LENGTH * 2);
            close(fd);
            break;
        }
        close(fd);
    }

    if (addr == NULL) {
        char *msg = strerror(errno);
        fprintf(stderr, "unable to connect to %s:%s: %s\n", host, port, msg);
        exit(1);
    }

    signal(SIGPIPE, SIG_IGN);
    signal(SIGINT,  SIG_IGN);
    cfg.addr = *addr;

    pthread_mutex_init(&statistics.mutex, NULL);
    statistics.latency  = stats_alloc(SAMPLES);
    statistics.requests = stats_alloc(SAMPLES);

    thread *threads = zcalloc(cfg.threads * sizeof(thread));
    uint64_t connections = cfg.connections / cfg.threads;
    uint64_t stop_at     = time_us() + (cfg.duration * 1000000);

    for (uint64_t i = 0; i < cfg.threads; i++) {
        thread *t = &threads[i];
        t->connections = connections;
        t->stop_at     = stop_at;

        if (pthread_create(&t->thread, NULL, &thread_main, t)) {
            char *msg = strerror(errno);
            fprintf(stderr, "unable to create thread %"PRIu64" %s\n", i, msg);
            exit(2);
        }
    }

    struct sigaction sa = {
        .sa_handler = handler,
        .sa_flags   = 0,
    };
    sigfillset(&sa.sa_mask);
    sigaction(SIGINT, &sa, NULL);

    char *time = format_time_s(cfg.duration);
    printf("Running %s test @ %s:%s\n", time, host, port);
    printf("  %"PRIu64" threads and %"PRIu64" connections\n", cfg.threads, cfg.connections);

    uint64_t start    = time_us();
    uint64_t complete = 0;
    uint64_t bytes    = 0;
    errors errors     = { 0 };

    for (uint64_t i = 0; i < cfg.threads; i++) {
        thread *t = &threads[i];
        pthread_join(t->thread, NULL);

        complete += t->complete;
        bytes    += t->bytes;

        errors.connect   += t->errors.connect;
        errors.handshake += t->errors.handshake;
        errors.read      += t->errors.read;
        errors.validate  += t->errors.validate;
        errors.write     += t->errors.write;
        errors.timeout   += t->errors.timeout;
    }

    uint64_t runtime_us = time_us() - start;
    long double runtime_s   = runtime_us / 1000000.0;
    long double req_per_s   = complete   / runtime_s;
    long double bytes_per_s = bytes      / runtime_s;

    print_stats_header();
    print_stats("Latency", statistics.latency, format_time_us);
    print_stats("Req/Sec", statistics.requests, format_metric);
    if (cfg.latency) print_stats_latency(statistics.latency);

    char *runtime_msg = format_time_us(runtime_us);

    printf("  %"PRIu64" requests in %s, %sB read\n", complete, runtime_msg, format_binary(bytes));
    if (errors.connect || errors.read || errors.write || errors.timeout) {
        printf("  Socket errors: connect %d, read %d, write %d, timeout %d\n",
               errors.connect, errors.read, errors.write, errors.timeout);
    }

    if (errors.handshake) {
        printf("  Bad handshakes from server: %d\n", errors.handshake);
    }

    if (errors.validate) {
        printf("  %d proofs failed verification.\n", errors.validate);
    }

    printf("Requests/sec: %9.2Lf\n", req_per_s);
    printf("Transfer/sec: %10sB\n", format_binary(bytes_per_s));

    return 0;
}

void *thread_main(void *arg) {
    thread *thread = arg;

    aeEventLoop *loop = aeCreateEventLoop(10 + cfg.connections * 3);
    thread->cs   = zmalloc(thread->connections * sizeof(connection));
    thread->loop = loop;
    tinymt64_init(&thread->rand, time_us());
    thread->latency = stats_alloc(100000);

    connection *c = thread->cs;

    for (uint64_t i = 0; i < thread->connections; i++, c++) {
        c->thread = thread;
        random_hash(&thread->rand, c->hash);
        connect_socket(thread, c);
    }

    aeCreateTimeEvent(loop, CALIBRATE_DELAY_MS, calibrate, thread, NULL);
    aeCreateTimeEvent(loop, TIMEOUT_INTERVAL_MS, check_timeouts, thread, NULL);

    thread->start = time_us();
    aeMain(loop);

    aeDeleteEventLoop(loop);
    zfree(thread->cs);

    uint64_t max = thread->latency->max;
    stats_free(thread->latency);

    pthread_mutex_lock(&statistics.mutex);
    for (uint64_t i = 0; i < thread->missed; i++) {
        stats_record(statistics.latency, max);
    }
    pthread_mutex_unlock(&statistics.mutex);

    return NULL;
}
Example #12
0
int main(int argc, char **argv) {
    int i = 0;

    signal(SIGHUP, SIG_IGN);
    signal(SIGPIPE, SIG_IGN);

    config.el = aeCreateEventLoop();
    config.debug = 0;
    config.done = 0;
    config.clients = listCreate();
    config.num_clients = 50;
    config.num_requests = 10000;
    config.issued_requests = 0;

    config.keepalive = 1;
    config.set_perc = 50;
    config.del_perc = 0;
    config.swapin_perc = 0;
    config.lpush_perc = 0;
    config.lpop_perc = 0;
    config.hset_perc = 0;
    config.hget_perc = 0;
    config.hgetall_perc = 0;
    config.datasize_min = 1;
    config.datasize_max = 64;
    config.keyspace = DEFAULT_KEYSPACE; /* 100k */
    config.hashkeyspace = DEFAULT_HASHKEYSPACE; /* 1k */
    config.check = 0;
    config.rand = 0;
    config.longtail = 0;
    config.quiet = 0;
    config.loop = 0;
    config.idlemode = 0;
    config.latency = NULL;
    config.latency = zmalloc(sizeof(int)*(MAX_LATENCY+1));
    config.ctrlc = 0;
    config.prngseed = (unsigned int) (microseconds()^getpid());

    config.hostip = "127.0.0.1";
    config.hostport = 6379;

    parseOptions(argc,argv);
    config.databuf = zmalloc(config.datasize_max);

    if (config.keepalive == 0) {
        printf("WARNING: keepalive disabled, you probably need 'echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse' for Linux and 'sudo sysctl -w net.inet.tcp.msl=1000' for Mac OS X in order to use a lot of clients/requests\n");
    }

    if (config.idlemode) {
        printf("Creating %d idle connections and waiting forever (Ctrl+C when done)\n", config.num_clients);
        memset(config.optab,REDIS_IDLE,100);
    } else {
        /* Setup the operation table. Start with a table with just GET
         * operations and overwrite it with others as needed. */
        memset(config.optab,REDIS_GET,100);
        fillOpTab(&i,REDIS_SET,config.set_perc);
        fillOpTab(&i,REDIS_DEL,config.del_perc);
        fillOpTab(&i,REDIS_LPUSH,config.lpush_perc);
        fillOpTab(&i,REDIS_LPOP,config.lpop_perc);
        fillOpTab(&i,REDIS_HSET,config.hset_perc);
        fillOpTab(&i,REDIS_HGET,config.hget_perc);
        fillOpTab(&i,REDIS_HGETALL,config.hgetall_perc);
        fillOpTab(&i,REDIS_SWAPIN,config.swapin_perc);
    }

    signal(SIGINT,ctrlc);
    srandom(config.prngseed);
    printf("PRNG seed is: %u - use the 'seed' option to reproduce the same sequence\n", config.prngseed);
    do {
        prepareForBenchmark();
        createMissingClients();
        aeMain(config.el);
        endBenchmark();
    } while(config.loop);

    return 0;
}
Example #13
0
static int dispatch_data_thread_init(dispatch_data_thread *ddt, char *test_target_groups, int connections)
{
    int i, j, k;

    ddt->id = 0;
    ddt->thread_id = 0;
    ddt->el = NULL;
    ddt->hz = 10;
    ddt->cronloops = 0;
    ddt->datas = NULL;
    ddt->rdatas = NULL;
    ddt->abgs = NULL;
    ddt->pause = 0;
    ddt->count_wait_for_reply = 0;
    ddt->reply_total_count_per_cycle = 0;
    ddt->reply_type_err_count_per_cycle = 0;

    ddt->el = aeCreateEventLoop(200);
    if (ddt->el == NULL) {
        return VRT_ERROR;
    }

    ddt->datas = dmtqueue_create();
    if (ddt->datas == NULL) {
        return VRT_ERROR;
    }

    if (dmtqueue_init_with_lockqueue(ddt->datas, NULL) != 0) {
        return VRT_ERROR;
    }

    ddt->rdatas = dlistCreate();
    if (ddt->rdatas == NULL) {
        return VRT_ERROR;
    }
    
    ddt->abgs = abtest_groups_create(test_target_groups);
    if (ddt->abgs == NULL) {
        return VRT_ERROR;
    }

    /* Init connection context for each server */
    for (i = 0; i < darray_n(ddt->abgs); i ++) {
        abtest_group *abg = darray_get(ddt->abgs, i);
        for (j = 0; j < darray_n(&abg->abtest_servers); j ++) {
            abtest_server *abs = darray_get(&abg->abtest_servers, j);
            abs->conn_contexts = darray_create(connections, sizeof(conn_context));
            for (k = 0; k < connections; k ++) {
                conn_context *cc = darray_push(abs->conn_contexts);
                if (dispatch_conn_context_init(cc,abs->host,abs->port) != VRT_OK) {
                    return VRT_ERROR;
                }
                cc->actx->data = ddt;
                redisAeAttach(ddt->el, cc->actx);
                redisAsyncSetConnectCallback(cc->actx,connect_callback);
                redisAsyncSetDisconnectCallback(cc->actx,disconnect_callback);
            }
        }
    }

    if (aeCreateTimeEvent(ddt->el, 1, dispatch_data_thread_cron, ddt, NULL) == AE_ERR) {
        return VRT_ERROR;
    }
    
    return VRT_OK;
}