# 10.「利用 Metrics 和 Diagnostics 提高性能」网络研讨活动

## 前言

之前参与了Apple「利用 Metrics 和 Diagnostics 提高性能」网络研讨活动，之前只简单的总结了一下，现在整理一下。感觉有些东西还是挺实用的。

## 一：内存指标

* 平均挂起内存和峰值内存
* 内存管理会影响启动时间（冷启动热启动）
* 通过这些指标了解内存的使用，可以：
  * 发现难以重现的内存泄露问题
  * 减小挂起平均内存
    * 减少挂起内存占用有助于减少应用在后台被释放的几率

## 二：MetricKit

* 搜集电池和性能指标和诊断的框架
* 应用内接入后就可以通过API获取丰富的性能指标数据
* 如果有必要可以将数据上报到自己的平台上，也可以作为开发过程中西能调优的工具

## 三：Xcode Metrics Organizer

在`Xcode`中`Window`下拉菜单选择`Organizer`

![MetricsXcode](/files/-MX2dbSVboQ5qGNG_9Dr)

![OrganizerCrash](/files/-MX2dbSWJnJG7gCcm26S)

这里是可以选择版本的包含：AppStore（Release） 和 TestFlight（Beta）

* Reports
  * 这里的数据也是系统自动收集的，前提是用户需要允许给Apple共享数据
  * Crashes
    * 实测是可以看到具体崩溃信息的，无须上传符号表
      * 自己的小项目没有太多的崩溃可以看，后面找时间看下公司的项目的信息
  * Disk Writes
    * 没看到数据，可能是有异常才会上报吧
  * Energy
    * 没看到数据，可能是有异常才会上报吧
* Metrics
  * 无须应用内集成`MetricsKit`就可以看到相关数据
  * 前提是用户量达到一定的水平
    * 具体是多少并没有提到，反正我自己的小App是没有的**TAT**
  * 需要用户在设置系统的时候统一将数据共享给Apple，否则是没有该用户的数据的
  * 所有数据都是苹果系统内自动收集上报的

## 四：App Store Connect API

这是苹果提供的一套`REST API`接口，可以直接通过请求获取`Metrics`数据。

但是需要和上面的满足一样的前提：达到一定用户量

## 五：参考

> 附本次主题参考资料：（会后Apple邮件提供的）
>
> [mprove performance and battery life in your app](https://developer.apple.com/news/?id=q8jd4xub)
>
> [Improving Battery Life and Performance](https://developer.apple.com/videos/play/wwdc2019/417/)
>
> [Optimizing Storage in Your App](https://developer.apple.com/videos/play/wwdc2019/419/)
>
> [MetricKit](https://developer.apple.com/documentation/metrickit)
>
> [Improving Your App's Performance](https://developer.apple.com/documentation/xcode/improving_your_app_s_performance)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ryukiedev.gitbook.io/wiki/ios/you-hua/10.-li-yong-metrics-he-diagnostics-ti-gao-xing-neng-wang-luo-yan-tao-huo-dong.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
