张添翼
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    > 本文作者 > 澄潭, 阿里云API网关软件工程师,Higress 项目主要贡献者 > 何良, Intel Web Platform Engineering 软件工程师, Wasm Micro Runtime 项目主要贡献者 本文介绍Higress将WASM插件的运行时从V8切换到WebAssembly Micro Runtime (WAMR)的最新进展。通过切换到WAMR并开启AOT模式大幅提升了WASM插件性能,从我们的测试中大部分插件平均有50%左右的性能提升,一些逻辑复杂的插件性能直接翻倍。 ## Higress WASM插件 Higress作为首个推出WASM扩展能力的云产品网关,从2022年就上线了WASM插件市场,我们使用WASM技术作为主要的网关扩展手段,是因为它能为用户带来的独特价值: 1. 工程可靠性:相比Lua等动态类型+解释执行语言,WASM可基于多种静态类型语言编译,可以做编译期检查,避免运行时出错把生产环境变成代码捉虫现场 2. 沙箱安全性:WASM插件运行在严格的虚拟机沙箱环境内,有自己的独立内存空间,不能直接访问外部内存,可以避免插件代码bug导致遭到缓冲区溢出、远程代码执行等攻击 3. 热更新:Higress基于Envoy的xDS机制,插件二进制和配置都可以独立热更新,不会引起连接断开,对WebSocket/gRPC等业务场景更友好 Higress站在Istio/Envoy的肩膀上,为WASM插件机制增加了三个核心能力: 1. 域名/路由级生效:Istio/Envoy自带的全局生效方式难以满足大部分场景需求,而基于Higress WASM sdk开发的插件可以做到这点,同时编译出的插件也跟Istio/Envoy生态兼容(仅全局生效) 2. Redis访问能力:提供了访问Redis的Host Function,插件代码可以基于Redis实现多种能力,例如全局限流,Session状态管理等 3. 虚拟机自愈机制:开发的插件逻辑中若出现了空指针访问、数组越界、内存泄漏等问题,将被运行时系统捕获,不会导致网关崩溃;Higress支持WASM模块异常后自动重启,并能在快速止血的同时,通过告警通知用户出现问题的代码堆栈 从Higress的企业用户看WASM插件技术的采用周期,已经跨域过鸿沟,步入早期采用大众阶段,核心的驱动力是性能红利带来的成本下降。用户使用WASM插件来开发满足自己特定业务需求的能力,对于鉴权、加解密、会话管理等逻辑在网关完成计算资源的卸载,无需后端服务处理,从而全局降低计算成本。 性能数据上,之前发表的这篇文档[《通过Higress Wasm插件3倍性能实现Spring Cloud Gateway功能》](https://mp.weixin.qq.com/s/hL8VjWSILLzvFwZ1yqJjMw )反馈了过去的性能提升成果。 而当Higress将WASM运行时从V8替换为WAMR后,WASM插件的性能对比之前的数据又有了大幅提升。 ## WASM运行时升级:从V8到WAMR ### V8存在的问题 WASM技术诞生于浏览器场景,作为Chromium的JS引擎,V8是最早支持WASM的运行时之一,V8引擎基于JIT模式运行WASM模块,有着很好的性能。但也存在以下问题: 1. V8项目复杂度很高:WASM相关实现跟JS处理逻辑有较多耦合,比如早期的Envoy WASM插件的一个[bug](https://bugs.chromium.org/p/v8/issues/detail?id=12592)就是V8为优化JS执行内存引入指针压缩导致 2. V8社区和Envoy社区之间缺少协作:Envoy目前对于V8的版本依赖还停留在2022年的提交,无法支持WASM GC等新特性,因为项目复杂度高,升级V8依赖的风险也很高 3. 客户端偏好:V8的用户和开发者大多来自客户端,考虑设备兼容性,更重视JIT模式的优化,AOT模式下性能提升不大,无法完全发挥WASM性能优势 ### WAMR的优势 WAMR是最早由Intel团队开发,在字节码联盟(Bytecode Alliance, 面向WASM软件生态的非盈利组织)下的一个广受欢迎的WebAssembly运行时开源项目。目前社区活跃的贡献者包含来自Intel、小米、亚马逊、索尼、Midokura、西门子、蚂蚁等公司的工程师。WAMR使用C语言开发,具有良好的平台适应性。支持解释模式、即时编译及预编译等模式运行Wasm模块,有着优良的性能,在多个公开性能测评报告中均表现优异,同时又极低的资源开销,可以在100KB内存中运行单个Wasm实例。 ### 性能对比 - **压测工具**:k6 - **服务器CPU型号**:Intel(R) Xeon(R) Platinum 8369B CPU @ 2.90GHz - **压测方式**:Higress启动2个worker线程,压测期间固定k6的压力,跑满两个线程 选取了部分Higress插件进行性能测试,情况如下: | 插件名称 | 插件用途 | V8<br>(ms) | WAMR<br>(ms) | 性能提升 | | -------- | -------- | -------- | -------- | -------- | | bot-detect | 基于正则识别阻止互联网爬虫对站点资源的爬取 | 1.25 | 0.64 | 95% | | hmac-auth | 基于HMAC算法为HTTP请求生成不可伪造的签名,<br>并基于签名实现身份认证和鉴权 | 4.44 | 3.25 | 36% | | jwt-auth | 基于JWT(JSON Web Tokens)进行认证鉴权 | 11.98 | 7.46 | 60% | | jwt-logout | 基于Redis实现JWT的弱状态管理,解决JWT无法登出的问题 | 14.08 | 8.44 | 66% | | key-auth | 基于 API Key 进行认证鉴权 | 1.66 | 1.16 | 43% | | oauth | 基于JWT进行OAuth2 Access Token签发 | 10.15 | 4.75 | 113% | 注:表格中的数据为单请求平均附加延时 整体来看,WASM指令越复杂的插件,WAMR的提升越明显。上述所有插件除`jwt-logout`是企业版插件未开源以外,其余插件均可以在Higress开源仓库目录下查看对应源码实现:https://github.com/alibaba/higress/tree/main/plugins/wasm-cpp/extensions 编译生成AOT文件,可以使用wamrc这个WAMR提供的官方编译工具:`wamrc --invoke-c-api-import -o plugin.aot plugin.wasm`。 为了生成的wasm文件可以兼容JIT模式,使用WAMR仓库下的脚本生成合并文件:`python3 wasm-micro-runtime/test-tools/append-aot-to-wasm/append_aot_to_wasm.py --aot plugin.aot --wasm plugin.wasm -o plugin.aot.wasm` 以提升最大的oauth插件为例,可以使用下述配置进行复现: **k6压测命令**:`k6 run --vus 300 ./script.js --duration 60s` **k6压测脚本**: ```js import http from 'k6/http'; import { check } from 'k6'; export default function () { const res = http.get('http://11.164.3.16:10000/',{headers: {'Authorization':'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6ImFwcGxpY2F0aW9uL2F0K2p3dCJ9.eyJhdWQiOiJ0ZXN0MiIsImNsaWVudF9pZCI6Ijk1MTViNTY0LTBiMWQtMTFlZS05YzRjLTAwMTYzZTEyNTBiNSIsImV4cCI6MTY2NTY3MzgyOSwiaWF0IjoxNjY1NjczODE5LCJpc3MiOiJIaWdyZXNzLUdhdGV3YXkiLCJqdGkiOiIxMDk1OWQxYi04ZDYxLTRkZWMtYmVhNy05NDgxMDM3NWI2M2MiLCJzY29wZSI6InRlc3QiLCJzdWIiOiJjb25zdW1lcjEifQ.LsZ6mlRxlaqWa0IAZgmGVuDgypRbctkTcOyoCxqLrHY'}}); check(res, { 'status was 200': (r) => r.status == 200 }); } ``` **envoy配置片段** ```yaml - name: envoy.filters.http.wasm typed_config: "@type": type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm config: name: "my_plugin" configuration: "@type": "type.googleapis.com/google.protobuf.StringValue" value: | { "consumers": [ { "name": "consumer1", "client_id": "9515b564-0b1d-11ee-9c4c-00163e1250b5", "client_secret": "9e55de56-0b1d-11ee-b8ec-00163e1250b5" } ], "clock_skew_seconds": 3153600000 } vm_config: runtime: envoy.wasm.runtime.wamr #runtime: envoy.wasm.runtime.v8 code: local: filename: "oauth.aot.wasm" allow_precompiled: true ``` ### 性能提升原因 主要的原因包含: 1. WAMR提供了深度优化的预编译的能力。在部署前,WAMR 将 Wasm opcodes 翻译为 IR,经过定制的优化流水线,生成指定平台的机器码。在运行时,执行预编译后的 Wasm 可以获得媲美 native binary 的性能。 2. WAMR 采用了高度优化的 FFI。有效降低在 host(c/c++) 和 guest(wasm) 两个世界间“穿梭”时需要的类型转换和内存拷贝的次数,减少不必要的损耗。 3. WAMR 可以智能感知平台的硬件加速能力并予以充分利用。比如当运行在X86平台时,WAMR实现了学术界最新提出的"segue"算法,利用GS寄存器作为寻址方法,提升了访问WASM线性空间的效率。 ## 未来展望 在Higress团队和WAMR团队之间的紧密协作下,除了在网关场景提升WASM插件性能,还带来了很多实用的新特性即将发布,敬请期待: 1. 支持生成CPU火焰图,例如下面是WASM插件中执行fibonacci递归看到的CPU火焰图: ![](https://img.alicdn.com/imgextra/i4/O1CN015ht4l81cPBc53EKYP_!!6000000003592-0-tps-1196-533.jpg) 2. 支持WASM插件中逻辑问题导致Crash后,插件日志中打印完整的函数堆栈,并可以通过WAMR提供的addr2line工具定位到源代码中的具体行号 3. 支持观测每个WASM插件模块的CPU和内存占用情况 4. 支持使用TypeScript编写WASM插件,完整语法支持 欢迎更多开发者一起参与Higress和WAMR开源社区,GitHub项目地址: Higress: https://github.com/alibaba/higress WAMR: https://github.com/bytecodealliance/wasm-micro-runtime

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully