【Rust日报】2020-09-14 测试数据表明, Rust 可以帮你省钱!

时间:2022-07-25
本文章向大家介绍【Rust日报】2020-09-14 测试数据表明, Rust 可以帮你省钱!,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

从压测 actix v2/v3 (Rust) 和 Http4k, Ktor(Kotlin) 微服务上所学到的

作者分别使用 Kotlin 和 Rust 的不同框架写了同样的微服务, 并且对其进行了压测对比. 之所有没有直接引用别人的压测结果(例如著名的 techempower: Web Framework Benchmarks), 而且是选择自己完全手写项目来测试,是因为:

  1. 作者想用接近生产级别的代码进行对比, 这些代码包括了合理的错误处理, HTTP状态码, JSON序列化, 参数处理等.
  2. 除了 req/s和延迟指标,作者想要更多的维度指标的对比.例如 CPU, 内存 等等.
  3. 使用正式环境压测,例如在 k8s中, 内存和 CPU 都是有限制的.
  4. 自己的存储, 例如例子中使用自己的 Elasticsearch.

原文中有大量的指标图对比,感兴趣的同学可以查看原文. 作者甚至粗略地计算了他们各自的成本:

Http4k

Ktor

Actix

CPU time per request

560 µs

460 µs

170 µs

Cost per billion requests

$4.3

$3.5

$1.3

在这个一切皆为资源的云时代, Rust可以帮你节省不少成本呢!

原文链接: https://matej.laitl.cz/bench-rust-kotlin-microservices/

techempower: Web Framework Benchmarks: https://www.techempower.com/benchmarks/

Actix-web 3.0 正式发布

Actix-web 3.0 正式发布了!

是目前为止最为稳定的版本,强烈建议大家升级到最新版本.

https://paper.dropbox.com/published/Announcing-Actix-Web-v3.0-QOXXb1lXgTubzXHzUq9ONY5

Bunt,一个简便易用的命令行颜色库

使用起来非常简单.

// Style tags will color/format text between the tags.
bunt::println!("I really like {$yellow}lemons{/$}! Like, {$blue+italic}a lot{/$}.");

// To style a single argument, you can also use the `{[style]...}` syntax. This
// can be combined with style tags.
let v = vec![1, 2, 3];
bunt::println!("Here is some data: {[green]:?}. {$bold}Length: {[cyan]}{/$}", v, v.len());

移植 PineTime Watch Face(C to Rust)

该文介绍了如果一步一步把一个基于 LGVL 的 PineTime (一款 linux 智能手表) app 从 C 移植到 Rust 上(RIOT系统上). 详细程度到代码级别, 采用 C 代码和 Rust 左右对比的模式,让你清清楚楚,从头到尾的了解他是如何从 C 移植到 Rust 上.

多达 30 步:

  1. Function Declaration
  2. Variable Declaration
  3. Null Pointers
  4. Import C Functions into Rust
  5. Numeric Types
  6. Pass Strings from Rust to C
  7. Pointer Dereferencing
  8. Return Value
  9. C to Rust Conversion: First Version

省略...

  1. What's Next
  2. References

每一步都会列出原始代码和转换后的代码:

原始的 C 代码:

lv_obj_t *screen_time_create(home_time_widget_t *ht) {
   // Create a label for time (00:00)
   lv_obj_t *scr = lv_obj_create(NULL, NULL);
   lv_obj_t *label1 = lv_label_create(scr, NULL);

   lv_label_set_text(label1, "00:00");
   lv_obj_set_width(label1, 240);
   lv_obj_set_height(label1, 200);
   ht->lv_time = label1;
   ...
   return scr;
}

转换后的 Rust 代码:

fn create_widgets(widgets: &mut WatchFaceWidgets) ->LvglResult<()> {
   // Create a label for time (00:00)
   let scr = widgets.screen;
   let label1 = label::create(scr, ptr::null()) ? ;

   label::set_text(label1, strn!("00:00")) ? ;
   obj::set_width(label1, 240) ? ;
   obj::set_height(label1, 200) ? ;
   widgets.time_label = label1;
   ...
   Ok(())
}

https://lupyuen.github.io/pinetime-rust-riot/articles/watch_face