Exemple #1
0
int http_show_state(int n)
{
    TIMER_SAMPLE(timer);
    if(PT_USEC_U(timer) > 0 && ncompleted > 0)
    {
        if(is_quiet)
        {
            REALLOG(logger, "timeout:%d error:%d total:%d "
                    "time used:%lld request per sec:%lld avg_time:%lld", 
                    ntimeout, nerrors, ncompleted, PT_USEC_U(timer), 
                    ((long long int)ncompleted * 1000000ll/PT_USEC_U(timer)),
                    (PT_USEC_U(timer)/ncompleted));
        }
        else
        {
            fprintf(stdout, "timeout:%d error:%d total:%d\n"
                    "time used:%lld request per sec:%lld avg_time:%lld\n", 
                    ntimeout, nerrors, ncompleted, PT_USEC_U(timer), 
                    ((long long int)ncompleted * 1000000ll/PT_USEC_U(timer)),
                    (PT_USEC_U(timer)/ncompleted));
        }
    }
    if(is_daemon == 0){running_status = 0;sbase->stop(sbase);}
    return 0;
}
Exemple #2
0
int http_show_state(int n)
{
    int nok = 0;
    TIMER_SAMPLE(timer);
    nok = n - ntimeouts;
    if(PT_USEC_U(timer) > 0 && nok  > 0)
    {
        if(is_quiet)
        {
            REALLOG(logger, "timeout:%d error:%d ok:%d total:%d "
                    "time used:%lld request per sec:%lld avg_time:%lld", 
                    ntimeouts, nerrors, nok, n, PT_USEC_U(timer), 
                    (((long long int)nok * 1000000ll)/PT_USEC_U(timer)),
                    (PT_USEC_U(timer)/nok));
        }
        else
        {
            fprintf(stdout, "timeout:%d error:%d ok:%d total:%d\n"
                    "time used:%lld request per sec:%lld avg_time:%lld\n", 
                    ntimeouts, nerrors, nok, n, PT_USEC_U(timer), 
                    ((long long int)nok * 1000000ll/PT_USEC_U(timer)),
                    (PT_USEC_U(timer)/nok));
        }
    }
    running_status = 0;sbase->stop(sbase);
    return 0;
}
Exemple #3
0
int new_request()
{
    int fd = 0, flag = 0, n = 0, opt = 1, prot = 0;
    struct sockaddr_in  lsa;
    socklen_t lsa_len = sizeof(struct sockaddr);

    if(ncompleted > 0 && ncompleted%1000 == 0)
    {
        TIMER_SAMPLE(timer);
        fprintf(stdout, "request:%d completed:%d time:%lld avg:%lld\n", nrequest, ncompleted, PT_USEC_U(timer), (long long int)ncompleted * 1000000ll/PT_USEC_U(timer));
    }
    if(sock_type == SOCK_DGRAM) prot = IPPROTO_UDP;
    if(nrequest < limit && (fd = socket(AF_INET, sock_type, prot)) > 0)
    {
        conns[fd].fd = fd;
        if(is_use_ssl && sock_type == SOCK_STREAM)
        {
            /* Connect */
            if(connect(fd, (struct sockaddr *)&xsa, xsa_len) != 0)
            {
                FATAL_LOG("Connect to %s:%d failed, %s", ip, port, strerror(errno));
                _exit(-1);
            }
#ifdef USE_SSL
            conns[fd].ssl = SSL_new(ctx);
            if(conns[fd].ssl == NULL )
            {
                FATAL_LOG("new SSL with created CTX failed:%s\n",
                        ERR_reason_error_string(ERR_get_error()));
                _exit(-1);
            }
            if(SSL_set_fd(conns[fd].ssl, fd) == 0)
            {
                FATAL_LOG("add SSL to tcp socket failed:%s\n",
                        ERR_reason_error_string(ERR_get_error()));
                _exit(-1);
            }
            /* SSL Connect */
            if(SSL_connect(conns[fd].ssl) < 0)
            {
                FATAL_LOG("SSL connection failed:%s\n",
                        ERR_reason_error_string(ERR_get_error()));
                _exit(-1);
            }
#endif
        }
        /* set FD NON-BLOCK */
        if(sock_type == SOCK_STREAM)
        {
           flag = fcntl(fd, F_GETFL, 0)|O_NONBLOCK;
            fcntl(fd, F_SETFL, flag);
            if(!is_use_ssl)
            {
                /* Connect */
                if(connect(fd, (struct sockaddr *)&xsa, xsa_len) != 0 && errno != EINPROGRESS)
                {
                    FATAL_LOG("Connect to %s:%d failed, %s", ip, port, strerror(errno));
                    _exit(-1);
                }
            }
            event_set(&conns[fd].event, fd, E_READ|E_WRITE|E_PERSIST, 
                    (void *)&(conns[fd].event), &ev_handler);
        }
        else
        {
            memset(&lsa, 0, sizeof(struct sockaddr));
            lsa.sin_family = AF_INET;
            if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, 
                        (char *)&opt, (socklen_t) sizeof(int)) != 0
#ifdef SO_REUSEPORT
                    || setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, 
                        (char *)&opt, (socklen_t) sizeof(int)) != 0
#endif
                    || bind(fd, (struct sockaddr *)&lsa, sizeof(struct sockaddr)) != 0) 
            {
                FATAL_LOG("Bind %d to %s:%d failed, %s",
                        fd, inet_ntoa(lsa.sin_addr), ntohs(lsa.sin_port), strerror(errno));
                close(fd);
                return -1;
            }
            //while(1)sleep(1);
            /* Connect */
            /*
            if(connect(fd, (struct sockaddr *)&xsa, xsa_len) != 0)
            {
                FATAL_LOG("Connect to %s:%d failed, %s", ip, port, strerror(errno));
                _exit(-1);
            }
            */
            getsockname(fd, (struct sockaddr *)&lsa, &lsa_len);
            SHOW_LOG("Connected to remote[%s:%d] local[%s:%d] via %d", ip, port, inet_ntoa(lsa.sin_addr), ntohs(lsa.sin_port), fd);
            n = atoi(ip);
            if(n >= 224 && n <= 239)
            {
                struct ip_mreq mreq;
                memset(&mreq, 0, sizeof(struct ip_mreq));
                mreq.imr_multiaddr.s_addr = inet_addr(ip);
                mreq.imr_interface.s_addr = INADDR_ANY;
                if(setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,(char*)&mreq, sizeof(mreq)) != 0)
                {
                    SHOW_LOG("Setsockopt(MULTICAST) failed, %s", strerror(errno));
                    return -1;
                }
                if(setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &lsa.sin_addr, sizeof(struct in_addr)) < 0)
                {
                    FATAL_LOG("Setsockopt(IP_MULTICAST_IF) failed, %s", strerror(errno));
                    return -1;
                }
            }
            event_set(&conns[fd].event, fd, E_READ|E_WRITE|E_PERSIST, 
                    (void *)&(conns[fd].event), &ev_udp_handler);
        }
        evbase->add(evbase, &(conns[fd].event));
        conns[fd].nresp = 0;
        if(keepalive)
            conns[fd].nreq = sprintf(conns[fd].request, "GET / HTTP/1.0\r\nConnection: Keep-Alive\r\n\r\n");
        else
            conns[fd].nreq = sprintf(conns[fd].request, "GET / HTTP/1.0\r\n\r\n");
    }
    else
    {
        if(ncompleted >= limit) running_status = 0;
    }
    return 0;
}