{"id":25939358,"url":"https://github.com/hellyguo/kcp_latency_demo","last_synced_at":"2026-06-05T04:31:26.750Z","repository":{"id":58184245,"uuid":"527872346","full_name":"hellyguo/kcp_latency_demo","owner":"hellyguo","description":"a demo for use libkcp","archived":false,"fork":false,"pushed_at":"2023-06-25T08:33:41.000Z","size":97,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-02T14:09:02.494Z","etag":null,"topics":["demo","kcp","low-latency","udp"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hellyguo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-08-23T07:04:36.000Z","updated_at":"2025-02-10T10:51:21.000Z","dependencies_parsed_at":"2025-03-04T04:16:51.524Z","dependency_job_id":"7302fa17-8185-4dc7-9104-8798ad06beba","html_url":"https://github.com/hellyguo/kcp_latency_demo","commit_stats":null,"previous_names":["hellyguo/kcpdemo"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/hellyguo/kcp_latency_demo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hellyguo%2Fkcp_latency_demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hellyguo%2Fkcp_latency_demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hellyguo%2Fkcp_latency_demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hellyguo%2Fkcp_latency_demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hellyguo","download_url":"https://codeload.github.com/hellyguo/kcp_latency_demo/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hellyguo%2Fkcp_latency_demo/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27471482,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-03T02:00:06.367Z","response_time":57,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["demo","kcp","low-latency","udp"],"created_at":"2025-03-04T04:16:48.977Z","updated_at":"2025-12-03T07:19:59.237Z","avatar_url":"https://github.com/hellyguo.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 样本\n\n测试 [KCP](https://github.com/skywind3000/kcp/) 延时的样例程序\n\n**说明**\n\n为了达到最佳延时效果，按 `KCP` 的建议，每次都调用 `ikcp_flush` 函数进行强制发送。\n\n## 结构\n\n- `kcpsrv` 监听 `udp` `12345` 端口\n- `kcpcli` 监听 `udp` `54321` 端口\n\n1. `kcpcli` 连接到 `kcpsrv` 的监听端口上，每隔 `1ms` 上送执行时的时间戳。\n2. `kcpsrv` 接收到报文后，用当前时间与报文时间戳相减，得到单次通讯延时。\n3. `kcpsrv` 将 `KCP` 底层确认包发送到 `kcpcli` 的监听端口。\n\n## 测试结果\n\n- 纯 `kcp` 测试结果：[纯 kcp 实现](result.00.kcp.md)\n- `kcp` + `pthread` 测试结果：[kcp + pthread 实现](result.01.pthread.md)\n- `kcp` + `libev` 测试结果：[kcp + libev 实现](result.02.libev.md)\n- `kcp` + `libev` 带负载测试结果：[kcp + libev 带负载实现](result.03.libev.payload.md)\n\n从测试结果看，相当稳定。\n\n## 编译\n\n**前提**\n\n- 将 `libkcp.a` 及 `libev.a` 放于 `lib` 内\n- 或将 `libkcp.a` 及 `libev.a` 安装到 `/usr` 下\n\n```shell\n$\u003e mkdir build\n$\u003e cd build\n$\u003e cmake .. \u0026\u0026 make\n```\n\n可打开 `kcpdemo.h` 中的 `__DEBUG` 宏，添加更多输出信息\n\n## 执行\n\n开两个终端\n\n**终端1**\n\n```shell\n$\u003e cd build\n$\u003e ./kcpsrv[n]\nor\n$\u003e ./kcpsrv[n] \u003cclient ip(format: a.b.c.d)\u003e #n=4,5\n```\n\n**终端2**\n\n```shell\n$\u003e cd build\n$\u003e ./kcpcli[n]\nor\n$\u003e ./kcpcli[n] \u003cserver ip(format: a.b.c.d)\u003e #n=4,5\n```\n\n`n` 可取 `[0,1,2,3,4,5]`, 分别对应于:\n\n| 编号 | 含义 |\n|-----|-----|\n| 0 | 纯 `kcp` 实现 |\n| 1 | `kcp`+`pthread` 实现 |\n| 2 | `kcp`+`libev` 实现 |\n| 3 | `kcp`+`libev` 带负载实现 |\n| 4 | `kcp`+`libev` 支持远端实现 |\n| 5 | `kcp`+`libev` 支持远端实现，延时为客户端发出并收回时间 |\n\n## 最佳效果\n\n通过将应用绑定在不同的 `CPU` 核，得到最佳性能\n\n```shell\ntaskset -c 0,1 ./kcpsrv4 127.0.0.1 \u0026\ntaskset -c 4,5 ./kcpcli4 127.0.0.1\n\ntaskset -c 0,1 ./kcpsrv5 127.0.0.1 \u0026\ntaskset -c 4,5 ./kcpcli5 127.0.0.1\n```\n\n## TODO\n\n1. ~~添加负载，测试不同负载下的结果~~\n\n## 缺陷\n\n1. ~~目前无法支持太多次连续调用~~\n    \u003e 原因：`KCP` 的队列不支持多线程操作，需要在一个线程中操作\n2. ~~使用了线程锁，延迟降低严重~~\n    \u003e 原因：`kcpcli` 一次只读一个包过慢，一次性读取足够多就能加速\n\n## 感谢\n\n感谢[林伟](https://github.com/skywind3000/)大佬提供的优秀网络库！\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhellyguo%2Fkcp_latency_demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhellyguo%2Fkcp_latency_demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhellyguo%2Fkcp_latency_demo/lists"}