> For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt.

# 标记渲染流水线

[Lynx Pipeline](/next/zh/guide/spec.md#lynx-pipeline) 定义了从渲染触发到屏幕显示的完整流程。如果你关注某些关键组件的渲染性能，可以通过设置该组件的 `__lynx_timing_flag` 属性来标记其 Lynx Pipeline，从而监测性能表现。

当标记的 Lynx Pipeline 执行完成并刷新屏幕显示后，会生成一个 [`PipelineEntry`](/next/zh/api/lynx-api/performance-api/performance-entry/pipeline-entry.md) 性能事件。你可以通过 [`PerformanceObserver`](/next/zh/api/lynx-api/performance-api/performance-observer.md) 获取该事件。

## 使用规则

- `__lynx_timing_flag` 属性必须为非空字符串。空值或非法类型将不会触发 [`PerformanceObserver`](/next/zh/api/lynx-api/performance-api/performance-observer.md) 的回调。
- 当 `__lynx_timing_flag` 属性值为 `__lynx_timing_actual_fmp` 时，会额外生成 [`MetricActualFmpEntry`](/next/zh/api/lynx-api/performance-api/performance-entry/metric-actual-fmp-entry.md) 性能事件。

## 使用示例



1. **标记节点**：在目标组件上设置 `__lynx_timing_flag` 属性。当该节点完成渲染时，框架将自动采集其 Lynx Pipeline 的性能数据。
2. **获取数据**：通过 [`lynx.performance.createObserver()`](/next/zh/api/lynx-api/lynx/lynx-performance/create-observer.md) 注册观察者（`PerformanceObserver`），可以获取相关性能数据（`PipelineEntry`）。

**下面是一个示例: performance-api**

**Entry:** `src/pipeline-entry`
**Bundle:** `dist/pipeline-entry.lynx.bundle`

```tsx {11-21,34}
import { root, useEffect, useState } from "@lynx-js/react";
import type { PerformanceEntry, PipelineEntry } from "@lynx-js/types";
import { ScrollItem } from "../common/scroll-item/index.jsx";
import "./index.scss";

export default function PipelineEntryExample() {
  const [pipelineEntry, setPipelineEntry] = useState("");
  const [myName, setMyName] = useState<string | undefined>(undefined);

  useEffect(() => {
    "background-only";
    // 1. Create a performance observer.
    const observer = lynx.performance.createObserver((entry: PerformanceEntry) => {
      if (entry.entryType == "pipeline") {
        // 3. Received "pipeline" event of "myPipeline".
        const pipelineEntry = entry as PipelineEntry;
        // `PerformanceEntry.identifier` is equal to `view.__lynx_timing_flag`.
        if (pipelineEntry.identifier == "myNamePipeline") {
          setPipelineEntry(JSON.stringify(pipelineEntry, null, 4));
        }
      }
    });
    // 2. Register to listen to the "pipeline" event.
    observer.observe(["pipeline"]);

    // Update real data after simulating a network request.
    setTimeout(() => {
      setMyName("PipelineEntry");
    }, 2000);
  }, []);

  return (
    <view className="container">
      <text className="title" __lynx_timing_flag={myName ? "myNamePipeline" : ""}>Hello {myName}~</text>
      <ScrollItem title="PipelineEntry" value={pipelineEntry} />
    </view>
  );
}

root.render(<PipelineEntryExample />);

if (import.meta.webpackHot) {
  import.meta.webpackHot.accept();
}

```

v0.8.0



## 注意事项

### 1. 多个组件设置相同的 `__lynx_timing_flag` 属性

```ts
export default function App() {
  const [showImage, setShowImage] = useState(false);

  useEffect(() => {
    setTimeout(() => {
      setShowImage(true);
    }, 3000);
  }, []);

  return (
    <view className="container">
      <text __lynx_timing_flag="__lynx_timing_actual_fmp">Hello World</text>
      {showImage && <image __lynx_timing_flag="__lynx_timing_actual_fmp" src="xxxx.png" />}
    </view>
  );
}
```

在这种情况下，仅会统计第一个上屏组件的 Lynx Pipeline 数据：

1. 计算一次 `ActualFMP` 并发送一次 [`MetricActualFmpEntry`](/next/zh/api/lynx-api/performance-api/performance-entry/metric-actual-fmp-entry.md)。
2. 发送一次 [`PipelineEntry`](/next/zh/api/lynx-api/performance-api/performance-entry/pipeline-entry.md)。

如果希望统计两个组件都完成渲染的时机，可以使用不同的 `__lynx_timing_flag`。

### 2. 同一个组件多次渲染

```ts
export default function App(this: any) {
  return (
    <view className="container">
      {
         // needShow: true -> false -> true
         data.needShow ? <text __lynx_timing_flag="__lynx_timing_actual_fmp">{data.msg}</text> : null
      }
    </view>
  );
}
```

在这种情况下，仅统计该组件第一次上屏的 Lynx Pipeline 数据：

1. 计算一次 `ActualFMP` 并发送一次 [`MetricActualFmpEntry`](/next/zh/api/lynx-api/performance-api/performance-entry/metric-actual-fmp-entry.md)。
2. 发送一次 [`PipelineEntry`](/next/zh/api/lynx-api/performance-api/performance-entry/pipeline-entry.md)。

如果希望统计每个组件的渲染性能，可以按如下方式实现：

```ts
let isFirst = true;
export default function App(this: any) {
  return (
    <view className="container">
      {
         // needShow: true -> false -> true
         data.needShow ? <text __lynx_timing_flag={"__lynx_timing_actual_fmp" + (isFirst ? "" : +data.id)}>{data.msg}</text> : null
      }
    </view>
  );
}
```

## 兼容性

**Compatibility Table**

**Query:** `lynx-api.performance-api.timing-flag`

[Source](https://github.com/lynx-family/lynx-website/tree/main/packages/lynx-compat-data/lynx-api/performance-api/timing-flag.json)

| Feature | Android | HarmonyOS | iOS | macOS | Windows | Web |
| --- | --- | --- | --- | --- | --- | --- |
| timing-flag | Yes (3.1) | Yes (3.4) | Yes (3.1) | Yes (3.7) | Yes (3.7) | No |

