来源:丛林 medium.com
大量的文章评估了一系列技术(包括 node.js、deno、bun、rust、go、spring、python 等)在简单的“hello world”场景中的性能。虽然这些文章获得了好评,但有一个共同点:忽略了现实场景开发中的复杂性 。
本文旨在通过现实场景的视角剖析各种技术,在这种特殊情况下,我们深入研究以下常见用例:
从 authorization header 中提取一个jwt。
验证jwt并从声明中提取用户的电子邮件。
使用提取的电子邮件执行mysql查询。
最后,返回用户的记录。
虽然这个场景看起来似乎也很简单,但它概括了 web 开发领域中经常遇到的现实挑战。
介绍
在本文中,我们将深入探讨所有同级产品之间的友好比较,即具有「物理线程、虚拟线程和 webflux 的 springboot」 ,重点关注它们在特定用例场景中的性能。我们已经探索了标准 springboot 应用程序如何与 webflux 相媲美,但现在,我们引入一个关键的区别:
带有虚拟线程的 spring boot
我们熟悉 springboot,但有一点不同——它在虚拟线程而不是传统的物理线程上运行。虚拟线程是并发领域的游戏规则改变者。这些轻量级线程简化了开发、维护和调试高吞吐量并发应用程序的复杂任务。
虽然虚拟线程仍然在底层操作系统线程上运行,但它们带来了显着的效率改进。当虚拟线程遇到阻塞 i/o 操作时,java 运行时会暂时挂起它,从而释放关联的操作系统线程来为其他虚拟线程提供服务。这个优雅的解决方案优化了资源分配并增强了整体应用程序响应能力。
考虑到这些有趣的设置,让我们更深入地研究我们的性能比较。撰写本文是为了解决最常见的请求之一,即查看物理、虚拟和 webflux 在实际用例中的比较。
测试环境及软件版本
我们的性能测试是在配备 16gb ram 的 macbook pro m1 上进行的,确保了可靠的测试平台。用于这些测试的软件堆栈包括:
springboot 3.1.3(在java 20上运行)
启用预览模式以获得虚拟线程的强大功能
jjwt用于jwt验证和解码,增强我们应用程序的安全性。
mysql-connector-java 用于执行 mysql 查询,维护数据完整性和一致性。
负载测试和 jwt
为了评估我们的应用程序在不同负载下的性能,我们使用了开源负载测试工具 bombardier。我们的测试场景涉及预先创建的 100000 个 jwt 列表。在测试过程中,bombardier 从该池中随机选择 jwt,并将它们包含在 http 请求的授权标头中。
mysql 数据库架构
用于这些性能测试的 mysql 数据库有一个名为 users 的表。该表设计有 6 列,足以模拟我们应用程序中的真实数据交互,使我们能够评估它们的响应能力和可扩展性。
mysql> desc users;+--------+--------------+------+-----+---------+-------+| field | type | null | key | default | extra |+--------+--------------+------+-----+---------+-------+| email | varchar(255) | no | pri | null | || first | varchar(255) | yes | | null | || last | varchar(255) | yes | | null | || city | varchar(255) | yes | | null | || county | varchar(255) | yes | | null | || age | int | yes | | null | |+--------+--------------+------+-----+---------+-------+6 rows in set (0.00 sec)
用户数据库已准备好包含 100000 条用户记录的初始数据集。
mysql> select count(*) from users;+----------+| count(*) |+----------+| 99999 |+----------+1 row in set (0.01 sec)
在我们对 springboot 物理线程、虚拟线程和 webflux 进行友好性能评估的背景下,了解关键的数据关系至关重要。具体来说,在json web token(jwt)有效负载中,每个电子邮件条目直接对应于存储在 mysql 数据库中的一条用户记录。
代码
springboot(物理线程)
配置信息
server.port=3000spring.datasource.url= jdbc//localhost:3306/testdb?usessl=false&allowpublickeyretrieval=truespring.datasource.username= dbuserspring.datasource.password= dbpwdspring.jpa.hibernate.ddl-auto= updatespring.datasource.driver-class-name=com.mysql.cj.jdbc.driverspring.jpa.properties.hibernate.dialect=org.hibernate.dialect.mysqldialect
实体类
package com.example.demo;import jakarta.persistence.entity;import jakarta.persistence.table;import jakarta.persistence.generatedvalue;import jakarta.persistence.generationtype;import jakarta.persistence.id;@entity@table(name = users)public class user { @id private string email; private string first; private string last; private string city; private string county; private int age; public string getid() { return email; } public void setid(string email) { this.email = email; } public string getfirst() { return first; } public void setfirst(string name) { this.first = name; } public string getlast() { return last; } public void setlast(string name) { this.last = name; } public string getemail() { return email; } public void setemail(string email) { this.email = email; } public string getcity() { return city; } public void setcity(string city) { this.city = city; } public string getcounty() { return county; } public void setcounty(string county) { this.county = county; } public int getage() { return age; } public void setage(int age) { this.age = age; }}
启动类
package com.example.demo;import org.springframework.boot.springapplication;import org.springframework.boot.autoconfigure.springbootapplication;import org.springframework.boot.web.embedded.tomcat.tomcatprotocolhandlercustomizer;import org.springframework.context.annotation.bean;@springbootapplicationpublic class userapplication { public static void main(string[] args) { springapplication.run(userapplication.class, args); }}
controller层
package com.example.demo;import org.springframework.web.bind.annotation.getmapping;import org.springframework.web.bind.annotation.requestheader;import org.springframework.http.responseentity;import org.springframework.http.httpstatus;import org.springframework.http.httpheaders;import org.springframework.web.bind.annotation.restcontroller;import org.springframework.beans.factory.annotation.autowired;import java.util.optional;import io.jsonwebtoken.jwts;import io.jsonwebtoken.jws;import io.jsonwebtoken.claims;import io.jsonwebtoken.signaturealgorithm;import io.jsonwebtoken.security.keys;import java.security.key;import com.example.demo.userrepository;import com.example.demo.user;@restcontrollerpublic class usercontroller { @autowired userrepository userrepository; private signaturealgorithm sa = signaturealgorithm.hs256; private string jwtsecret = system.getenv(jwt_secret); @getmapping(/) public user handlerequest(@requestheader(httpheaders.authorization) string authhdr) { string jwtstring = authhdr.replace(bearer,); claims claims = jwts.parser() .setsigningkey(jwtsecret.getbytes()) .parseclaimsjws(jwtstring).getbody(); optional user = userrepository.findbyid((string)claims.get(email)); return user.get(); }}
接口类
package com.example.demo;import org.springframework.data.repository.crudrepository;import com.example.demo.user;public interface userrepository extends crudrepository {}
springboot(虚拟线程)
其余代码基本照搬上述 「物理线程」 , 启动类修改如下:
package com.example.demo;import org.springframework.boot.springapplication;import org.springframework.boot.autoconfigure.springbootapplication;import org.springframework.boot.web.embedded.tomcat.tomcatprotocolhandlercustomizer;import org.springframework.context.annotation.bean;import java.util.concurrent.executors;@springbootapplicationpublic class userapplication { public static void main(string[] args) { springapplication.run(userapplication.class, args); } @bean public tomcatprotocolhandlercustomizer protocolhandlervirtualthreadexecutorcustomizer() { return protocolhandler -> { protocolhandler.setexecutor(executors.newvirtualthreadpertaskexecutor()); }; }}
springboot(webflux)
server.port=3000spring.r2dbc.url=r2dbc//localhost:3306/testdb?allowpublickeyretrieval=true&ssl=falsespring.r2dbc.username=dbuserspring.r2dbc.password=dbpwdspring.r2dbc.pool.initial-size=10spring.r2dbc.pool.max-size=10
启动类
package webfluxdemo;import org.springframework.boot.springapplication;import org.springframework.boot.autoconfigure.springbootapplication;import org.springframework.context.annotation.bean;import org.springframework.core.io.classpathresource;import org.springframework.r2dbc.connection.init.connectionfactoryinitializer;import org.springframework.r2dbc.connection.init.resourcedatabasepopulator;import org.springframework.web.reactive.config.enablewebflux;import io.r2dbc.spi.connectionfactory;@enablewebflux@springbootapplicationpublic class userapplication { public static void main(string[] args) { springapplication.run(userapplication.class, args); }}
controller层代码
package webfluxdemo;import org.springframework.beans.factory.annotation.autowired;import org.springframework.http.httpstatus;import org.springframework.web.bind.annotation.getmapping;import org.springframework.web.bind.annotation.pathvariable;import org.springframework.web.bind.annotation.requestbody;import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.requestparam;import org.springframework.web.bind.annotation.responsestatus;import org.springframework.web.bind.annotation.restcontroller;import org.springframework.web.bind.annotation.requestheader;import org.springframework.http.httpheaders;import webfluxdemo.user;import webfluxdemo.userservice;import io.jsonwebtoken.jwts;import io.jsonwebtoken.jws;import io.jsonwebtoken.claims;import io.jsonwebtoken.signaturealgorithm;import io.jsonwebtoken.security.keys;import java.security.key;import reactor.core.publisher.flux;import reactor.core.publisher.mono;@restcontroller@requestmapping(/)public class usercontroller { @autowired userservice userservice; private signaturealgorithm sa = signaturealgorithm.hs256; private string jwtsecret = system.getenv(jwt_secret); @getmapping(/) @responsestatus(httpstatus.ok) public mono getuserbyid(@requestheader(httpheaders.authorization) string authhdr) { string jwtstring = authhdr.replace(bearer,); claims claims = jwts.parser() .setsigningkey(jwtsecret.getbytes()) .parseclaimsjws(jwtstring).getbody(); return userservice.findbyid((string)claims.get(email)); }}
接口类
package webfluxdemo;import org.springframework.data.r2dbc.repository.r2dbcrepository;import org.springframework.stereotype.repository;import webfluxdemo.user;public interface userrepository extends r2dbcrepository {}
service层代码
package webfluxdemo;import java.util.optional;import org.springframework.beans.factory.annotation.autowired;import org.springframework.stereotype.service;import webfluxdemo.user;import webfluxdemo.userrepository;import reactor.core.publisher.flux;import reactor.core.publisher.mono;@servicepublic class userservice { @autowired userrepository userrepository; public mono findbyid(string id) { return userrepository.findbyid(id); }}
结果
为了评估性能,我们进行了一系列严格的测试。每个测试由100万个请求组成,我们评估了它们在不同并发连接级别(50、100和300)下的性能。
现在,让我们深入研究结果,以图表形式呈现:
所用时间对比 每秒请求数 最小延迟 10%延迟 25%延迟 平均延迟 中位数延迟 75%延迟 90%延迟 99%延迟 最高延迟 平均cpu使用率 平均内存使用率
分析
在此设置中,即使用mysql驱动程序时,虚拟线程提供的性能最低、webflux保持遥遥领先。
芯盾时代实力入选首批 “网络安全能力评价工作组”成员单位
经典 向前:魅蓝note5上手评测
锐龙5000系列处理器新版微代码四方面升级
关于IOT还是自主研发芯片的分析和研究
智信通携手靠谱金服,开拓汽车金融新时代
SpringBoot物理线程、虚拟线程、Webflux性能比较
音频功率放大器的CMOS电路设计
华为Mate 30 5G到底怎么样
鸿蒙系统怎么截屏 5种方法的详细介绍
车载电源树参考设计白皮书
LED蓝光对人眼会造成危害?大谣言!
NVIDIA Jetson TX2 NX GPU助力微链DaoAI加速数据处理
华米AMAZFIT运动手环多少钱:699元起 可心电ID身份识别
土壤ph速测仪的性能及参数
电池的充电倍率指的是什么?
苹果iphone 5S:Say Goodbye,就再不相见
浩亭助力中国生产接轨工业4.0
RFID无线射频识别技术基本原理
互相成就+1!新思科技携手AMD,在EPYC 9004上加速复杂芯片设计
左蓝微电子发布DiFEM模组 开启国产射频模组发展新格局