這裡分類和彙總了欣宸的全部原創(含配套原始碼):https://github.com/zq2599/blog_demos
sudo docker run \
--name postgres \
-p 5432:5432 \
-e POSTGRES_DB=quarkus_test \
-e POSTGRES_USER=quarkus \
-e POSTGRES_PASSWORD=123456 \
-d \
postgres:15
create table person (
id serial primary key,
name varchar(255),
gender varchar(255),
age int,
external_id int
);
insert into person(name, age, gender, external_id) values('John Smith', 25, 'MALE', 10);
insert into person(name, age, gender, external_id) values('Paul Walker', 65, 'MALE', 20);
insert into person(name, age, gender, external_id) values('Lewis Hamilton', 35, 'MALE', 30);
insert into person(name, age, gender, external_id) values('Veronica Jones', 20, 'FEMALE', 40);
insert into person(name, age, gender, external_id) values('Anne Brown', 60, 'FEMALE', 50);
insert into person(name, age, gender, external_id) values('Felicia Scott', 45, 'FEMALE', 60);
quarkus.datasource.db-kind=postgresql
quarkus.datasource.username=quarkus
quarkus.datasource.password=123456
quarkus.datasource.reactive.url=postgresql://192.168.0.99:5432/quarkus_test
我這裡,application.properties的路徑是:/home/lighthouse/config/quarkus/application.properties
檢查application.properties檔案的可讀性,執行以下命令設定
sudo chmod a+r application.properties
docker run -idt \
--name quarkus \
-p 8080:8080 \
-v /home/lighthouse/config/quarkus/application.properties:/application/config/application.properties \
bolingcavalry/quarkus-virual-threads-demo:x64-0.0.3
上述命令中,quarkus應用的映象bolingcavalry/quarkus-virual-threads-demo:x64-0.0.3是我提前準備好的,本篇只管使用即可,至於如何製作此映象,接下來的文章會有詳細說明
用sudo docker logs quarkus命令檢視啟動紀錄檔,如果啟動成功,資訊如下所示
__ ____ __ _____ ___ __ ____ ______
--/ __ \/ / / / _ | / _ \/ //_/ / / / __/
-/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/
2022-10-16 11:46:41,406 INFO [io.quarkus] (main) quarkus-virual-threads-demo 1.0-SNAPSHOT on JVM (powered by Quarkus 2.13.2.Final) started in 0.804s. Listening on: http://0.0.0.0:8080
2022-10-16 11:46:41,414 INFO [io.quarkus] (main) Profile prod activated.
2022-10-16 11:46:41,414 INFO [io.quarkus] (main) Installed features: [cdi, reactive-pg-client, resteasy-reactive, resteasy-reactive-jackson, smallrye-context-propagation, vertx]
對於虛擬執行緒的介面http://192.168.0.1:8080/vt/persons/1,可以嘗試多次存取,可見每次返回的虛擬執行緒Id都不一樣,而使用執行緒池的介面http://192.168.0.1:8080/pool/persons/1,多次存取,返回的始終是同一個執行緒的id
基本功能似乎沒有問題,接下來可以壓測了,用資料說話
import http from 'k6/http';
import { sleep, check } from 'k6';
export let options = {
vus: 30,
duration: '60s',
};
export default function () {
let r = Math.floor(Math.random() * 6) + 1;
const res = http.get(`http://192.168.0.1:8080/vt/persons/${r}`);
check(res, {
'is status 200': (res) => res.status === 200,
'body size is > 0': (r) => r.body.length > 0,
});
sleep(1);
}
docker run --rm -i loadimpact/k6 run - < k6-vt-docker.js
再來個狠的,並行數一口暴漲到5000試試,如下圖,這麼高的並行,已經無法保障100%的成功率了,好在95%也不低,另外平均等待時間從39毫秒暴漲到6.26秒,至於QPS當然不會太高,僅比300並行的時候高了百分之五十
import http from 'k6/http';
import { sleep, check } from 'k6';
export let options = {
vus: 30,
duration: '60s',
};
export default function () {
let r = Math.floor(Math.random() * 6) + 1;
const res = http.get(`http://192.168.0.1:8080/pool/persons/${r}`);
check(res, {
'is status 200': (res) => res.status === 200,
'body size is > 0': (r) => r.body.length > 0,
});
sleep(1);
}
30並行的壓測結果如下,和使用虛擬執行緒並無區別
300並行壓測結果如下,和使用虛擬執行緒並無區別
在響應式web服務中,並且關聯的資料庫操作也是響應式的,相比傳統的執行緒池模型,虛擬執行緒並未帶來明顯收益
不甘心啊,接下來就換成SpringBoot應用,模擬咱們日常開發最常見的資料庫存取場景,看看相比之下,差距有多大?
sudo docker stop quarkus
spring.datasource.url=jdbc:postgresql://42.193.162.141:5432/quarkus_test
spring.datasource.username=quarkus
spring.datasource.password=123456
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.datasource.hikari.maximum-pool-size=256
我這裡,application.properties的路徑是:/home/lighthouse/config/springboot/application.properties
檢查application.properties檔案的可讀性,執行以下命令設定
sudo chmod a+r application.properties
docker run -idt \
--name springboot \
-p 8080:8080 \
-v /home/lighthouse/config/springboot/application.properties:/application/BOOT-INF/classes/application.properties \
bolingcavalry/springboot-postgresql-demo:x64-0.0.3
上述命令中,springboot應用的映象bolingcavalry/springboot-postgresql-demo:x64-0.0.3是我提前準備好的,本篇只管使用即可,至於如何製作此映象,接下來的文章會有詳細說明
看到以下紀錄檔,證明應用啟動成功
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.4)
2022-10-16 13:01:01.022 INFO 1 --- [ main] .b.s.SpringbootPostgresqlDemoApplication : Starting SpringbootPostgresqlDemoApplication v0.0.1-SNAPSHOT using Java 11.0.13 on 5c25db81639e with PID 1 (/application/BOOT-INF/classes started by root in /application)
2022-10-16 13:01:01.025 INFO 1 --- [ main] .b.s.SpringbootPostgresqlDemoApplication : No active profile set, falling back to 1 default profile: "default"
2022-10-16 13:01:01.795 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-10-16 13:01:01.857 INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 47 ms. Found 1 JPA repository interfaces.
2022-10-16 13:01:02.392 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-10-16 13:01:02.405 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-10-16 13:01:02.405 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-10-16 13:01:02.492 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-10-16 13:01:02.492 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1364 ms
2022-10-16 13:01:02.701 INFO 1 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-10-16 13:01:02.741 INFO 1 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.11.Final
2022-10-16 13:01:02.867 INFO 1 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-10-16 13:01:02.942 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-10-16 13:01:03.164 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2022-10-16 13:01:03.179 INFO 1 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2022-10-16 13:01:03.688 INFO 1 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-10-16 13:01:03.695 INFO 1 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
import http from 'k6/http';
import { sleep, check } from 'k6';
export let options = {
vus: 30,
duration: '60s',
};
export default function () {
let r = Math.floor(Math.random() * 6) + 1;
const res = http.get(`http://192.168.0.1:8080/springboot/persons/${r}`);
check(res, {
'is status 200': (res) => res.status === 200,
'body size is > 0': (r) => r.body.length > 0,
});
sleep(1);
}