

# 管理 A/B 测试
<a name="ab-testing-manage"></a>

更新 A/B 测试以启动、暂停或停止。您可以使用该`UpdateABTest`操作来更改执行状态。

**注意**  
 AgentCore CLI 生命周期命令（`view``pause``resume`、`stop`、`promote`、、、`archive`）通过传递的**任务 ID** 而不是名称来标识测试。`-i, --id`从`agentcore run ab-test --json`（`id`字段）或通过列出测试来获取 ID `agentcore view ab-test`。作业 ID 使用格式`name-suffix`。例如：`customerSupportTargetTest-Ab1Cd2Ef3G`。Job 记录以 JSON 形式存储`.cli/jobs/ab-tests/`在本地项目中。

## 代码示例
<a name="manage-ab-test-examples"></a>

### 查看、暂停、恢复和停止
<a name="manage-ab-test-start-stop"></a>

**Example**  
列出所有 A/B 测试作业，或详细查看一个测试作业（还会显示调用网址和结果）：  

```
agentcore view ab-test
agentcore view ab-test <ab-test-id>
```
恢复暂停的测试：  

```
agentcore resume ab-test -i <ab-test-id>
```
暂停正在运行的测试。流量停止分割，所有请求都会路由到控制变体。可以恢复。  

```
agentcore pause ab-test -i <ab-test-id>
```
永久停止测试。所有流量都将恢复到控制变体。此操作无法撤消。  

```
agentcore stop ab-test -i <ab-test-id>
```
开始测试（将执行状态设置为`RUNNING`）：  

```
import boto3
import uuid

client = boto3.client("bedrock-agentcore", region_name="us-west-2")

response = client.update_ab_test(
    abTestId="customerSupportPromptTest-Ab1Cd2Ef3G",
    executionStatus="RUNNING",
    clientToken=str(uuid.uuid4()),
)
print(f"Execution status: {response['executionStatus']}")
```
暂停正在运行的测试：  

```
response = client.update_ab_test(
    abTestId="customerSupportPromptTest-Ab1Cd2Ef3G",
    executionStatus="PAUSED",
    clientToken=str(uuid.uuid4()),
)
print(f"Execution status: {response['executionStatus']}")
```
永久停止正在运行的测试：  

```
response = client.update_ab_test(
    abTestId="customerSupportPromptTest-Ab1Cd2Ef3G",
    executionStatus="STOPPED",
    clientToken=str(uuid.uuid4()),
)
print(f"Execution status: {response['executionStatus']}")
```

### 推广治疗变体
<a name="manage-ab-test-promote"></a>

当结果具有统计学意义时，推广治疗变体。 `promote`停止测试并将治疗变体写入`agentcore.json`；然后`agentcore deploy`运行以将其推出。

**Example**  

```
agentcore promote ab-test -i <ab-test-id>
agentcore deploy
```
SDK 没有单一的 “升级” 操作。停止测试，然后自己将获胜的变体应用于您的资源（例如，更新控制配置包或网关目标以匹配处理方法）。

### 删除 A/B 测试
<a name="manage-ab-test-remove"></a>

删除 A/B 测试及其关联的元数据。在删除测试之前，测试必须处于`STOPPED`执行状态。删除测试不会影响该测试引用的配置包、 AgentCore 网关或在线评估配置。

**Example**  

```
agentcore archive ab-test -i <ab-test-id>
```
停止然后删除：  

```
import boto3
import uuid

client = boto3.client("bedrock-agentcore", region_name="us-west-2")

# First stop the test if it's running
client.update_ab_test(
    abTestId="customerSupportPromptTest-Ab1Cd2Ef3G",
    executionStatus="STOPPED",
    clientToken=str(uuid.uuid4()),
)

# Then delete
response = client.delete_ab_test(
    abTestId="customerSupportPromptTest-Ab1Cd2Ef3G"
)
print(f"Deleted: {response['abTestId']}")
```

## 执行状态转换
<a name="manage-ab-test-execution-states"></a>


| 来源 | 目标 | 说明 | 
| --- | --- | --- | 
|  `NOT_STARTED`  |  `RUNNING`  | 开始测试。 AgentCore 网关开始分流量。 | 
|  `RUNNING`  |  `PAUSED`  | 暂停测试。交通停止分裂；所有交通路线都要控制。 | 
|  `PAUSED`  |  `RUNNING`  | 恢复暂停的测试。 | 
|  `RUNNING`  |  `STOPPED`  | 永久停止测试。流量路由立即结束。 | 
|  `PAUSED`  |  `STOPPED`  | 永久停止暂停的测试。 | 