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:
+173
@@ -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;
|
||||
}
|
||||
}
|
||||
+8
-3
@@ -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
|
||||
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
package com.nanri.aiimage.modules.brand.client;
|
||||
|
||||
import com.nanri.aiimage.config.BrandCheckProperties;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* 一批标题的品牌校验(外观专利热路径:原先每行各调一次 checkTitleText,
|
||||
* 一行一个 90 秒预算,上游 16890 劣化时十行就是九百秒)。
|
||||
*
|
||||
* 合批之后必须钉住三件事,缺一条就是静默错判:
|
||||
* ① 结论按**本行自己的词**归因,别行的侵权词不得渗进本行;
|
||||
* ② 同一个词在一批里只查一次(省的是上游请求数,不是行数);
|
||||
* ③ 空标题行的词集合仍为空——buildTitleRisk 靠 brands().isEmpty() 回退成标题本身,
|
||||
* 如果被整批去重后的词表污染,判定会从「标题原文」变成「无侵权」。
|
||||
*/
|
||||
class BrandCheckClientTitleBatchTest {
|
||||
|
||||
@Test
|
||||
void verdictIsAttributedToEachTitleOwnWords() throws Exception {
|
||||
Map<String, String> hits = new ConcurrentHashMap<>();
|
||||
hits.put("阿凡达", "{\"faild_data\":[\"阿凡达\"],\"query_faild_data\":[]}");
|
||||
HttpServer server = startBrandCheckServer(brandResponses(hits));
|
||||
try {
|
||||
List<BrandCheckClient.BrandCheckBatchResult> results =
|
||||
newClient(server, 3).checkTitleTexts(List.of("阿凡达,玩具", "小米,充电宝"), "Terms");
|
||||
|
||||
assertThat(results).hasSize(2);
|
||||
assertThat(results.get(0).hasFailedData()).as("第 1 行含侵权词").isTrue();
|
||||
assertThat(results.get(1).hasFailedData()).as("第 2 行不得继承第 1 行的侵权结论").isFalse();
|
||||
assertThat(results.get(1).hasQueryFailedData()).isFalse();
|
||||
assertThat(results.get(1).brands()).containsExactly("小米", "充电宝");
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void sameWordAcrossTitlesIsQueriedOnce() throws Exception {
|
||||
// 计数器必须按品牌各自一个:共用一个计数器会让第二个词一进门就落在 count>1,
|
||||
// 被判成"重试过",量出来的请求数就不是去重结果了。
|
||||
Map<String, AtomicInteger> counters = new ConcurrentHashMap<>();
|
||||
HttpServer server = startBrandCheckServer(exchange -> {
|
||||
String brand = readBrand(exchange);
|
||||
int count = counters.computeIfAbsent(brand, key -> new AtomicInteger()).incrementAndGet();
|
||||
respond(exchange, count == 1 ? "{\"faild_data\":[],\"query_faild_data\":[]}"
|
||||
: "{\"faild_data\":[],\"query_faild_data\":[\"" + brand + "\"]}");
|
||||
});
|
||||
try {
|
||||
List<BrandCheckClient.BrandCheckBatchResult> results =
|
||||
newClient(server, 3).checkTitleTexts(List.of("阿凡达,玩具", "阿凡达,手机", "阿凡达,周边"), "Terms");
|
||||
|
||||
assertThat(counters.get("阿凡达").get()).as("跨行同一个词只查一次").isEqualTo(1);
|
||||
assertThat(counters).hasSize(4);
|
||||
assertThat(counters.keySet()).containsExactlyInAnyOrder("阿凡达", "玩具", "手机", "周边");
|
||||
assertThat(results).hasSize(3);
|
||||
assertThat(results).allSatisfy(result -> {
|
||||
assertThat(result.hasQueryFailedData()).as("每个词首轮即成功").isFalse();
|
||||
assertThat(result.hasFailedData()).isFalse();
|
||||
});
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void blankTitleKeepsEmptyWordSetEvenWhenOtherRowsHaveWords() throws Exception {
|
||||
HttpServer server = startBrandCheckServer(brandResponses(
|
||||
Map.of("阿凡达", "{\"faild_data\":[\"阿凡达\"],\"query_faild_data\":[]}")));
|
||||
try {
|
||||
List<BrandCheckClient.BrandCheckBatchResult> results =
|
||||
newClient(server, 3).checkTitleTexts(List.of("", "阿凡达"), "Terms");
|
||||
|
||||
assertThat(results.get(0).brands()).as("空标题行的词集合必须为空,否则判定从标题原文变成无侵权").isEmpty();
|
||||
assertThat(results.get(0).hasFailedData()).isFalse();
|
||||
assertThat(results.get(1).brands()).containsExactly("阿凡达");
|
||||
assertThat(results.get(1).hasFailedData()).isTrue();
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryFailureOnOneRowDoesNotMarkOtherRowsFailed() throws Exception {
|
||||
// 上游对「任天堂」持续返回查询失败:只有含该词的行判 BRAND_QUERY_FAILED,
|
||||
// 同批里查成功的行仍判无侵权——归因粒度必须停在行。
|
||||
Map<String, String> hits = new ConcurrentHashMap<>();
|
||||
hits.put("任天堂", "{\"faild_data\":[],\"query_faild_data\":[\"任天堂\"]}");
|
||||
HttpServer server = startBrandCheckServer(brandResponses(hits));
|
||||
try {
|
||||
List<BrandCheckClient.BrandCheckBatchResult> results =
|
||||
newClient(server, 2).checkTitleTexts(List.of("任天堂,主机", "小米,充电宝"), "Terms");
|
||||
|
||||
assertThat(results.get(0).hasQueryFailedData()).isTrue();
|
||||
assertThat(results.get(1).hasQueryFailedData()).as("别行的查询失败不得扩散").isFalse();
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
private com.sun.net.httpserver.HttpHandler brandResponses(Map<String, String> canned) {
|
||||
return exchange -> {
|
||||
String brand = readBrand(exchange);
|
||||
respond(exchange, canned.getOrDefault(brand, "{\"faild_data\":[],\"query_faild_data\":[]}"));
|
||||
};
|
||||
}
|
||||
|
||||
private String readBrand(com.sun.net.httpserver.HttpExchange exchange) throws IOException {
|
||||
String body = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
|
||||
int start = body.indexOf("\"brand\"");
|
||||
int from = body.indexOf('"', start + 7);
|
||||
int to = body.indexOf('"', from + 1);
|
||||
return body.substring(from + 1, to);
|
||||
}
|
||||
|
||||
private BrandCheckClient newClient(HttpServer server, int retryTimes) {
|
||||
BrandCheckProperties props = new BrandCheckProperties();
|
||||
props.setBaseUrl("http://127.0.0.1:" + server.getAddress().getPort());
|
||||
props.setPath("/brand_check");
|
||||
props.setRetryTimes(retryTimes);
|
||||
props.setRetryIntervalMillis(1);
|
||||
return new BrandCheckClient(props, null);
|
||||
}
|
||||
|
||||
private HttpServer startBrandCheckServer(com.sun.net.httpserver.HttpHandler handler) throws IOException {
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/brand_check", handler);
|
||||
server.start();
|
||||
return server;
|
||||
}
|
||||
|
||||
private void respond(com.sun.net.httpserver.HttpExchange exchange, String body) throws IOException {
|
||||
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
|
||||
exchange.getResponseHeaders().set("Content-Type", "application/json; charset=utf-8");
|
||||
exchange.sendResponseHeaders(200, bytes.length);
|
||||
try (OutputStream os = exchange.getResponseBody()) {
|
||||
os.write(bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user