安全与效率优化,引入Redis,包名修改
This commit is contained in:
@@ -0,0 +1,290 @@
|
||||
package com.linkerbot.vibemake.api;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import com.linkerbot.vibemake.bean.Contracts.ProjectView;
|
||||
import com.linkerbot.vibemake.bean.entity.User;
|
||||
import com.linkerbot.vibemake.common.PageResponse;
|
||||
import com.linkerbot.vibemake.service.*;
|
||||
import com.linkerbot.vibemake.service.admin.AdminManagementService;
|
||||
import com.linkerbot.vibemake.service.support.ProjectActors;
|
||||
|
||||
import org.apache.ibatis.executor.statement.StatementHandler;
|
||||
import org.apache.ibatis.plugin.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.*;
|
||||
import org.springframework.context.annotation.*;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
|
||||
import java.lang.management.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@SpringBootTest(
|
||||
properties = {
|
||||
"spring.datasource.url=jdbc:h2:mem:query-efficiency;MODE=MySQL;DB_CLOSE_DELAY=-1",
|
||||
"spring.datasource.username=sa",
|
||||
"spring.datasource.password=",
|
||||
"spring.datasource.driver-class-name=org.h2.Driver",
|
||||
"vibemake.ai.enabled=false",
|
||||
"vibemake.oss.enabled=false",
|
||||
"logging.level.root=WARN"
|
||||
})
|
||||
@ActiveProfiles("test")
|
||||
@Import({WorkflowTest.Config.class, QueryEfficiencyTest.Config.class})
|
||||
class QueryEfficiencyTest {
|
||||
private static final String MYSQL_URL = System.getenv("BENCHMARK_MYSQL_URL");
|
||||
private static final String DATABASE =
|
||||
"vibemake_benchmark_" + UUID.randomUUID().toString().replace("-", "");
|
||||
private static boolean databaseCreated;
|
||||
|
||||
@DynamicPropertySource
|
||||
static void isolatedMysql(DynamicPropertyRegistry registry) throws Exception {
|
||||
if (MYSQL_URL == null || MYSQL_URL.isBlank()) return;
|
||||
if (!MYSQL_URL.startsWith("jdbc:mysql:"))
|
||||
throw new IllegalArgumentException("基准测试只接受 MySQL URL");
|
||||
try (java.sql.Connection connection = bootstrapConnection();
|
||||
java.sql.Statement statement = connection.createStatement()) {
|
||||
if (!"MySQL".equals(connection.getMetaData().getDatabaseProductName()))
|
||||
throw new IllegalStateException("需要 MySQL");
|
||||
statement.execute(
|
||||
"CREATE DATABASE "
|
||||
+ DATABASE
|
||||
+ " CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
|
||||
databaseCreated = true;
|
||||
}
|
||||
String plain = MYSQL_URL.split("\\?", 2)[0];
|
||||
String isolated =
|
||||
plain.substring(0, plain.lastIndexOf('/') + 1)
|
||||
+ DATABASE
|
||||
+ (MYSQL_URL.contains("?") ? "?" + MYSQL_URL.split("\\?", 2)[1] : "");
|
||||
registry.add("spring.datasource.url", () -> isolated);
|
||||
registry.add("spring.datasource.username", () -> System.getenv("BENCHMARK_MYSQL_USERNAME"));
|
||||
registry.add(
|
||||
"spring.datasource.password",
|
||||
() -> System.getenv().getOrDefault("BENCHMARK_MYSQL_PASSWORD", ""));
|
||||
registry.add("spring.datasource.driver-class-name", () -> "com.mysql.cj.jdbc.Driver");
|
||||
}
|
||||
|
||||
private static java.sql.Connection bootstrapConnection() throws java.sql.SQLException {
|
||||
return java.sql.DriverManager.getConnection(
|
||||
MYSQL_URL,
|
||||
System.getenv("BENCHMARK_MYSQL_USERNAME"),
|
||||
System.getenv().getOrDefault("BENCHMARK_MYSQL_PASSWORD", ""));
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void removeOnlyIsolatedDatabase() throws Exception {
|
||||
if (!databaseCreated) return;
|
||||
try (java.sql.Connection connection = bootstrapConnection();
|
||||
java.sql.Statement statement = connection.createStatement()) {
|
||||
statement.execute("DROP DATABASE " + DATABASE);
|
||||
}
|
||||
}
|
||||
|
||||
private void resetSchema() throws Exception {
|
||||
if (!databaseCreated) {
|
||||
SchemaSupport.reset(jdbc);
|
||||
return;
|
||||
}
|
||||
// 连接目标必须是本次创建的随机临时库,禁止对外部配置指定的业务库执行 DDL。
|
||||
try (java.sql.Connection connection =
|
||||
java.util.Objects.requireNonNull(jdbc.getDataSource()).getConnection()) {
|
||||
assertThat(connection.getCatalog()).isEqualTo(DATABASE);
|
||||
}
|
||||
for (String table :
|
||||
List.of(
|
||||
"project_import_task",
|
||||
"project_like",
|
||||
"project_comment",
|
||||
"project",
|
||||
"sys_user")) jdbc.execute("DROP TABLE IF EXISTS " + table);
|
||||
java.nio.file.Path docs = java.nio.file.Path.of("../docs");
|
||||
if (!java.nio.file.Files.exists(docs)) docs = java.nio.file.Path.of("docs");
|
||||
for (String name : List.of("database.md", "admin-database.md")) {
|
||||
java.util.regex.Matcher tables =
|
||||
java.util.regex.Pattern.compile("CREATE TABLE [\\s\\S]*?;")
|
||||
.matcher(java.nio.file.Files.readString(docs.resolve(name)));
|
||||
while (tables.find()) jdbc.execute(tables.group());
|
||||
}
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
static class Config {
|
||||
@Bean
|
||||
SqlCounter sqlCounter() {
|
||||
return new SqlCounter();
|
||||
}
|
||||
}
|
||||
|
||||
@Intercepts(
|
||||
@Signature(
|
||||
type = StatementHandler.class,
|
||||
method = "prepare",
|
||||
args = {java.sql.Connection.class, Integer.class}))
|
||||
static class SqlCounter implements Interceptor {
|
||||
final AtomicInteger count = new AtomicInteger();
|
||||
|
||||
@Override
|
||||
public Object intercept(Invocation invocation) throws Throwable {
|
||||
if (((StatementHandler) invocation.getTarget())
|
||||
.getBoundSql()
|
||||
.getSql()
|
||||
.stripLeading()
|
||||
.toUpperCase(Locale.ROOT)
|
||||
.startsWith("SELECT")) count.incrementAndGet();
|
||||
return invocation.proceed();
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired SqlCounter sql;
|
||||
@Autowired JdbcTemplate jdbc;
|
||||
@Autowired ProjectService projects;
|
||||
@Autowired UserService users;
|
||||
@Autowired InteractionService interactions;
|
||||
@Autowired AdminManagementService management;
|
||||
private User owner;
|
||||
|
||||
@BeforeEach
|
||||
void seed() throws Exception {
|
||||
resetSchema();
|
||||
owner = users.login("efficiency@example.invalid");
|
||||
for (int index = 1; index <= 21; index++) {
|
||||
String code = String.format("%012d", index);
|
||||
jdbc.update(
|
||||
"INSERT INTO"
|
||||
+ " project(project_code,owner_user_id,name,description,project_type,gitea_owner,gitea_repo_name,gitea_repo_id,repo_status,publish_status,published_commit_sha,published_at,source_project_id,created_at,updated_at)"
|
||||
+ " VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)",
|
||||
code,
|
||||
owner.getId(),
|
||||
"项目" + index,
|
||||
"性能验证",
|
||||
"GENERAL",
|
||||
"vibemake",
|
||||
"p_" + code,
|
||||
(long) index,
|
||||
"READY",
|
||||
"PUBLISHED",
|
||||
"a".repeat(40),
|
||||
java.sql.Timestamp.valueOf("2026-01-01 00:00:00"),
|
||||
index == 1 ? null : 1L);
|
||||
if (index > 1) {
|
||||
jdbc.update(
|
||||
"INSERT INTO project_like(project_id,user_id,created_at)"
|
||||
+ " VALUES(?,?,CURRENT_TIMESTAMP)",
|
||||
index,
|
||||
owner.getId());
|
||||
jdbc.update(
|
||||
"INSERT INTO"
|
||||
+ " project_import_task(task_code,project_code,operator_user_code,source_repository_id,source_repository_url,source_default_branch,status,phase,warnings_json,created_at,updated_at)"
|
||||
+ " VALUES(?,?,?,?,?,?,?,?,'[]',CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)",
|
||||
String.format("%032d", index),
|
||||
code,
|
||||
owner.getUserCode(),
|
||||
(long) index,
|
||||
"https://github.com/example/r" + index,
|
||||
"main",
|
||||
"SUCCEEDED",
|
||||
"READY");
|
||||
}
|
||||
}
|
||||
for (int index = 1; index <= 20; index++)
|
||||
jdbc.update(
|
||||
"INSERT INTO"
|
||||
+ " project_comment(comment_code,project_id,user_id,content,deleted,created_at,updated_at)"
|
||||
+ " VALUES(?,2,?,?,0,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)",
|
||||
String.format("%032d", index),
|
||||
owner.getId(),
|
||||
"评论" + index);
|
||||
for (int index = 1; index <= 19; index++)
|
||||
users.login("efficiency-" + index + "@example.invalid");
|
||||
sql.count.set(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void batchesKeepResponsesAndBoundQueryCounts() {
|
||||
PageResponse<ProjectView> result = projects.list(null, null, null, 1, 20, owner.getId());
|
||||
int projectQueries = sql.count.getAndSet(0);
|
||||
assertThat(result.items()).hasSize(20);
|
||||
assertThat(result.items())
|
||||
.allSatisfy(
|
||||
view -> {
|
||||
assertThat(view.owner().userCode()).isEqualTo(owner.getUserCode());
|
||||
assertThat(view.likeCount()).isEqualTo(1);
|
||||
assertThat(view.liked()).isTrue();
|
||||
assertThat(view.sourceProjectCode()).isEqualTo("000000000001");
|
||||
assertThat(view.sourceRepositoryUrl())
|
||||
.startsWith("https://github.com/example/");
|
||||
});
|
||||
assertThat(interactions.list("000000000002", 1, 20).items()).hasSize(20);
|
||||
int commentQueries = sql.count.getAndSet(0);
|
||||
try (ProjectActors.Scope scope =
|
||||
ProjectActors.enter(new ProjectActors.AdminActor(owner.getUserCode(), "管理员"))) {
|
||||
assertThat(management.users(1, 20, null, null, null, null, null).items())
|
||||
.hasSize(20)
|
||||
.anySatisfy(
|
||||
view -> {
|
||||
assertThat(view.userCode()).isEqualTo(owner.getUserCode());
|
||||
assertThat(view.projectCount()).isEqualTo(21);
|
||||
});
|
||||
}
|
||||
int userQueries = sql.count.getAndSet(0);
|
||||
System.out.printf(
|
||||
"QUERY_COUNTS projects=%d comments=%d users=%d%n",
|
||||
projectQueries, commentQueries, userQueries);
|
||||
if (!Boolean.getBoolean("vibemake.benchmark.baseline")) {
|
||||
assertThat(projectQueries).isLessThanOrEqualTo(8);
|
||||
assertThat(commentQueries).isLessThanOrEqualTo(4);
|
||||
assertThat(userQueries).isLessThanOrEqualTo(3);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void repeatableListBenchmark() throws Exception {
|
||||
for (int i = 0; i < 15; i++) projects.list(null, null, null, 1, 20, owner.getId());
|
||||
benchmark(1);
|
||||
benchmark(4);
|
||||
}
|
||||
|
||||
private void benchmark(int concurrency) throws Exception {
|
||||
System.gc();
|
||||
Thread.sleep(100);
|
||||
for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans())
|
||||
if (pool.getType() == MemoryType.HEAP) pool.resetPeakUsage();
|
||||
long[] samples = new long[100];
|
||||
sql.count.set(0);
|
||||
try (java.util.concurrent.ExecutorService pool =
|
||||
java.util.concurrent.Executors.newFixedThreadPool(concurrency)) {
|
||||
List<java.util.concurrent.Future<?>> tasks = new ArrayList<>();
|
||||
for (int i = 0; i < samples.length; i++) {
|
||||
int sample = i;
|
||||
tasks.add(
|
||||
pool.submit(
|
||||
() -> {
|
||||
long start = System.nanoTime();
|
||||
projects.list(null, null, null, 1, 20, owner.getId());
|
||||
samples[sample] = System.nanoTime() - start;
|
||||
}));
|
||||
}
|
||||
for (java.util.concurrent.Future<?> task : tasks) task.get();
|
||||
}
|
||||
Arrays.sort(samples);
|
||||
long heapPoolPeaks =
|
||||
ManagementFactory.getMemoryPoolMXBeans().stream()
|
||||
.filter(pool -> pool.getType() == MemoryType.HEAP)
|
||||
.mapToLong(pool -> pool.getPeakUsage().getUsed())
|
||||
.sum();
|
||||
System.out.printf(
|
||||
Locale.ROOT,
|
||||
"LIST_BENCHMARK samples=100 concurrency=%d sqlPerRequest=%d p95Ms=%.3f"
|
||||
+ " heapPoolPeakBytes=%d%n",
|
||||
concurrency,
|
||||
sql.count.get() / samples.length,
|
||||
samples[94] / 1_000_000.0,
|
||||
heapPoolPeaks);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user