Erlang/OTP - 计时应用

发布于 2024-10-05 03:43:41 字数 363 浏览 0 评论 0原文

我有兴趣对程序的不同部分进行速度基准测试。我尝试使用 info(statistics)erlang:now()

我需要知道精确到微秒的平均速度是多少。我不知道为什么我编写的脚本出现问题。

它应该能够在任何地方开始并在任何地方结束。当我尝试在可能并行运行多达四次的进程上启动它时,我遇到了问题。

有没有人已经解决了这个问题?

编辑:

如果有人可以提供脚本来执行此操作,愿意给予赏金。它需要通过多个进程生成'。我不能接受像计时器这样的功能..至少在我见过的实现中。 IT 仅遍历一个流程,即使如此,为了对完整程序进行全面测试,仍需要进行一些重大编辑。希望我说得足够清楚。

I am interested in bench-marking different parts of my program for speed. I having tried using info(statistics) and erlang:now()

I need to know down to the microsecond what the average speed is. I don't know why I am having trouble with a script I wrote.

It should be able to start anywhere and end anywhere. I ran into a problem when I tried starting it on a process that may be running up to four times in parallel.

Is there anyone who already has a solution to this issue?

EDIT:

Willing to give a bounty if someone can provide a script to do it. It needs to spawn though multiple process'. I cannot accept a function like timer.. at least in the implementations I have seen. IT only traverses one process and even then some major editing is necessary for a full test of a full program. Hope I made it clear enough.

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

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

发布评论

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

评论(4

深白境迁sunset 2024-10-12 03:43:41

以下是如何使用 eprof,这可能是您最简单的解决方案:

首先您需要启动它,就像大多数应用程序一样:

23> eprof:start().
{ok,<0.95.0>}

Eprof 支持两种分析模式。您可以调用它并要求分析某个函数,但我们不能使用它,因为其他进程会弄乱一切。我们需要手动启动它的分析并告诉它何时停止(顺便说一句,这就是为什么你不会有一个简单的脚本)。

24> eprof:start_profiling([self()]).
profiling

这告诉 eprof 分析将从 shell 运行和生成的所有内容。新流程将包含在这里。我将运行一些任意的多处理函数,该函数会产生大约 4 个进程,彼此通信几秒钟:

25> trade_calls:main_ab().
Spawned Carl: <0.99.0>
Spawned Jim: <0.101.0>
<0.100.0>
Jim: asking user <0.99.0> for a trade
Carl: <0.101.0> asked for a trade negotiation
Carl: accepting negotiation
Jim: starting negotiation
... <snip> ...

我们现在可以告诉 eprof 在函数运行完毕后停止分析。

26> eprof:stop_profiling().
profiling_stopped

我们想要日志。默认情况下,Eprof 会将它们打印到屏幕上。您还可以要求它使用 eprof:log(File) 记录到文件。然后你可以告诉它分析结果。我们告诉它使用选项total将所有进程的运行时间折叠到一个表中(请参阅manual 了解更多选项):

27> eprof:analyze(total).           
FUNCTION                                  CALLS      %  TIME  [uS / CALLS]
--------                                  -----    ---  ----  [----------]
io:o_request/3                               46   0.00     0  [      0.00]
io:columns/0                                  2   0.00     0  [      0.00]
io:columns/1                                  2   0.00     0  [      0.00]
io:format/1                                   4   0.00     0  [      0.00]
io:format/2                                  46   0.00     0  [      0.00]
io:request/2                                 48   0.00     0  [      0.00]
...
erlang:atom_to_list/1                         5   0.00     0  [      0.00]
io:format/3                                  46  16.67  1000  [     21.74]
erl_eval:bindings/1                           4  16.67  1000  [    250.00]
dict:store_bkt_val/3                        400  16.67  1000  [      2.50]
dict:store/3                                114  50.00  3000  [     26.32]

并且可以看到大部分时间(50%)都花在了 dict:store/3 上。 16.67% 用于输出结果,另外 16.67% 由 erl_eval 占用(这就是为什么在 shell 中运行短函数——解析它们比运行它们要长)。

然后您可以从那里开始。这是使用 Erlang 分析运行时间的基础知识。请小心处理,eprof 可能会给生产系统或运行时间过长的函数带来相当大的负载。特别是在生产系统上。

Here's how to use eprof, likely the easiest solution for you:

First you need to start it, like most applications out there:

23> eprof:start().
{ok,<0.95.0>}

Eprof supports two profiling mode. You can call it and ask to profile a certain function, but we can't use that because other processes will mess everything up. We need to manually start it profiling and tell it when to stop (this is why you won't have an easy script, by the way).

