# 获取性能数据

[调试诊断](https://developers.weixin.qq.com/console/devtools/debug?utm_source=api_tools)

> 接口应在服务器端调用，不可在前端（小程序、网页、APP等）直接调用，具体可参考[接口调用指南](/doc/oplatform/developers/dev/guide.md)。

接口英文名：getPerformance

该接口用于获取性能数据

## 1. 调用方式

### HTTPS 调用

```bash
POST https://api.weixin.qq.com/wxaapi/log/get_performance?access_token=ACCESS_TOKEN
```

> **支持加密请求：** 本接口支持服务通信二次加密和签名，可有效防止数据篡改与泄露。[查看详情](/miniprogram/dev/server/getting_started/api_signature)

### 云调用

- 调用方法：operation.getPerformance

- 出入参和 HTTPS 调用相同，调用方式可查看 [云调用](/doc/oplatform/developers/dev/cloudCall.md) 说明文档。

### 第三方调用

- 本接口支持第三方平台代商家调用。

- 该接口所属的权限集 id 为：18

- 服务商获得其中之一权限集授权后，可通过使用 [authorizer_access_token](/doc/oplatform/developers/dev/AuthorizerAccessToken.md) 代商家进行调用，具体可查看 [第三方调用](/doc/oplatform/Third-party_Platforms/2.0/Before_Develop/call_interface.md) 说明文档。

## 2. 请求参数

### 查询参数 `Query String Parameters`

```json
[
  {
    "参数名": "access_token",
    "类型": "string",
    "必填": "是",
    "说明": "接口调用凭证，可使用 [access_token](/miniprogram/dev/server/API/mp-access-token/api_getaccesstoken)、[authorizer_access_token](/doc/oplatform/openApi/ticket-token/api_getauthorizeraccesstoken.md)"
  }
]
```

### 请求体 `Request Payload`

```json
[
  {
    "参数名": "cost_time_type",
    "类型": "number",
    "必填": "是",
    "说明": "可选值 1（启动总耗时）， 2（下载耗时），3（初次渲染耗时）"
  },
  {
    "参数名": "default_start_time",
    "类型": "number",
    "必填": "是",
    "说明": "查询开始时间"
  },
  {
    "参数名": "default_end_time",
    "类型": "number",
    "必填": "是",
    "说明": "查询结束时间"
  },
  {
    "参数名": "device",
    "类型": "string",
    "必填": "是",
    "说明": "系统平台，可选值 \"@_all:\"（全部），1（IOS）， 2（android）"
  },
  {
    "参数名": "is_download_code",
    "类型": "string",
    "必填": "是",
    "说明": "是否下载代码包，当 type 为 1 的时候才生效，可选值 \"@_all:\"（全部），1（是）， 2（否）"
  },
  {
    "参数名": "scene",
    "类型": "string",
    "必填": "是",
    "说明": "访问来源，当 type 为 1 或者 2 的时候才生效，通过 getSceneList 接口获取"
  },
  {
    "参数名": "networktype",
    "类型": "string",
    "必填": "是",
    "说明": "网络环境, 当 type 为 2 的时候才生效，可选值 \"@_all:\"，wifi, 4g, 3g, 2g"
  }
]
```

## 3. 返回参数

### 返回体 `Response Payload`

```json
[
  {
    "参数名": "errcode",
    "类型": "number",
    "说明": "错误码"
  },
  {
    "参数名": "errmsg",
    "类型": "string",
    "说明": "错误信息"
  },
  {
    "参数名": "default_time_data",
    "类型": "string",
    "说明": "错误查询数据(json字符串，结构如下所述的 strbody)"
  },
  {
    "参数名": "compare_time_data",
    "类型": "string",
    "说明": "比较数据"
  }
]
```

## 4. 注意事项

#### strbody 的结构

| 属性 | 类型 | 说明 |
| ---- | ---- | ---- |
| ref_date  | string | 日期 |
| cost_time_type  | number |  意思同参数里面的 cost_time_type |
| cost_time  | number |  耗时(毫秒) |

## 5. 代码示例

### 5.1 HTTPQ请求示例

请求示例

```json
{
  "cost_time_type": 2,
  "default_start_time": 1572339403,
  "default_end_time": 1574931403,
  "device": "@_all",
  "networktype": "@_all",
  "scene": "@_all",
  "is_download_code": "@_all"
}
```

返回示例

```json
{
  "default_time_data": "{\"list\":[{\"ref_date\":\"20191029\",\"cost_time_type\":2,\"cost_time\":1533},{\"ref_date\":\"20191030\",\"cost_time_type\":2,\"cost_time\":1682}]}",
  "compare_time_data": "",
  "errcode": 0
}
```

### 5.2 云函数调用示例

请求示例

```json
const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV,
})
exports.main = async (event, context) => {
  try {
    const result = await cloud.openapi.operation.getPerformance({
        "device": '@_all',
        "networktype": '@_all',
        "scene": '@_all',
        "costTimeType": 2,
        "defaultStartTime": 1572339403,
        "defaultEndTime": 1574931403,
        "isDownloadCode": '@_all'
      })
    return result
  } catch (err) {
    return err
  }
}
```

返回示例

```json
const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV,
})
exports.main = async (event, context) => {
  try {
    const result = await cloud.openapi.operation.getPerformance({
        "errcode": 0,
        "defaultTimeData": '{\'list\":[{\"ref_date\":\"20191029\",\"cost_time_type\":2,\"cost_time\":1533},{\"ref_date\":\"20191030\",\"cost_time_type\":2,\"cost_time\":1682}]}",
        "compareTimeData": ''
      })
    return result
  } catch (err) {
    return err
  }
}
```

## 6. 错误码

以下是本接口的错误码列表，其他错误码可参考 [通用错误码](/doc/oplatform/developers/errCode/README.md)；调用接口遇到报错，可使用官方提供的 [API 诊断工具](https://developers.weixin.qq.com/console/devtools/debug?utm_source=api_errcode) 辅助定位和分析问题。

```json
[
  {
    "错误码": "-1",
    "错误描述": "system error",
    "解决方案": "系统繁忙，此时请开发者稍候再试"
  },
  {
    "错误码": "40001",
    "错误描述": "invalid credential  access_token isinvalid or not latest",
    "解决方案": "获取 access_token 时 AppSecret 错误，或者 access_token 无效。请开发者认真比对 AppSecret 的正确性，或查看是否正在为恰当的公众号调用接口"
  }
]
```

## 7. 适用范围

本接口在不同账号类型下的可调用情况：

```json
[
  {
    "小程序": "✔",
    "小游戏": "✔"
  }
]
```

- ✔：该账号可调用此接口。
- 其他未明确声明的账号类型，如无特殊说明，均不可调用此接口。
