Skip to main content

2 posts tagged with "python"

View All Tags

Rust(actix) 和 Java(webflux) 性能对比

· 4 min read
Wayne
Wayne

Rust(actix) 和 Java(webflux) 性能对比

Rust

Rust 看起来 非常小而美,在我看来 几个优点非常看好。

  • Cargo 包管理:设计简洁,上手难度低
  • Rust 基于 C++,这就带来了很强的性能,且 rust 语法中出现不安全的内存操作,则会直接编译不通过,这点就很让人安心。
  • 跨平台编译二进制文件(这个我非常在意,也是我之前为什么选择 Golang)

Web Benchmark

Rust: https://actix.rs/
Java: https://docs.spring.io/spring-framework/reference/web/webflux/new-framework.html
K6: https://k6.io/

VUs = 500

Rust:

     data_received..................: 5.2 MB 29 kB/s
data_sent......................: 2.8 MB 16 kB/s
http_req_blocked...............: avg=266.24ms min=0s med=0s max=32.02s p(90)=0s p(95)=0s
http_req_connecting............: avg=266.24ms min=0s med=0s max=32.02s p(90)=0s p(95)=0s
http_req_duration..............: avg=309.86µs min=0s med=0s max=4.52s p(90)=692.3µs p(95)=796.5µs
{ expected_response:true }...: avg=174.08µs min=0s med=0s max=1.71ms p(90)=692.3µs p(95)=796.36µs
http_req_failed................: 0.01% 4 out of 33318
http_req_receiving.............: avg=38.61µs min=0s med=0s max=1.48ms p(90)=40.22µs p(95)=508.7µs
http_req_sending...............: avg=4.33µs min=0s med=0s max=1.02ms p(90)=0s p(95)=0s
http_req_tls_handshaking.......: avg=0s min=0s med=0s max=0s p(90)=0s p(95)=0s
http_req_waiting...............: avg=266.91µs min=0s med=0s max=4.52s p(90)=586.6µs p(95)=719.6µs
http_reqs......................: 33318 184.522672/s
iteration_duration.............: avg=1.26s min=1s med=1s max=33.02s p(90)=1s p(95)=1s
iterations.....................: 33318 184.522672/s
vus............................: 3 min=3 max=499
vus_max........................: 500 min=500 max=500


running (3m00.6s), 000/500 VUs, 33318 complete and 0 interrupted iterations
default ✓ [======================================] 000/500 VUs 3m0s

Java:

     data_received..................: 5.0 MB 28 kB/s
data_sent......................: 3.6 MB 20 kB/s
http_req_blocked...............: avg=12.31µs min=0s med=0s max=10.9ms p(90)=0s p(95)=0s
http_req_connecting............: avg=9.79µs min=0s med=0s max=2.13ms p(90)=0s p(95)=0s
http_req_duration..............: avg=528.32µs min=0s med=550.6µs max=158.21ms p(90)=1.1ms p(95)=1.33ms
{ expected_response:true }...: avg=528.32µs min=0s med=550.6µs max=158.21ms p(90)=1.1ms p(95)=1.33ms
http_req_failed................: 0.00% 0 out of 42266
http_req_receiving.............: avg=63.55µs min=0s med=0s max=1.49ms p(90)=208.9µs p(95)=526µs
http_req_sending...............: avg=5.71µs min=0s med=0s max=4.74ms p(90)=0s p(95)=0s
http_req_tls_handshaking.......: avg=0s min=0s med=0s max=0s p(90)=0s p(95)=0s
http_req_waiting...............: avg=459.05µs min=0s med=523.7µs max=158.21ms p(90)=1.01ms p(95)=1.22ms
http_reqs......................: 42266 234.328464/s
iteration_duration.............: avg=1s min=1s med=1s max=1.16s p(90)=1s p(95)=1s
iterations.....................: 42266 234.328464/s
vus............................: 2 min=2 max=499
vus_max........................: 500 min=500 max=500


running (3m00.4s), 000/500 VUs, 42266 complete and 0 interrupted iterations
default ✓ [======================================] 000/500 VUs 3m0s

VUs = 1500

Rust

     data_received..................: 7.1 MB 117 kB/s