24> eprof:start_profiling([self()]).
profiling

This tells eprof to profile everything that will be run and spawned from the shell. New processes will be included here. I will run some arbitrary multiprocessing function I have, which spawns about 4 processes communicating with each other for a few seconds:

25> trade_calls:main_ab().
Spawned Carl: <0.99.0>
Spawned Jim: <0.101.0>
<0.100.0>
Jim: asking user <0.99.0> for a trade
Carl: <0.101.0> asked for a trade negotiation
Carl: accepting negotiation
Jim: starting negotiation
... <snip> ...

We can now tell eprof to stop profiling once the function is done running.

26> eprof:stop_profiling().
profiling_stopped

And we want the logs. Eprof will print them to screen by default. You can ask it to also log to a file with eprof:log(File). Then you can tell it to analyze the results. We tell it to collapse the run time from all processes into a single table with the option total (see the manual for more options):

27> eprof:analyze(total).           
FUNCTION                                  CALLS      %  TIME  [uS / CALLS]
--------                                  -----    ---  ----  [----------]
io:o_request/3                               46   0.00     0  [      0.00]
io:columns/0                                  2   0.00     0  [      0.00]
io:columns/1                                  2   0.00     0  [      0.00]
io:format/1                                   4   0.00     0  [      0.00]
io:format/2                                  46   0.00     0  [      0.00]
io:request/2                                 48   0.00     0  [      0.00]
...
erlang:atom_to_list/1                         5   0.00     0  [      0.00]
io:format/3                                  46  16.67  1000  [     21.74]
erl_eval:bindings/1                           4  16.67  1000  [    250.00]
dict:store_bkt_val/3                        400  16.67  1000  [      2.50]
dict:store/3                                114  50.00  3000  [     26.32]

And you can see that most of the time (50%) is spent in dict:store/3. 16.67% is taken in outputting the result, another 16.67% is taken by erl_eval (this is why you get by running short functions in the shell -- parsing them becomes longer than running them).

You can then start going from there. That's the basics of profiling run times with Erlang. Handle with care, eprof can be quite a load on a production system or for functions that run for too long. Especially on a production system.

在风中等你 2024-10-12 03:43:41

您可以使用eprof 或 fprof

You can use eprof or fprof.

二智少女 2024-10-12 03:43:41

执行此操作的正常方法是使用计时器:tc。 这里是一个很好的解释。

The normal way to do this is with timer:tc. Here is a good explanation.

浅忆 2024-10-12 03:43:41

我可以向您推荐这个工具: https://github.com/virtan/eep

您会得到类似的东西这个 https://raw.github.com/virtan/eep/master/结果是 doc/sshot1.png

对正在运行的系统上的所有进程进行分析的分步说明:

在目标系统上:

1> eep:start_file_tracing("file_name"), timer:sleep(20000), eep:stop_tracing().
$ scp -C $PWD/file_name.trace desktop:

在桌面上:

1> eep:convert_tracing("file_name").
$ kcachegrind callgrind.out.file_name

I can recommend you this tool: https://github.com/virtan/eep

You will get something like this https://raw.github.com/virtan/eep/master/doc/sshot1.png as a result.

Step by step instruction for profiling all processes on running system:

On target system:

1> eep:start_file_tracing("file_name"), timer:sleep(20000), eep:stop_tracing().
$ scp -C $PWD/file_name.trace desktop:

On desktop:

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