{"id":26436785,"url":"https://github.com/xvertile/tcp-copy-benchmark","last_synced_at":"2025-03-18T08:18:15.092Z","repository":{"id":251281434,"uuid":"836945052","full_name":"xvertile/tcp-copy-benchmark","owner":"xvertile","description":"This project aims to benchmark various methods of copying data between TCP sockets, measuring both performance and CPU usage. The goal is to identify the fastest methods with the least amount of CPU overhead.","archived":false,"fork":false,"pushed_at":"2024-08-01T22:27:34.000Z","size":157,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-08-02T00:04:13.849Z","etag":null,"topics":["golang-tcp","io-benchmark","proxy-server","tcp","tcp-benchmarking","tcp-proxy"],"latest_commit_sha":null,"homepage":"","language":"Go","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/xvertile.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":"2024-08-01T22:17:35.000Z","updated_at":"2024-08-02T00:04:17.057Z","dependencies_parsed_at":"2024-08-02T00:14:23.910Z","dependency_job_id":null,"html_url":"https://github.com/xvertile/tcp-copy-benchmark","commit_stats":null,"previous_names":["xvertile/tcp-copy-benchmark"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xvertile%2Ftcp-copy-benchmark","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xvertile%2Ftcp-copy-benchmark/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xvertile%2Ftcp-copy-benchmark/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xvertile%2Ftcp-copy-benchmark/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xvertile","download_url":"https://codeload.github.com/xvertile/tcp-copy-benchmark/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244181351,"owners_count":20411605,"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","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":["golang-tcp","io-benchmark","proxy-server","tcp","tcp-benchmarking","tcp-proxy"],"created_at":"2025-03-18T08:18:14.551Z","updated_at":"2025-03-18T08:18:15.087Z","avatar_url":"https://github.com/xvertile.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TCP Data Copy Benchmark\n\nThis project aims to benchmark various methods of copying data between TCP sockets, measuring both performance and CPU usage. The goal is to identify the fastest methods with the least amount of CPU overhead.\n\n## Benchmark Methods\n\nThe benchmark tests the following methods:\n\n1. **IoPipe**: Utilizes the `io.Pipe` function for data transfer.\n2. **IoCopy**: Uses the `io.Copy` function.\n3. **IoCopyBuffer**: Uses `io.CopyBuffer` for buffered copying.\n4. **Syscall**: Direct system calls for data transfer.\n5. **IoCopyDirect**: Direct copy using `io.Copy`.\n6. **UnixSyscall**: Unix-specific system calls.\n7. **Bufio**: Buffered I/O using `bufio` package.\n8. **Splice**: Linux `splice` system call.\n9. **Sendfile**: Uses the `sendfile` system call.\n10. **ReadvWritev**: Vectorized I/O operations using `readv` and `writev`.\n\n## Benchmark Setup\nWe are using the `net` package to create a TCP server and client for data transfer. The server listens on a specified port, and the client connects to the server to send and receive data.\nthe payload size is set to 10Kb, and the number of iterations is set to 5000.\n\n```go\nconst (\n\taddress    = \"localhost:12345\"\n\tnumClients = 5000\n\tbufferSize = 32 * 1024\n)\n\nvar (\n\tmessage       = generateRandomString(10240) // Generate a 10kb random string\n\tmessageLength = len(message)\n)\n```\n\n## Benchmark Results\n\n### notes\nTested on a base Hetzner instance with the following specifications:\n- **CPU**: Intel Xeon (Skylake, IBRS, no TSX) (4) @ 2.099GHz\n- **RAM**: 7747MiB\n- **OS**: Ubuntu 22.04.4 LTS x86_64\n\nThe benchmark results are summarized as follows:\n\n- **Execution Times (ns/op)**:\n  ![Benchmark Results (ns/op)](https://raw.githubusercontent.com/xvertile/tcp-copy-benchmark/main/images/Benchmark%20Results%20ns_op.png)\n\n- **CPU Time (ms)**:\n  ![Benchmark CPU Time](https://raw.githubusercontent.com/xvertile/tcp-copy-benchmark/main/images/Benchmark%20CPU%20Time.png)\n\n### Top 3 Methods with Least CPU Usage\n\n1. **UnixSyscall**: 1580 ms\n2. **IoPipe**: 3140 ms\n3. **IoCopy**: 5940 ms\n\n![Top 3 Profiles with Least CPU Usage](https://raw.githubusercontent.com/xvertile/tcp-copy-benchmark/main/images/Top%203%20Profiles%20with%20Least%20CPU%20Usage.png)\n\n## Analysis\n\nThe `IoPipe` method stands out as a native solution working with the `net.Conn` interface, providing a balance between performance and CPU usage. However, methods such as `UnixSyscall` show the potential for further optimization by directly interfacing with the underlying system calls.\n\n## How to Run\n\nTo execute the benchmarks and analyze the results, use the following commands:\n\n```bash\ngo test -bench=. test/tcp_test.go \u0026\u0026 go run analyse.go\n```\n\nThe above commands will run the benchmark tests and generate a detailed analysis of each method's performance.\n\n## Conclusion\nThe native UnixSyscall method provides the best performance with the least CPU overhead. However, the IoPipe method is a close second and offers a more straightforward implementation. The choice of method depends on the specific requirements of the application, balancing performance and resource utilization.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxvertile%2Ftcp-copy-benchmark","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxvertile%2Ftcp-copy-benchmark","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxvertile%2Ftcp-copy-benchmark/lists"}