Skip to content

Commit

Permalink
Add client test
Browse files Browse the repository at this point in the history
  • Loading branch information
uoo723 committed Jun 23, 2017
1 parent 3519461 commit b7c988d
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 27 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ add_executable(main ${sources})
add_executable(node_test "test/node_test.c")
add_executable(hashtable_test "test/hash_test.c")
add_executable(thpool_rwlock_test "test/thpool_rwlock_test.c")
add_executable(client_test "test/client_test.c" "src/client.c" "src/server.c")

target_link_libraries(hashtable node)
target_link_libraries(thpool pthread)
target_link_libraries(main hashtable thpool)
target_link_libraries(node_test node)
target_link_libraries(hashtable_test hashtable)
target_link_libraries(thpool_rwlock_test thpool)
target_link_libraries(client_test thpool hashtable)

target_compile_definitions(client_test PUBLIC CLIENT_TEST)
target_compile_options(client_test PRIVATE "-g")
7 changes: 1 addition & 6 deletions include/client.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#ifndef CLIENT_H
#define CLIENT_H

#ifndef CLIENT_TEST
#define client() client_non_test()
#else
#define client() client_test()
#endif

void client();
void req_put(unsigned int, char *);
void req_get(unsigned int);
void req_remove(unsigned int);
Expand Down
27 changes: 16 additions & 11 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,58 +20,63 @@ static void menu_quit();
static void snd_msg();

static key_t key_id;
static msgbuf_t msg;

