用于统计 iPhone 上方法调用次数的工具

发布于 2024-10-05 00:54:35 字数 57 浏览 2 评论 0原文

Time Profiler 可以测量某些方法所花费的时间量。是否有类似的方法来测量方法被调用的次数?

The Time Profiler can measure the amount of time spent on certain methods. Is there a similar method that measures the number of times a method is called?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对岸观火 2024-10-12 00:54:35

DTrace 可以做到这一点,但仅限于 iPhone 模拟器(Snow Leopard 支持它,但 iOS 尚不支持)。我在 MacResearch 上有两篇关于这项技术的文章 这里此处,我将介绍一些使用 DTrace 查找特定方法及其调用时间的案例研究。

例如,我创建了以下 DTrace 脚本来测量对带有 CP 前缀的类调用方法的次数,以及计算在这些方法中花费的时间的总和:

#pragma D option quiet
#pragma D option aggsortrev

dtrace:::BEGIN
{
    printf("Sampling Core Plot methods ... Hit Ctrl-C to end.\n");
    starttime = timestamp;      
}

objc$target:CP*::entry
{
    starttimeformethod[probemod,probefunc] = timestamp;
    methodhasenteredatleastonce[probemod,probefunc] = 1;
}

objc$target:CP*::return
/methodhasenteredatleastonce[probemod,probefunc] == 1/
{
    this->executiontime = (timestamp - starttimeformethod[probemod,probefunc]) / 1000;
    @overallexecutions[probemod,probefunc] = count();
    @overallexecutiontime[probemod,probefunc] = sum(this->executiontime);
    @averageexecutiontime[probemod,probefunc] = avg(this->executiontime);
}

dtrace:::END 
{
    milliseconds = (timestamp - starttime) / 1000000;
    normalize(@overallexecutiontime, 1000);
    printf("Ran for %u ms\n", milliseconds);
    printf("%30s %30s %20s %20s %20s\n", "Class", "Method", "Total CPU time (ms)",  "Executions", "Average CPU time (us)");
    printa("%30s %30s %20@u %20@u %20@u\n", @overallexecutiontime, @overallexecutions, @averageexecutiontime);
}

生成以下格式良好的输出:

        Class                         Method  Total CPU time (ms)           Executions Average CPU time (us)
      CPLayer                -drawInContext:                 6995                  352                19874
       CPPlot                -drawInContext:                 5312                   88                60374
CPScatterPlot      -renderAsVectorInContext:                 4332                   44                98455
CPXYPlotSpace        -viewPointForPlotPoint:                 3208                 4576                  701
       CPAxis               -layoutSublayers                 2050                   44                46595
CPXYPlotSpace -viewCoordinateForViewLength:linearPlotRange:plotCoordinateValue:                 1870                 9152
...

这会 从命令行运行 DTrace 脚本,您最好的选择可能是在 Instruments 中创建自定义仪器并在该仪器中填写适当的 D 代码。然后,您可以在模拟器中针对您的应用程序轻松运行它。

同样,这在设备上不起作用,但如果您只想统计某项被调用的次数,而不是它运行的持续时间,这可能可以完成工作。

DTrace can do this, but only in the iPhone Simulator (it's supported by Snow Leopard, but not yet by iOS). I have two writeups about this technology on MacResearch here and here where I walk through some case studies of using DTrace to look for specific methods and when they are called.

For example, I created the following DTrace script to measure the number of times methods were called on classes with the CP prefix, as well as total up the time spent in those methods:

#pragma D option quiet
#pragma D option aggsortrev

dtrace:::BEGIN
{
    printf("Sampling Core Plot methods ... Hit Ctrl-C to end.\n");
    starttime = timestamp;      
}

objc$target:CP*::entry
{
    starttimeformethod[probemod,probefunc] = timestamp;
    methodhasenteredatleastonce[probemod,probefunc] = 1;
}

objc$target:CP*::return
/methodhasenteredatleastonce[probemod,probefunc] == 1/
{
    this->executiontime = (timestamp - starttimeformethod[probemod,probefunc]) / 1000;
    @overallexecutions[probemod,probefunc] = count();
    @overallexecutiontime[probemod,probefunc] = sum(this->executiontime);
    @averageexecutiontime[probemod,probefunc] = avg(this->executiontime);
}

dtrace:::END 
{
    milliseconds = (timestamp - starttime) / 1000000;
    normalize(@overallexecutiontime, 1000);
    printf("Ran for %u ms\n", milliseconds);
    printf("%30s %30s %20s %20s %20s\n", "Class", "Method", "Total CPU time (ms)",  "Executions", "Average CPU time (us)");
    printa("%30s %30s %20@u %20@u %20@u\n", @overallexecutiontime, @overallexecutions, @averageexecutiontime);
}

This generates the following nicely formatted output:

        Class                         Method  Total CPU time (ms)           Executions Average CPU time (us)
      CPLayer                -drawInContext:                 6995                  352                19874
       CPPlot                -drawInContext:                 5312                   88                60374
CPScatterPlot      -renderAsVectorInContext:                 4332                   44                98455
CPXYPlotSpace        -viewPointForPlotPoint:                 3208                 4576                  701
       CPAxis               -layoutSublayers                 2050                   44                46595
CPXYPlotSpace -viewCoordinateForViewLength:linearPlotRange:plotCoordinateValue:                 1870                 9152
...

While you can create and run DTrace scripts from the command line, probably your best bet would be to create a custom instrument in Instruments and fill in the appropriate D code within that instrument. You can then easily run that against your application in the Simulator.

Again, this won't work on the device, but if you just want statistics on the number of times something is called, and not the duration it runs for, this might do the job.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文