Skip to content

Commit

Permalink
Add IO benchmark test demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengshuxin committed Oct 30, 2024
1 parent 1fc4f9a commit 8464223
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib_acl_cpp/src/stdlib/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,24 @@ long long get_curr_stamp(void)

double stamp_sub(const struct timeval& from, const struct timeval& sub)
{
#if 0
struct timeval res;

memcpy(&res, &from, sizeof(struct timeval));

res.tv_usec -= sub.tv_usec;
if (res.tv_usec < 0) {
--res.tv_sec;
res.tv_usec += 1000000;
}

res.tv_sec -= sub.tv_sec;
return res.tv_sec * 1000.0 + res.tv_usec / 1000.0;
#else

return (from.tv_sec - sub.tv_sec) * 1000
+ (from.tv_usec - sub.tv_usec) / 1000;
#endif
}

} // namespace acl
3 changes: 3 additions & 0 deletions lib_fiber/samples-c++1x/client2/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include ../Makefile_cpp.in
CFLAGS += -std=c++11
PROG = client
111 changes: 111 additions & 0 deletions lib_fiber/samples-c++1x/client2/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include "stdafx.h"

static bool use_sockopt = false;
static std::atomic<long long> total_count(0);

static void fiber_main(const char *addr, int count, int timeo) {
acl::socket_stream conn;
if (!conn.open(addr, timeo, timeo)) {
printf("Connect %s error %s\r\n", addr, acl::last_serror());
return;
}

conn.set_rw_timeout(timeo, use_sockopt);

printf("Connect %s ok, fd=%d\r\n", addr, conn.sock_handle());

const char s[] = "hello world!\r\n";
char buf[1024];
int i;

for (i = 0; i < count; i++) {
if (conn.write(s, sizeof(s) - 1) == -1) {
printf("write error %s\r\n", acl::last_serror());
break;
}
if (conn.read(buf, sizeof(buf) -1, false) == -1) {
printf("read error %s\r\n", acl::last_serror());
break;
}
}

total_count += i;
}

static void thread_main(const char *addr, int nfibers, int count , int timeo) {
for (int j = 0; j < nfibers; j++) {
go[=] {
fiber_main(addr, count, timeo);
};
}


acl::fiber::schedule_with(acl::FIBER_EVENT_T_KERNEL);
}

static void usage(const char *procname) {
printf("usage: %s -h [help]\r\n"
" -s server_addr[default: 127.0.0.1:9001]\r\n"
" -k cpus[default: 1]\r\n"
" -c fibers count[default: 100]\r\n"
" -o io timeout[default: -1]\r\n"
" -O [if use setsockopt to set IO timeout, default: false]\r\n"
, procname);
}

int main(int argc, char *argv[]) {
int ch, cpus = 1, count = 10000, nfibers = 100, timeo = -1;
std::string addr("127.0.0.1:9001");

while ((ch = getopt(argc, argv, "hs:k:c:o:O")) > 0) {
switch (ch) {
case 'h':
usage(argv[0]);
return 0;
case 's':
addr = optarg;
break;
case 'k':
cpus = atoi(optarg);
break;
case 'c':
nfibers = atoi(optarg);
break;
case 'o':
timeo = atoi(optarg);
break;
case 'O':
use_sockopt = true;
break;
default:
usage(argv[0]);
return 1;
}
}

struct timeval begin;
gettimeofday(&begin, nullptr);

std::vector<std::thread*> threads;
for (int i = 0; i < cpus; i++) {
std::thread *thr = new std::thread([&]{
thread_main(addr.c_str(), nfibers, count, timeo);
});
threads.push_back(thr);
}

for (auto thr : threads) {
thr->join();
delete thr;
}

struct timeval end;
gettimeofday(&end, nullptr);
double tc = acl::stamp_sub(end, begin);

double speed = (total_count * 1000) / (tc > 0 ? tc : 0.0001);
printf("Total count=%lld, tc=%.2f ms, speed=%.2f\r\n",
total_count.load(), tc, speed);

return 0;
}
27 changes: 27 additions & 0 deletions lib_fiber/samples-c++1x/client2/stdafx.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (C) 2015-2018 IQIYI
* All rights reserved.
*
* AUTHOR(S)
* Zheng Shuxin
* E-mail: [email protected]
*
* VERSION
* 三 10/30 15:30:05 2024
*/

#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/time.h>
#include <string>
#include <atomic>
#include <vector>
#include <thread>

#include "lib_acl.h"
#include "acl_cpp/lib_acl.hpp"
#include "fiber/fiber.hpp"
#include "fiber/go_fiber.hpp"

0 comments on commit 8464223

Please sign in to comment.