perf(外观专利): 品牌校验整批共用一次查询与一个超时预算

checkTitleText → checkTitleTexts:一批 10 行原先各带一个 total-timeout-millis(90s)
预算串行,上游 16890 劣化时批间隔被拉到分钟级;改成跨行去重一次查询、整批共用
deadline、结论按行归因、返回与入参同序同长。真正的价值是把爆炸半径从"行"收回"批"。

改动比预估大了一圈:三个回退点(标题缺失 / 标题识别失败 / 外观识别失败)原先也在
逐行发品牌查询,而回退行查的是整条原始标题,splitTitleText 能切出十几个词,逐行发
反而最贵。现在回退行只造结论不写行,与正常行汇进同一条 (row, LlmResult) 队列,
出口收敛到 applyLlmRowsInOneBatch 一处;pending.isEmpty() 的早退分支同样要刷,
否则回退行连品牌结论都拿不到。单参 applyResult 重载已删(零调用方 + 编译验证)。

行数不变、LLM 请求次数不变、代理提取次数不变 ⇒ 零成本。语义变化只有当初拍过的那条:
同批同品牌共享一次结论。

护栏 AppearancePatentBrandBatchTest 3 例 + BrandCheckClientTitleBatchTest 4 例,
断言各守一个坑:times(1)+never(checkTitleText) 证明合批真发生、两行结论不同证明
批内归因停在行;变异自证 6/6。后端全量 3631 例 Failures 0 Errors 0。
This commit is contained in:
2026-10-04 18:38:27 +08:00
parent 5111858346
commit 3432232ee1
5 changed files with 467 additions and 31 deletions
@@ -0,0 +1,173 @@
package com.nanri.aiimage.modules.appearancepatent.client;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.config.AppearancePatentProperties;
import com.nanri.aiimage.config.BrandCheckProperties;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
import com.nanri.aiimage.modules.brand.client.BrandCheckClient;
import com.nanri.aiimage.modules.usersecret.service.UserSecretUsageService;
import com.sun.net.httpserver.HttpServer;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* 外观专利合并一行的品牌校验必须走批接口。
*
* 原先 applyResult 里逐行 checkTitleText:一行一个 90 秒耗时预算,十行最坏九百秒,
* 上游 16890 劣化时批间隔被拉到分钟级(同项目货源侧已改成整批共用预算)。
*
* 这两条断言各自守一个坑:
* - times(1) + never():合批真的发生了,而不是把逐行调用换了个名字;
* - 两行结论不同:批内归因停在行,别行的侵权词没有渗进干净行。
*/
class AppearancePatentBrandBatchTest {
private BrandCheckClient brandCheckClient = mock(BrandCheckClient.class);
private final AppearancePatentLlmClient client = new AppearancePatentLlmClient(
new AppearancePatentProperties(),
new ObjectMapper(),
null,
brandCheckClient,
mock(UserSecretUsageService.class)
);
@Test
void mergedBatchAsksBrandCheckOnceAndAttributesPerRow() throws Exception {
// 桩按「本行的词」返回:含阿凡达的行 failedData 非空,另一行为空。
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"))).thenAnswer(invocation -> {
List<String> titles = invocation.getArgument(0);
List<BrandCheckClient.BrandCheckBatchResult> results = new ArrayList<>();
for (String title : titles) {
List<Object> failed = title.contains("阿凡达") ? List.of("阿凡达") : List.of();
results.add(new BrandCheckClient.BrandCheckBatchResult(List.of(title), failed, List.of()));
}
return results;
});
List<AppearancePatentResultRowDto> rows = List.of(row("1"), row("2"));
String dataText = """
{"data":[
{"row_id":"1","title":"阿凡达","appearance":"无侵权","result":"无侵权"},
{"row_id":"2","title":"小米","appearance":"无侵权","result":"无侵权"}
]}
""";
List<AppearancePatentResultRowDto> merged = client.mergeRowsFromDataText(rows, dataText);
verify(brandCheckClient, times(1)).checkTitleTexts(anyList(), eq("Terms"));
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
assertThat(merged).hasSize(2);
assertThat(merged.get(0).getTitleRisk()).isEqualTo("侵权");
assertThat(merged.get(1).getTitleRisk()).as("干净行不得继承别行的侵权词").isEqualTo("无侵权");
}
@Test
void batchWithNothingToCheckSendsNoBrandQueryAtAll() throws Exception {
List<AppearancePatentResultRowDto> rows = List.of(row("1"));
String dataText = """
{"data":[{"row_id":"1","title":"无","appearance":"无侵权","result":"无侵权"}]}
""";
client.mergeRowsFromDataText(rows, dataText);
verify(brandCheckClient, never()).checkTitleTexts(anyList(), anyString());
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
}
/**
* 回退行(商标识别失败 / 外观识别失败)也必须走批接口。
*
* 这条比正常行更贵:回退时查的是**整条原始标题**而不是模型抽出的关键词,
* splitTitleText 会把十几个词逐个排队,还各带一个耗时预算;
* 而回退行偏偏集中在上游劣化的那一轮——正是预算最紧的时候。
*/
@Test
void fallbackRowsAlsoAskBrandCheckOnceForTheWholeBatch() throws Exception {
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"))).thenAnswer(invocation -> {
List<String> titles = invocation.getArgument(0);
List<BrandCheckClient.BrandCheckBatchResult> results = new ArrayList<>();
for (String title : titles) {
List<Object> failed = title.contains("阿凡达") ? List.of("阿凡达") : List.of();
results.add(new BrandCheckClient.BrandCheckBatchResult(List.of(title), failed, List.of()));
}
return results;
});
HttpServer llmDown = startUnusableLlm();
try {
AppearancePatentProperties properties = new AppearancePatentProperties();
properties.setLlmHost("http://127.0.0.1:" + llmDown.getAddress().getPort());
properties.setLlmRetryTimes(1);
properties.setLlmReadTimeoutMillis(5_000);
AppearancePatentLlmClient inspectClient = new AppearancePatentLlmClient(
properties, new ObjectMapper(), null, brandCheckClient, mock(UserSecretUsageService.class));
List<AppearancePatentResultRowDto> rows = inspectClient.inspectRows(
List.of(
source("1", "阿凡达 玩具 手办 正版"),
source("2", "小米 充电宝 20000mAh 快充")
),
null,
"test-key");
@SuppressWarnings("unchecked")
ArgumentCaptor<List<String>> titlesCaptor = ArgumentCaptor.forClass(List.class);
verify(brandCheckClient, times(1)).checkTitleTexts(titlesCaptor.capture(), eq("Terms"));
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
assertThat(titlesCaptor.getValue())
.containsExactly("阿凡达 玩具 手办 正版", "小米 充电宝 20000mAh 快充");
assertThat(rows).hasSize(2);
for (AppearancePatentResultRowDto row : rows) {
assertThat(row.getAppearanceRisk()).isEqualTo("外观识别异常");
}
assertThat(rows.get(0).getTitleRisk()).isEqualTo("侵权");
assertThat(rows.get(1).getTitleRisk()).as("回退行的归因同样停在行").isEqualTo("无侵权");
} finally {
llmDown.stop(0);
}
}
/** LLM 一律回 502:让每一行都落到回退分支,只观察品牌校验的调用形状。 */
private HttpServer startUnusableLlm() throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/v1/chat/completions", exchange -> {
byte[] bytes = "{\"error\":{\"message\":\"bad gateway\"}}".getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(502, bytes.length);
try (OutputStream os = exchange.getResponseBody()) {
os.write(bytes);
}
});
server.start();
return server;
}
private AppearancePatentResultRowDto source(String id, String title) {
AppearancePatentResultRowDto row = row(id);
row.setTitle(title);
row.setUrl("https://img.example.com/" + id + ".jpg");
return row;
}
private AppearancePatentResultRowDto row(String id) {
AppearancePatentResultRowDto row = new AppearancePatentResultRowDto();
row.setId(id);
row.setAsin("B000000" + id);
row.setCountry("US");
return row;
}
}
@@ -10,7 +10,9 @@ import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
@@ -87,8 +89,10 @@ class AppearancePatentLlmClientTest {
@Test
void callBrandCheckOnlyWhenTitleAndAppearanceArePresent() throws Exception {
BrandCheckClient brandCheckClient = mock(BrandCheckClient.class);
when(brandCheckClient.checkTitleText("阿凡达", "Terms"))
.thenReturn(new BrandCheckClient.BrandCheckBatchResult(List.of("阿凡达"), List.of(), List.of()));
// 一行也走批接口:逐行调用等于把 90 秒耗时预算按行复制
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms")))
.thenReturn(List.of(new BrandCheckClient.BrandCheckBatchResult(
List.of("阿凡达"), List.of(), List.of())));
AppearancePatentLlmClient llmClient = new AppearancePatentLlmClient(
new AppearancePatentProperties(),
new ObjectMapper(),
@@ -106,7 +110,8 @@ class AppearancePatentLlmClientTest {
assertThat(merged).hasSize(1);
assertThat(merged.get(0).getTitleRisk()).isEqualTo("无侵权");
verify(brandCheckClient).checkTitleText("阿凡达", "Terms");
verify(brandCheckClient).checkTitleTexts(List.of("阿凡达"), "Terms");
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
}
@Test