Skip to content

Commit

Permalink
add client bench
Browse files Browse the repository at this point in the history
  • Loading branch information
chkang0912 committed Jan 20, 2020
1 parent e5b2634 commit 70abf3a
Show file tree
Hide file tree
Showing 22 changed files with 493 additions and 109 deletions.
18 changes: 13 additions & 5 deletions bworker.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include "bworker.h"
pthread_t bworker_threads[MAX_BWORKER];
pthread_mutex_t bworker_mutex;
#ifdef __APPLE__
sem_t *sem_bworker;

#else
sem_t sem_bworker;
#endif
void *bworkerRecvJobs(void *arg) {
int id = (int)arg;
struct kvClient *c = server.clients[id];
Expand Down Expand Up @@ -60,13 +63,18 @@ void *bworkerProcessBackgroundJobs(void *arg) {
struct hashEntry *he = QueueDequeue(server.db->memQueue);
char *evict_key = he->key;
struct kvObject *valueObj = he->val;
chLog(LOG_NOTICE, "[BWK] Flush start key: %s", evict_key);
pthread_mutex_lock(&bworker_mutex);
flashCacheInsert(server.db->flashCache, evict_key, valueObj->ptr->buf,
valueObj->ptr->len);
LRUCacheErase(server.db->memLRU, evict_key);
chfree(he->key);
chfree(he->val);
chfree(he);
if (server.cache_mode == MODE_MEM_FIFO)
hashDelete(server.db->memCache, evict_key);
else if (server.cache_mode == MODE_MEM_LRU) {
LRUCacheErase(server.db->memLRU, evict_key);
chfree(he->key);
chfree(he->val);
chfree(he);
}
pthread_mutex_unlock(&bworker_mutex);
}
}
Expand Down
4 changes: 4 additions & 0 deletions bworker.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

extern pthread_t bworker_threads[MAX_BWORKER];
extern pthread_mutex_t bworker_mutex;
#ifdef __APPLE__
extern sem_t *sem_bworker;
#else
extern sem_t sem_bworker;
#endif

void bworkerInit(void);
#endif
38 changes: 38 additions & 0 deletions client-benchmark/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
CC=gcc
CPP=g++
OPT=-O0
DEBUG=-g -ggdb #-DWITHOUT_LOG #-DONLY_FLASH #-DWITHOUT_LOG #-DONLY_FLASH
DEPENDENCY_TARGETS=
CFLAGS+= $(DEBUG) $(OPT) -std=c11
CXXFLAGS+= $(DEBUG) $(OPT) -std=c++11
LDFLAGS=-Llib -Iinclude
LIBS=-pthread
OBJ_FOLDER = obj
uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')
# ifeq ($(uname_S),Darwin)
# CFLAGS+=-fPIC
# OBJ_FOLDER=obj_mac
# BIN_FOLDER=bin_mac
# else
# OBJ_FOLDER=obj_linux
# BIN_FOLDER=bin_linux
# endif
TARGET=client
CLIENT_OBJ=net.o client.o util.o

all: $(TARGET)

dep:
-(cd ../deps && $(MAKE) $(DEPENDENCY_TARGETS))

client: $(CLIENT_OBJ)
$(CPP) $(CXXFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
# mv *.o $(OBJ_FOLDER)/

%.o: %.c
$(CC) $(CFLAGS) $(LDFLAGS) -c $<
%.o: %.cc
$(CPP) $(CXXFLAGS) $(LDFLAGS) -c $<

clean:
rm -rf $(TARGET) $(BIN_FOLDER)/*.o $(OBJ_FOLDER)/*.o *.o
29 changes: 29 additions & 0 deletions client-benchmark/client.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <random>
#include <string>

using namespace std;

int main() {
default_random_engine generator; // generator 생성
normal_distribution<double> distribution(5.0, 4.0);

map<int, int> hist; // hostogram 을 그리기 위한 버퍼
for (int n = 0; n < 5000000; ++n) {
double number = distribution(generator);
if ((number >= 0.0) && (number < 10.0)) ++hist[int(number)];
// ++hist[std::round(distribution(generator))]; // histogram buffering
}
int total = 0;
for (auto p : hist) { // * 로 나타내기
std::cout << std::fixed << std::setprecision(1) << std::setw(2) << p.first
<< ' ' << std::string(p.second / 25000, '*') << '\n';
total += p.second;
}
std::cout << total << std::endl;
getchar(); // 결과 창이 없어지지 않게 하기 위함
return 0;
}
63 changes: 63 additions & 0 deletions client-benchmark/client.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "client.h"
struct kvClient client;
static void sigShutdownHandler(int sig) {
std::string msg;
switch (sig) {
case SIGINT:
msg = "Received SIGINT scheduling shutdown...";
break;
case SIGTERM:
msg = "Received SIGTERM scheduling shutdown...";
break;
default:
msg = "Received shutdown signal, scheduling shutdown...";
};
std::cout << msg << std::endl;
if (sig == SIGINT) {
std::cout << "You insist... exiting now." << std::endl;
exit(1);
}
}
int setupSignalHandlers(void) {
sighandler_t sig_ret;
sig_ret = signal(SIGTERM, sigShutdownHandler);
if (sig_ret == SIG_ERR) {
std::cout << "can't set SIGTERM handler" << std::endl;
return EXIT_FAILURE;
}
signal(SIGINT, sigShutdownHandler);
if (sig_ret == SIG_ERR) {
std::cout << "can't set SIGINT handler" << std::endl;
return EXIT_FAILURE;
}
return 0;
}

static void cliRefreshPrompt(void) {
int len;
client.prompt.clear();
client.prompt +=
client.ip.append(":") + std::to_string(client.port).append("> ");
}

void initClient(void) {
Net_util net(client.port, client.ip);
net.tcpConnect();
client.cli = static_cast<void*>(&net);
cliRefreshPrompt();
}
int main(int argc, char** argv) {
if (argc >= 2) {
char* configfile = NULL;
configfile = argv[1];
loadServerConfig(configfile);
} else {
std::cout << "ex: " << argv[0] << " conf/client.conf" << std::endl;
exit(-1);
}
initClient();
std::cout << client.prompt;
int data;
std::cin >> data;
return 0;
}
26 changes: 26 additions & 0 deletions client-benchmark/client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef __SERVER_H
#define __SERVER_H
#include <signal.h>
#include <stddef.h>
#include <sys/syscall.h>
#include "net.h"
#include "util.h"
#define KV_IOBUF_LEN 1024 * 40

typedef void (*sighandler_t)(int);

struct msg {
unsigned int len;
char buf[];
};

struct kvClient {
/* Networking */
int port;
std::string ip;
void *cli;
std::string prompt;
};