void client_non_test() {
void client() {
if ((key_id = msgget(KEYID, IPC_CREAT|0666)) < 0) {
perror("msgget error ");
perror("msgget error in client ");
exit(0);
}

#ifndef CLIENT_TEST
while (1) {
client_view();
}
#endif
}

void req_put(unsigned int key, char *value) {
msgbuf_t msg;
msg.mtype = TYPE_SERVER;
msg.type = TYPE_REQ_PUT;
msg.key = key;
strcpy(msg.value, value);
snd_msg();
snd_msg(&msg);
}

void req_get(unsigned int key) {
msgbuf_t msg;
msg.mtype = TYPE_SERVER;
msg.type = TYPE_REQ_GET;
msg.key = key;
snd_msg();
snd_msg(&msg);

if (msgrcv(key_id, &msg, MSGSIZE, TYPE_CLIENT, 0) < 0) {
perror("msgrcv error ");
perror("msgrcv error in client ");
return;
}

show_get_result(msg.key, msg.value);
}

void req_remove(unsigned int key) {
msgbuf_t msg;
msg.mtype = TYPE_SERVER;
msg.type = TYPE_REQ_REMOVE;
msg.key = key;
snd_msg();
snd_msg(&msg);
}

void req_quit() {
msgbuf_t msg;
msg.mtype = TYPE_SERVER;
msg.type = TYPE_QUIT;
snd_msg();
snd_msg(&msg);
exit(0);
}

static void snd_msg() {
if (msgsnd(key_id, &msg, MSGSIZE, 0) < 0) {
perror("msgsnd error ");
static void snd_msg(msgbuf_t *msg) {
if (msgsnd(key_id, msg, MSGSIZE, 0) < 0) {
perror("msgsnd error in client ");
return;
}
}
38 changes: 29 additions & 9 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ typedef struct {
char value[VALUESIZE];
} args_t;

static int thread_num = 4;

static key_t key_id;
static msgbuf_t msg;

static hashtable_t *hashtable;
static threadpool_t thpool;

Expand All @@ -42,7 +44,7 @@ void server() {
signal(SIGINT, int_handler);

if ((key_id = msgget(KEYID, IPC_CREAT|0666)) < 0) {
perror("msgget error ");
perror("msgget error in client ");
exit(0);
}

Expand All @@ -51,7 +53,7 @@ void server() {
exit(0);
}

if ((thpool = thpool_init(4)) == NULL) {
if ((thpool = thpool_init(thread_num)) == NULL) {
perror("thpool is null ");
exit(0);
}
Expand All @@ -62,41 +64,60 @@ void server() {
}

args_t *args = NULL;
msgbuf_t msg;

while (1) {
if (msgrcv(key_id, &msg, MSGSIZE, TYPE_SERVER, 0) < 0) {
perror("msgrcv error ");
perror("msgrcv error in server ");
return;
}

switch (msg.type) {
case TYPE_REQ_PUT:

#ifndef SINGLE_THREAD
args = malloc(sizeof(args_t));
args->key = msg.key;
strcpy(args->value, msg.value);

thpool_add_work(thpool, (void *) rcv_put_internal, (void *) args);
#else
rcv_put(msg.key, msg.value);
#endif

break;

case TYPE_REQ_GET:

#ifndef SINGLE_THREAD
args = malloc(sizeof(args_t));
args->key = msg.key;

thpool_add_work(thpool, (void *) rcv_get_internal, (void *) args);
#else
rcv_get(msg.key);
#endif

break;

case TYPE_REQ_REMOVE:

#ifndef SINGLE_THREAD
args = malloc(sizeof(args_t));
args->key = msg.key;

thpool_add_work(thpool, (void *) rcv_remove_internal, (void *) args);
#else
rcv_remove(msg.key);
#endif

break;

case TYPE_QUIT:
thpool_destroy(thpool);
pthread_rwlock_destroy(&rwlock);
ht_destroy(hashtable);

int_handler(2);
exit(0);
break;
Expand Down Expand Up @@ -143,6 +164,7 @@ void rcv_put(unsigned int key, char *value) {

void rcv_get(unsigned int key) {
node_t *node = ht_get(hashtable, key);
msgbuf_t msg;

if (node == NULL) {
strcpy(msg.value, NODATA);
Expand All @@ -152,13 +174,11 @@ void rcv_get(unsigned int key) {

msg.mtype = TYPE_CLIENT;
msg.type = TYPE_RES_GET;

msg.key = key;
if (msgsnd(key_id, &msg, MSGSIZE, 0) < 0) {
perror("msgsnd error ");
perror("msgsnd error in server ");
return;
}

msg.mtype = 0;
}

void rcv_remove(unsigned int key) {
Expand Down
120 changes: 120 additions & 0 deletions test/client_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#define MAX_CONTENTS 100000
#define MAX_OPERATION 500000

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>

#include "client.h"
#include "node.h"
#include "server.h"
#include "msg.h"
#include "thpool.h"

static void fill_contents();
static void client_test_put();
static void client_test_get();

static void start_timer();
static void stop_timer();

typedef threadpool threadpool_t;

typedef struct {
unsigned int key;
char value[VALUESIZE];

} args_t;

static unsigned int key_list[MAX_CONTENTS];
static threadpool_t thpool;

static time_t start_time;
static time_t end_time;

void client_test() {
printf("client_test!!\n");

if ((thpool = thpool_init(4)) == NULL) {
perror("thpool is null ");
exit(0);
}

printf("fill_contents()\n");
start_timer();
fill_contents();
stop_timer();

printf("operation: %d, put(): 5%%, get(): 95%%\n", MAX_OPERATION);
start_timer();
int i;
for (i = 0; i < MAX_OPERATION; i++) {
int p = (int) (rand() / (float) RAND_MAX * 100);

// thpool_add_work(thpool, (void *) client_test_get, NULL);
if (p > 5)
thpool_add_work(thpool, (void *) client_test_get, NULL);
else
thpool_add_work(thpool, (void *) client_test_put, NULL);
}
thpool_wait(thpool);
stop_timer();
req_quit();
}

int main() {
srand(time(NULL));
if (fork() == 0) {
client();
client_test();
} else {
server();
}

return 0;
}

void show_get_result(unsigned int key, char *value) {
// if (strcmp(value, NODATA) == 0) {
// printf("value: no data\n");
// } else {
// printf("key: %u, value: %s in %u\n", key, value, (int) pthread_self());
// }
}

static void fill_contents() {
int i;
for (i = 0; i < MAX_CONTENTS; i++) {
unsigned int key = rand();
char value[VALUESIZE];
sprintf(value, "%u", key);
req_put(key, value);
key_list[i] = key;
}
}

static void client_test_put() {
// printf("client_test_put() in %u\n", (int) pthread_self());
unsigned int key = rand();
char value[VALUESIZE];
sprintf(value, "%u", key);
req_put(key, value);
}

static void client_test_get() {
// printf("client_test_get() in %u\n", (int) pthread_self());
int index = rand() % MAX_CONTENTS;
req_get(key_list[index]);
}

static void start_timer() {
start_time = time(NULL);
}

static void stop_timer() {
end_time = time(NULL);
printf("elapsed: %ld second(s)\n", end_time - start_time);
}
2 changes: 1 addition & 1 deletion test/thpool_rwlock_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ int main() {
}
}

sleep(1);
thpool_wait(thpool);
thpool_destroy(thpool);
pthread_rwlock_destroy(&rwlock);
return 0;
Expand Down

0 comments on commit b7c988d

Please sign in to comment.