Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2654646

Browse files
committed
update docs for v3.0.1
1 parent b11b40a commit 2654646

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+12454
-9912
lines changed

config.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ enableGitInfo = true
8080

8181
# Set source repository location.
8282
# Used for 'Last Modified' and 'Edit this page' links.
83-
BookRepo = 'https://github.com/cocoyaxi/codoc'
83+
BookRepo = 'https://github.com/coostdocs/codoc'
8484

8585
# (Optional, default 'commit') Specifies commit portion of the link to the page's last modified
8686
# commit hash for 'doc' page type.

content/cn/about/co.md

Lines changed: 63 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ title: "简介"
1212

1313
**[coost](https://github.com/idealvin/coost)** 是一个**兼具性能与易用性**的跨平台 C++ 基础库,其目标是打造一把 C++ 开发神器,让 C++ 编程变得简单、轻松、愉快。
1414

15-
coost 原名为 co,后改为 cocoyaxi,前者过短,后者过长,取中庸之道,又改为 coost。它曾被称为小型 [boost](https://www.boost.org/) 库,与 boost 相比,coost 小而精美,在 **linux 与 mac 上编译出来的静态库仅 1M 左右大小**,却包含了不少强大的功能:
15+
coost 简称为 co,曾被称为小型 [boost](https://www.boost.org/) 库,与 boost 相比,coost 小而精美,在 **linux 与 mac 上编译出来的静态库仅 1M 左右大小**,却包含了不少强大的功能:
1616

1717

1818
{{< columns >}}
1919

2020
- 命令行与配置文件解析(flag)
2121
- **高性能日志库(log)**
22-
- 单元测试框架(unitest)
22+
- 单元测试框架
23+
- 基准测试框架
2324
- **go-style 协程**
2425
- 基于协程的网络编程框架
25-
- 高效 JSON 库
2626
- **基于 JSON 的 RPC 框架**
2727

2828
<--->
@@ -38,7 +38,7 @@ coost 原名为 co,后改为 cocoyaxi,前者过短,后者过长,取中
3838
<--->
3939

4040
- **面向玄学编程**
41-
- LruMap
41+
- 高效 JSON 库
4242
- hash 库
4343
- path 库
4444
- 文件系统操作(fs)
@@ -93,21 +93,21 @@ xmake r co
9393

9494
### 使用 coost 开发 C++ 项目
9595

96-
最简单的,可以直接包含 [co/all.h](https://github.com/idealvin/coost/blob/master/include/co/all.h),使用 coost 中的所有特性。如果担心影响编译速度,也可以只包含需要用到的头文件,如包含 [co/co.h](https://github.com/idealvin/coost/blob/master/include/co/co.h),可以使用 co/flag, co/log 以及协程相关的所有特性。
96+
最简单的,可以直接包含 [co/all.h](https://github.com/idealvin/coost/blob/master/include/co/all.h),使用 coost 中的所有特性。如果担心影响编译速度,也可以只包含需要用到的头文件,如包含 [co/co.h](https://github.com/idealvin/coost/blob/master/include/co/co.h),可以使用 flag, log 以及协程相关的所有特性。
9797

9898
```cpp
9999
#include "co/all.h"
100100

101101
DEF_string(s, "nice", "");
102102

103103
int main(int argc, char** argv) {
104-
flag::init(argc, argv);
104+
flag::parse(argc, argv);
105105
LOG << FLG_s;
106106
return 0;
107107
}
108108
```
109109
110-
上面是一个简单的例子,main 函数第一行用于解析命令行参数及配置文件。coost 中的部分组件用 flag 定义配置项,因此需要在 main 函数开头调用 `flag::init()` 进行初始化。
110+
上面是一个简单的例子,main 函数第一行用于解析命令行参数及配置文件。coost 中的部分组件用 flag 定义配置项,因此需要在 main 函数开头调用 `flag::parse()` 进行初始化。
111111
112112
用户也可以用宏 `DEF_main` 定义 main 函数:
113113
@@ -122,7 +122,7 @@ DEF_main(argc, argv) {
122122
}
123123
```
124124

125-
`DEF_main` 将 main 函数中的代码放到协程中运行,内部已经调用了 `flag::init()`,用户无需手动调用。
125+
`DEF_main` 将 main 函数中的代码放到协程中运行,内部已经调用了 `flag::parse()`,用户无需手动调用。
126126

127127

128128

@@ -131,7 +131,7 @@ DEF_main(argc, argv) {
131131

132132
### 内存分配器
133133

134-
ptmalloc、jemalloc、tcmalloc 以及 mimalloc 等内存分配器,小内存释放后大概率不会归还给操作系统,这可能**造成释放大量小内存后内存占用量却始终不降的疑似内存泄漏的现象**。为解决此问题,coost 设计了一个专用的内存分配器(co/malloc),在兼顾性能的同时,会尽可能多的将释放的内存归还给系统,有利于降低程序的内存占用量,在实测中也取得了良好的效果。
134+
ptmalloc、jemalloc、tcmalloc 以及 mimalloc 等内存分配器,小内存释放后大概率不会归还给操作系统,这可能**造成释放大量小内存后内存占用量却始终不降的疑似内存泄漏的现象**。为解决此问题,coost 设计了一个专用的内存分配器,在兼顾性能的同时,会尽可能多的将释放的内存归还给系统,有利于降低程序的内存占用量,在实测中也取得了良好的效果。
135135

136136
[co/test](https://github.com/idealvin/coost/blob/master/test/mem.cc) 中提供了简单的测试代码,可以执行如下命令编译及运行:
137137

@@ -150,7 +150,7 @@ xmake r mem -t 4 -s
150150

151151
表中数据为平均耗时,单位为纳秒(ns),linux 是在 Windows WSL 中运行的 ubuntu 系统,speedup 是 coost 内存分配器相对于系统内存分配器的性能提升倍数。
152152

153-
可以看到,**co::alloc 在 Linux 上比 ::malloc 提升了近 99 倍**,这其中的一个重要原因是 ptmalloc 在多线程环境中锁竞争开销较大,而 co/malloc 在设计上尽可能避免锁的使用,小块内存的分配、释放不需要锁,跨线程释放时连自旋锁也不用。
153+
可以看到,**co::alloc 在 Linux 上比 ::malloc 提升了近 99 倍**,这其中的一个重要原因是 ptmalloc 在多线程环境中锁竞争开销较大,而 coost 内存分配器在设计上尽可能避免锁的使用,小块内存的分配、释放不需要锁,跨线程释放时连自旋锁也不用。
154154

155155

156156

@@ -172,7 +172,7 @@ xmake r mem -t 4 -s
172172
| 4 | 0.206712 | 4.764238 | 0.316607 | 0.743227 | 23.0/2.3 |
173173
| 8 | 0.302088 | 3.963644 | 0.406025 | 1.417387 | 13.1/3.5 |
174174

175-
上表是分别[用 1、2、4、8 个线程打印 100 万条日志](https://github.com/idealvin/coost/tree/benchmark)的耗时,单位为秒,speedup 是 co/log 在 linux、windows 平台相对于 spdlog 的性能提升倍数。
175+
上表是分别[用 1、2、4、8 个线程打印 100 万条日志](https://github.com/idealvin/coost/tree/benchmark)的耗时,单位为秒,speedup 是 co.log 在 linux、windows 平台相对于 spdlog 的性能提升倍数。
176176

177177

178178

@@ -184,7 +184,7 @@ xmake r mem -t 4 -s
184184
| mac | 783 | 1097 | 1289 | 1658 | 1.6/1.5 |
185185
| linux | 468 | 764 | 1359 | 1070 | 2.9/1.4 |
186186

187-
上表是将 [twitter.json](https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/twitter.json) 最小化后测得的 stringify 及 parse 的平均耗时,单位为微秒(us),speedup 是 co/json 在 stringify、parse 方面相对于 rapidjson 的性能提升倍数。
187+
上表是将 [twitter.json](https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/twitter.json) 最小化后测得的 stringify 及 parse 的平均耗时,单位为微秒(us),speedup 是 co.json 在 stringify、parse 方面相对于 rapidjson 的性能提升倍数。
188188

189189

190190

@@ -193,7 +193,7 @@ xmake r mem -t 4 -s
193193

194194
### 面向玄学编程
195195

196-
[co/god.h](https://github.com/idealvin/coost/blob/master/include/co/god.h) 提供模板相关的一些功能。模板用到深处,代码深奥难懂,有些 C++ 程序员称之为面向玄学编程。
196+
[co/god.h](https://github.com/idealvin/coost/blob/master/include/co/god.h) 提供模板相关的一些功能。模板用到深处有点玄,有些 C++ 程序员称之为面向玄学编程。
197197

198198
```cpp
199199
#include "co/god.h"
@@ -223,7 +223,7 @@ DEF_uint32(u, 0, "xxx");
223223
DEF_string(s, "", "xx");
224224

225225
int main(int argc, char** argv) {
226-
flag::init(argc, argv);
226+
flag::parse(argc, argv);
227227
COUT << "x: " << FLG_x;
228228
COUT << "y: " << FLG_y;
229229
COUT << "debug: " << FLG_debug;
@@ -259,7 +259,7 @@ log 支持两种类型的日志:一种是 level log,分为 debug, info, warn
259259
#include "co/log.h"
260260

261261
int main(int argc, char** argv) {
262-
flag::init(argc, argv);
262+
flag::parse(argc, argv);
263263

264264
TLOG("xx") << "s" << 23; // topic log
265265
DLOG << "hello " << 23; // debug
@@ -305,14 +305,14 @@ DEF_test(os) {
305305
} // namespace test
306306
```
307307

308-
上面是一个简单的例子,`DEF_test` 宏定义了一个测试单元,实际上就是一个函数(类中的方法)`DEF_case` 宏定义了测试用例,每个测试用例实际上就是一个代码块。main 函数一般只需要下面几行:
308+
上面是一个简单的例子,`DEF_test` 宏定义了一个测试单元,实际上就是一个函数。`DEF_case` 宏定义了测试用例,每个测试用例实际上就是一个代码块。main 函数一般只需要下面几行:
309309

310310
```cpp
311311
#include "co/unitest.h"
312312

313313
int main(int argc, char** argv) {
314-
flag::init(argc, argv);
315-
unitest::run_all_tests();
314+
flag::parse(argc, argv);
315+
unitest::run_tests();
316316
return 0;
317317
}
318318
```
@@ -325,6 +325,41 @@ xmake r unitest -os # 仅运行 os 单元中的测试用例
325325
```
326326

327327

328+
### 基准测试
329+
330+
[benchmark](../../co/benchmark/) 是一个简单易用的基准测试框架。
331+
332+
```cpp
333+
#include "co/benchmark.h"
334+
#include "co/mem.h"
335+
336+
BM_group(malloc) {
337+
void* p;
338+
339+
BM_add(::malloc)(
340+
p = ::malloc(32);
341+
);
342+
BM_use(p);
343+
344+
BM_add(co::alloc)(
345+
p = co::alloc(32);
346+
);
347+
BM_use(p);
348+
}
349+
350+
int main(int argc, char** argv) {
351+
flag::parse(argc, argv);
352+
bm::run_benchmarks();
353+
return 0;
354+
}
355+
```
356+
357+
上例中,`BM_group` 定义了一个测试组,`BM_add` 添加了两个需要进行比较的测试用例,`BM_use` 防止编译器将测试代码优化掉。
358+
359+
基准测试结果以 markdown table 格式输出,如下所示:
360+
![bm.png](/images/bm.png)
361+
362+
328363
329364
### JSON
330365
@@ -371,15 +406,15 @@ coost 实现了类似 golang 中 goroutine 的协程机制,它有如下特性
371406
- 支持多线程调度,默认线程数为系统 CPU 核数。
372407
- 共享栈,同一线程中的协程共用若干个栈(大小默认为 1MB),内存占用低。
373408
- 各协程之间为平级关系,可以在任何地方(包括在协程中)创建新的协程。
374-
- 支持[协程同步事件](../../co/coroutine/#协程同步事件coevent)[协程锁](../../co/coroutine/#协程锁comutex)[channel](../../co/coroutine/#channelcochan)[waitgroup](../../co/coroutine/#waitgroupcowaitgroup) 等协程同步机制。
409+
- 支持[协程同步事件](../../co/concurrency/coroutine/event/)[协程锁](../../co/concurrency/coroutine/mutex/)[channel](../../co/concurrency/coroutine/chan/)[waitgroup](../../co/concurrency/coroutine/wg/) 等协程同步机制。
375410

376411
```cpp
377412
#include "co/co.h"
378413

379414
int main(int argc, char** argv) {
380-
flag::init(argc, argv);
415+
flag::parse(argc, argv);
381416

382-
co::WaitGroup wg;
417+
co::wait_group wg;
383418
wg.add(2);
384419

385420
go([wg](){
@@ -397,16 +432,16 @@ int main(int argc, char** argv) {
397432
}
398433
```
399434
400-
上面的代码中,`go()` 创建的协程会均匀的分配到不同的调度线程中。用户也可以自行控制协程的调度:
435+
上面的代码中,`go()` 创建的协程会根据默认策略分配到不同的调度线程中。用户也可以自行控制协程的调度:
401436
402437
```cpp
403438
// run f1 and f2 in the same scheduler
404-
auto s = co::next_scheduler();
439+
auto s = co::next_sched();
405440
s->go(f1);
406441
s->go(f2);
407442
408443
// run f in all schedulers
409-
for (auto& s : co::schedulers()) {
444+
for (auto& s : co::scheds()) {
410445
s->go(f);
411446
}
412447
```
@@ -415,11 +450,10 @@ for (auto& s : co::schedulers()) {
415450

416451
### 网络编程
417452

418-
coost 提供了一套基于协程的网络编程框架,大致可以分为三个部分
453+
coost 提供了一套基于协程的网络编程框架:
419454

420-
- **[协程化的 socket API](../../co/coroutine/#协程化的-socket-api)**,形式上与系统 socket API 类似,熟悉 socket 编程的用户,可以轻松的用同步的方式写出高性能的网络程序。
455+
- **[协程化的 socket API](../../co/net/sock/)**,形式上与系统 socket API 类似,熟悉 socket 编程的用户,可以轻松的用同步的方式写出高性能的网络程序。
421456
- [TCP](../../co/net/tcp/)[HTTP](../../co/net/http/)[RPC](../../co/net/rpc/) 等高层网络编程组件,兼容 IPv6,同时支持 SSL,用起来比 socket API 更方便。
422-
- **[系统 API hook](../../co/coroutine/#%E7%B3%BB%E7%BB%9F-api-hook)**,支持在协程中使用一般的三方网络库。
423457

424458

425459
**RPC server**
@@ -430,7 +464,7 @@ coost 提供了一套基于协程的网络编程框架,大致可以分为三
430464
#include "co/time.h"
431465

432466
int main(int argc, char** argv) {
433-
flag::init(argc, argv);
467+
flag::parse(argc, argv);
434468

435469
rpc::Server()
436470
.add_service(new xx::HelloWorldImpl)
@@ -457,7 +491,7 @@ curl http://127.0.0.1:7788/xx --request POST --data '{"api":"ping"}'
457491
DEF_string(d, ".", "root dir"); // docroot for the web server
458492

459493
int main(int argc, char** argv) {
460-
flag::init(argc, argv);
494+
flag::parse(argc, argv);
461495
so::easy(FLG_d.c_str()); // mum never have to worry again
462496
return 0;
463497
}

content/cn/co/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
---
22
weight: 3
3-
title: "CO 参考文档"
3+
title: "参考文档"
44
---

content/cn/co/benchmark.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
weight: 7
3+
title: "基准测试"
4+
---
5+
6+
include: [co/benchmark.h](https://github.com/idealvin/coost/blob/master/include/co/benchmark.h).
7+
8+
9+
## 基本概念
10+
11+
**co.benchmark** 是 v3.0.1 新增的基准测试框架,可用于性能基准测试。
12+
13+
14+
### BM_group
15+
16+
```cpp
17+
#define BM_group(_name_) \
18+
...... \
19+
void _co_bm_group_##_name_(bm::xx::Group& _g_)
20+
```
21+
22+
- `BM_group` 宏用于定义基准测试组,实际上定义了一个函数。
23+
- 每个 group 内可以用 [BM_add](#bm_add) 定义多条基准测试。
24+
25+
{{< hint warning >}}
26+
参数 `_name_` 是组名,也是所定义函数名的一部分,如 `BM_group(atomic)` 是合理的,而 `BM_group(co.atomic)` 则是不允许的。
27+
{{< /hint >}}
28+
29+
30+
31+
32+
### BM_add
33+
34+
```cpp
35+
#define BM_add(_name_) \
36+
_g_.bm = #_name_; \
37+
_BM_add
38+
```
39+
40+
- `BM_add` 宏用于定义基准测试,它必须在 [BM_group](#bm_group) 定义的函数内使用。
41+
42+
{{< hint warning >}}
43+
参数 `_name_` 是基准测试名,与 `BM_group` 不同,`BM_add(co.atomic)` 也是允许的。
44+
{{< /hint >}}
45+
46+
47+
48+
### BM_use
49+
50+
```cpp
51+
#define BM_use(v) bm::xx::use(&v, sizeof(v))
52+
```
53+
54+
- `BM_use` 宏告诉编译器变量 `v` 会被使用,防止编译器将一些测试代码优化掉。
55+
56+
57+
58+
59+
## 编写基准测试代码
60+
61+
62+
### 测试代码示例
63+
64+
```cpp
65+
#include "co/benchmark.h"
66+
#include "co/mem.h"
67+
68+
BM_group(malloc) {
69+
void* p;
70+
71+
BM_add(::malloc)(
72+
p = ::malloc(32);
73+
);
74+
BM_use(p);
75+
76+
BM_add(co::alloc)(
77+
p = co::alloc(32);
78+
);
79+
BM_use(p);
80+
}
81+
82+
int main(int argc, char** argv) {
83+
flag::parse(argc, argv);
84+
bm::run_benchmarks();
85+
return 0;
86+
}
87+
```
88+
89+
- 上面的代码定义了一个名为 `malloc` 的基准测试组,组内用 `BM_add` 添加了 2 个基准测试。
90+
- 调用 `bm::run_benchmarks()`,会执行所有的基准测试代码。
91+
92+
{{< hint warning >}}
93+
上例中,若无 `BM_use(p)`,编译器可能认为 `p` 是未使用的变量,将相关的测试代码优化掉,导致无法测出准确的结果。
94+
{{< /hint >}}
95+
96+
97+
98+
99+
### 测试结果示例
100+
101+
![bm.png](/images/bm.png)
102+
103+
- 基准测试结果输出为 markdown 表格,可以轻松将测试结果复制到 markdown 文档中。
104+
- 多个 `BM_group` 会生成多个 markdown 表格。
105+
- 表格第 1 列是 group 内的所有基准测试,第 2 列是单次迭代用时(单位为纳秒),第 3 列是每秒迭代次数,第 4 列是性能提升倍数,以第一个基准测试为基准。

0 commit comments

Comments
 (0)