'Fail to create echo-server benchmark program (Non-pair "send" and "recv" )

I tried to create a program that measure the performance of echo-server. I implement it by creating several threads (like concurrent connections), sending buffer and waiting for echo from server. Following is client side code:

#include <arpa/inet.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#define TARGET_HOST "127.0.0.1"
#define TARGET_PORT 12345
#define BENCH_COUNT 10
#define BENCHMARK_RESULT_FILE "bench.txt"

/* length of unique message (TODO below) should shorter than this */
#define MAX_MSG_LEN 1000
#define MAX_THREAD 100

static char *msg;

static pthread_t pt[MAX_THREAD];

/* block all workers before they are all ready to benchmarking kecho */
static bool ready;
static bool shut;

static pthread_mutex_t res_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t worker_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t worker_wait = PTHREAD_COND_INITIALIZER;

unsigned long long total_success = 0;

static void init(){
    msg = (char*) malloc(sizeof(char) * MAX_MSG_LEN);
    for(int i = 0; i < MAX_MSG_LEN; i++){
        msg[i] = 'a';
    }
    msg[MAX_MSG_LEN] = '\0';
}

static void *bench_worker(__attribute__((unused)))
{
    int sock_fd;
    char dummy[MAX_MSG_LEN];
    unsigned long long local_success = 0;

    /* wait until all workers created */
    pthread_mutex_lock(&worker_lock);
    while (!ready)
        if (pthread_cond_wait(&worker_wait, &worker_lock)) {
            puts("pthread_cond_wait failed");
            exit(-1);
        }
    pthread_mutex_unlock(&worker_lock);

    sock_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (sock_fd == -1) {
        perror("socket");
        exit(-1);
    }

    struct sockaddr_in info = {
        .sin_family = PF_INET,
        .sin_addr.s_addr = inet_addr(TARGET_HOST),
        .sin_port = htons(TARGET_PORT),
    };

    if (connect(sock_fd, (struct sockaddr *) &info, sizeof(info)) == -1) {
        perror("connect");
        exit(-1);
    }

    while (!shut) {
        ssize_t rret;

        send(sock_fd, msg, strlen(msg), 0);
        rret = recv(sock_fd, dummy, MAX_MSG_LEN, 0);

        if(rret < 0)
            printf("recv error \n");
        else
            local_success++;

    }

    pthread_mutex_lock(&res_lock);
    total_success += local_success;
    pthread_mutex_unlock(&res_lock);

    shutdown(sock_fd, SHUT_RDWR);
    close(sock_fd);

    pthread_exit(NULL);
}

static void create_worker(int thread_qty)
{
    for (int i = 0; i < thread_qty; i++) {
        if (pthread_create(&pt[i], NULL, bench_worker, NULL)) {
            puts("thread creation failed");
            exit(-1);
        }
    }
}

static void bench(void)
{
    ready = false;
    shut = false;

    create_worker(MAX_THREAD);

    pthread_mutex_lock(&worker_lock);

    ready = true;

    /* all workers are ready, let's start bombing kecho */
    pthread_cond_broadcast(&worker_wait);
    pthread_mutex_unlock(&worker_lock);

    /* duration */
    sleep(1);
    shut = true;

    /* waiting for all workers to finish the measurement */
    for (int x = 0; x < MAX_THREAD; x++)
        pthread_join(pt[x], NULL);

    free(msg);

    /* summarize result */
    printf("--> %lld\n", total_success);
}

int main(void)
{
    init();
    bench();
    return 0;
}

As echo server, I used the implementation with epoll, and can be found at here.

I found my benchmark program will block at recv and only return after canceling the server. I'm wondering where I did wrong in client-side? (server-side blocks at epoll_wait)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source