data_sent......................: 3.9 MB 64 kB/s
http_req_blocked...............: avg=36.9µs min=0s med=0s max=4ms p(90)=0s p(95)=0s
http_req_connecting............: avg=34.73µs min=0s med=0s max=4ms p(90)=0s p(95)=0s
http_req_duration..............: avg=374.98µs min=0s med=522.9µs max=14.25ms p(90)=624.6µs p(95)=733.4µs
{ expected_response:true }...: avg=374.98µs min=0s med=522.9µs max=14.25ms p(90)=624.6µs p(95)=733.4µs
http_req_failed................: 0.00% 0 out of 45690
http_req_receiving.............: avg=35.15µs min=0s med=0s max=1.99ms p(90)=16.4µs p(95)=508µs
http_req_sending...............: avg=4.35µs min=0s med=0s max=1.76ms p(90)=0s p(95)=0s
http_req_tls_handshaking.......: avg=0s min=0s med=0s max=0s p(90)=0s p(95)=0s
http_req_waiting...............: avg=335.46µs min=0s med=516.35µs max=14.25ms p(90)=602.7µs p(95)=688.81µs
http_reqs......................: 45690 749.430697/s
iteration_duration.............: avg=1s min=1s med=1s max=1.01s p(90)=1s p(95)=1s
iterations.....................: 45690 749.430697/s
vus............................: 60 min=24 max=1498
vus_max........................: 1500 min=1500 max=1500


running (1m01.0s), 0000/1500 VUs, 45690 complete and 0 interrupted iterations
default ✓ [======================================] 0000/1500 VUs 1m0s

Java:

     data_received..................: 5.4 MB 89 kB/s
data_sent......................: 3.9 MB 64 kB/s
http_req_blocked...............: avg=35.32µs min=0s med=0s max=6.03ms p(90)=0s p(95)=0s
http_req_connecting............: avg=33.13µs min=0s med=0s max=6.03ms p(90)=0s p(95)=0s
http_req_duration..............: avg=393.34µs min=0s med=518.59µs max=175.24ms p(90)=779.4µs p(95)=1.05ms
{ expected_response:true }...: avg=393.34µs min=0s med=518.59µs max=175.24ms p(90)=779.4µs p(95)=1.05ms
http_req_failed................: 0.00% 0 out of 45691
http_req_receiving.............: avg=40.46µs min=0s med=0s max=3.75ms p(90)=29.1µs p(95)=508.49µs
http_req_sending...............: avg=3.83µs min=0s med=0s max=1.7ms p(90)=0s p(95)=0s
http_req_tls_handshaking.......: avg=0s min=0s med=0s max=0s p(90)=0s p(95)=0s
http_req_waiting...............: avg=349.04µs min=0s med=217.5µs max=174.24ms p(90)=676.1µs p(95)=1ms
http_reqs......................: 45691 749.01963/s
iteration_duration.............: avg=1s min=1s med=1s max=1.17s p(90)=1s p(95)=1s
iterations.....................: 45691 749.01963/s
vus............................: 68 min=24 max=1498
vus_max........................: 1500 min=1500 max=1500

Conclusion

整体来看, Java 这边在Spring这个大框架下 只慢了一丢丢,大多都输在延迟上,但是也是有两大神器提供的加持,Java 23 和 Reactive, 所以只能说是没输。

而 Rust 这边,则是以微小领先取得了胜利,但是并没有拉开太大的差距。且第一轮对决出现了 4 个 failed(可能是测试条件问题)。

所以选择哪个语言和框架,区别应该在于场景下,比如想要更小的体积和更快的启动速度,那么 Rust 是最优选。

但是如果想要生态支持和更多开发者的协作,可能还得是 Java。

Pip 正在集成到 Linux repository 中

· 2 min read
Wayne
Wayne

Pip 正在集成到 Linux repository 中

现在 再使用 pip install 可能会 提示以下消息

error: externally-managed-environment

× This environment is externally managed
╰─>
The system-wide python installation should be maintained using the system
package manager (apk) only.

If the package in question is not packaged already (and hence installable via
"apk add py3-somepackage"), please consider installing it inside a virtual
environment, e.g.:

python3 -m venv /path/to/venv
. /path/to/venv/bin/activate
pip install mypackage

To exit the virtual environment, run:

deactivate

The virtual environment is not deleted, and can be re-entered by re-sourcing
the activate file.

To automatically manage virtual environments, consider using pipx (from the
pipx package).

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

你可能需要 将 pip install somepackage 替换为 apk add py3-somepackage (Alpine Linux).

其实这是一个好消息

  1. python 的包本身就是安装到系统的某个目录中,而不是安装到项目目录中。系统软件包接管 这很合理。
  2. 对于国内这样的糟糕的网络,镜像站则是必须的,对于 pip 来说,可以少设置一层 镜像或代理,而且多亏了这些镜像站,速度非常的快。