Skip to content

Commit 8464223

Browse files
committed
Add IO benchmark test demo.
1 parent 1fc4f9a commit 8464223

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

lib_acl_cpp/src/stdlib/util.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,24 @@ long long get_curr_stamp(void)
6060

6161
double stamp_sub(const struct timeval& from, const struct timeval& sub)
6262
{
63+
#if 0
64+
struct timeval res;
65+
66+
memcpy(&res, &from, sizeof(struct timeval));
67+
68+
res.tv_usec -= sub.tv_usec;
69+
if (res.tv_usec < 0) {
70+
--res.tv_sec;
71+
res.tv_usec += 1000000;
72+
}
73+
74+
res.tv_sec -= sub.tv_sec;
75+
return res.tv_sec * 1000.0 + res.tv_usec / 1000.0;
76+
#else
77+
6378
return (from.tv_sec - sub.tv_sec) * 1000
6479
+ (from.tv_usec - sub.tv_usec) / 1000;
80+
#endif
6581
}
6682

6783
} // namespace acl
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include ../Makefile_cpp.in
2+
CFLAGS += -std=c++11
3+
PROG = client
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include "stdafx.h"
2+
3+
static bool use_sockopt = false;
4+
static std::atomic<long long> total_count(0);
5+
6+
static void fiber_main(const char *addr, int count, int timeo) {
7+
acl::socket_stream conn;
8+
if (!conn.open(addr, timeo, timeo)) {
9+
printf("Connect %s error %s\r\n", addr, acl::last_serror());
10+
return;
11+
}
12+
13+
conn.set_rw_timeout(timeo, use_sockopt);
14+
15+
printf("Connect %s ok, fd=%d\r\n", addr, conn.sock_handle());
16+
17+
const char s[] = "hello world!\r\n";
18+
char buf[1024];
19+
int i;
20+
21+
for (i = 0; i < count; i++) {
22+
if (conn.write(s, sizeof(s) - 1) == -1) {
23+
printf("write error %s\r\n", acl::last_serror());
24+
break;
25+
}
26+
if (conn.read(buf, sizeof(buf) -1, false) == -1) {
27+
printf("read error %s\r\n", acl::last_serror());
28+
break;
29+
}
30+
}
31+
32+
total_count += i;
33+
}
34+
35+
static void thread_main(const char *addr, int nfibers, int count , int timeo) {
36+
for (int j = 0; j < nfibers; j++) {
37+
go[=] {
38+
fiber_main(addr, count, timeo);
39+
};
40+
}
41+
42+
43+
acl::fiber::schedule_with(acl::FIBER_EVENT_T_KERNEL);
44+
}
45+
46+
static void usage(const char *procname) {
47+
printf("usage: %s -h [help]\r\n"
48+
" -s server_addr[default: 127.0.0.1:9001]\r\n"
49+
" -k cpus[default: 1]\r\n"
50+
" -c fibers count[default: 100]\r\n"
51+
" -o io timeout[default: -1]\r\n"
52+
" -O [if use setsockopt to set IO timeout, default: false]\r\n"
53+
, procname);
54+
}
55+
56+
int main(int argc, char *argv[]) {
57+
int ch, cpus = 1, count = 10000, nfibers = 100, timeo = -1;
58+
std::string addr("127.0.0.1:9001");
59+
60+
while ((ch = getopt(argc, argv, "hs:k:c:o:O")) > 0) {
61+
switch (ch) {
62+
case 'h':
63+
usage(argv[0]);
64+
return 0;
65+
case 's':
66+
addr = optarg;
67+
break;
68+
case 'k':
69+
cpus = atoi(optarg);
70+
break;
71+
case 'c':
72+
nfibers = atoi(optarg);
73+
break;
74+
case 'o':
75+
timeo = atoi(optarg);
76+
break;
77+
case 'O':
78+
use_sockopt = true;
79+
break;
80+
default:
81+
usage(argv[0]);
82+
return 1;
83+
}
84+
}
85+
86+
struct timeval begin;
87+
gettimeofday(&begin, nullptr);
88+
89+
std::vector<std::thread*> threads;
90+
for (int i = 0; i < cpus; i++) {
91+
std::thread *thr = new std::thread([&]{
92+
thread_main(addr.c_str(), nfibers, count, timeo);
93+
});
94+
threads.push_back(thr);
95+
}
96+
97+
for (auto thr : threads) {
98+
thr->join();
99+
delete thr;
100+
}
101+
102+
struct timeval end;
103+
gettimeofday(&end, nullptr);
104+
double tc = acl::stamp_sub(end, begin);
105+
106+
double speed = (total_count * 1000) / (tc > 0 ? tc : 0.0001);
107+
printf("Total count=%lld, tc=%.2f ms, speed=%.2f\r\n",
108+
total_count.load(), tc, speed);
109+
110+
return 0;
111+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (C) 2015-2018 IQIYI
3+
* All rights reserved.
4+
*
5+
* AUTHOR(S)
6+
* Zheng Shuxin
7+
8+
*
9+
* VERSION
10+
* 三 10/30 15:30:05 2024
11+
*/
12+
13+
#pragma once
14+
15+
#include <stdio.h>
16+
#include <stdlib.h>
17+
#include <getopt.h>
18+
#include <sys/time.h>
19+
#include <string>
20+
#include <atomic>
21+
#include <vector>
22+
#include <thread>
23+
24+
#include "lib_acl.h"
25+
#include "acl_cpp/lib_acl.hpp"
26+
#include "fiber/fiber.hpp"
27+
#include "fiber/go_fiber.hpp"

0 commit comments

Comments
 (0)