extern struct kvClient client;
#endif
4 changes: 4 additions & 0 deletions client-benchmark/conf/client.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#client config
port 19502
host 127.0.0.1
32 changes: 32 additions & 0 deletions client-benchmark/net.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "net.h"
void Net_util::tcpConnect() {
struct sockaddr_in server_addr, client_addr;
if ((cfd_ = socket(AF_INET, SOCK_STREAM, 0)) == -1) { // 소켓 생성
printf("[NET] Can't open stream socket.");
exit(-1);
}
memset(&server_addr, 0x00, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(ip_.c_str());
server_addr.sin_port = htons(port_);
int ret = connect(cfd_, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (ret < 0) {
printf("[NET] Can't connect port #%d.", port_);
exit(-1);
}
querybuf_ = (struct msg *)malloc(sizeof(struct msg) + KV_IOBUF_LEN);
std::cout << "[NET] Connect " << ip_ << ":" << port_ << std::endl;
}

void Net_util::tcpRecv() {
int rst;
rst = read(cfd_, querybuf_->buf, KV_IOBUF_LEN);
if (rst < 0) printf("recv error");
printf("[NET] MSG Recived\n%s", querybuf_->buf);
}
void Net_util::tcpSend() {
int rst;
rst = write(cfd_, querybuf_->buf, querybuf_->len);
if (rst < 0) printf("recv error");
printf("[NET] MSG Recived\n%s", querybuf_->buf);
}
30 changes: 30 additions & 0 deletions client-benchmark/net.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef __NET_H
#define __NET_H

#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include "client.h"

#define IP_STR_LEN 46
class Net_util {
public:
Net_util(int port, std::string ip) : port_(port), ip_(ip) {}
~Net_util() {}
void tcpConnect();
void tcpRecv();
void tcpSend();
struct msg *querybuf_;

private:
int port_;
std::string ip_;
int cfd_;
};
#endif
29 changes: 29 additions & 0 deletions client-benchmark/util.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "util.h"

void loadServerConfig(char *filename) {
/* Load the file content */
char buf[1024];
char *argv[10];
int argc = 0;
if (filename) {
FILE *fp;
if ((fp = fopen(filename, "r")) == NULL) {
printf("Fatal error, can't open config file '%s'", filename);
exit(1);
}
while (fgets(buf, 1024, fp) != NULL) {
if (buf[0] == '#' || buf[0] == '\0' || buf[0] == '\n') continue;
argv[argc] = strtok(buf, " ");
while (argv[argc] != NULL) {
argv[++argc] = strtok(NULL, " "); // 다음 문자열을 잘라서 포인터를 반환
}
if (!strcasecmp(argv[0], "port") && argc == 2) {
client.port = atoi(argv[1]);
} else if (!strcasecmp(argv[0], "host") && argc == 2) {
client.ip = std::string(argv[1]);
}
argc = 0;
}
if (fp != stdin) fclose(fp);
}
}
15 changes: 15 additions & 0 deletions client-benchmark/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef __UTIL_H
#define __UTIL_H

#include <stdio.h>
#include "client.h"

// LOG_DEBUG 0
// LOG_NOTICE 1
// LOG_ERROR 2

int string2ll(const char *s, size_t slen, long long *value);
void loadServerConfig(char *filename);
void makingMessage(struct kvClient *c);

#endif
Loading

0 comments on commit 70abf3a

Please sign in to comment.