如何在图书馆中使用 NYTProf
我想知道 Devel::NYTProf 是否可以用作另一个库中的库。我想做类似以下的事情
around 'somesub' => sub {
my $orig = shift;
my $self = shift;
start-timing;
$self->$orig(@_);
end-timing;
print '$time';
}
,但从它的文档中我无法确定它是否可以这样使用。是否可以?有人可以告诉我我会执行哪些 API 调用吗?
I'm wondering if Devel::NYTProf can be used as a library in another library. I'd like to do something like the following
around 'somesub' => sub {
my $orig = shift;
my $self = shift;
start-timing;
$self->$orig(@_);
end-timing;
print '$time';
}
but from it's documentation I'm unable to determine if it can be used like this. Is it possible? could someone tell me the API calls that i'd do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单、最可靠的做法是:
DB::enable_profile
和DB::disable_profile
调用(您可能需要首先检查是否定义了 subs) ,以避免在未加载 NYTProf 时损坏)。所有这些在 Devel::NYTProf 文档 中都有非常清楚的解释。
您可以尝试让您的库有条件地加载 NYTProf,但这里的问题是只有加载 NYTProf 后编译的内容才能获取任何跟踪点。这听起来可能完全没问题,因为您只想分析您的库,但不清楚如果您的库调用(或回调)任何其他代码会发生什么,而且我没有测试它。制作简单版本可能会容易得多:)
The simplest, most reliable thing to do is:
DB::enable_profile
andDB::disable_profile
calls in your library (you might want to check whether the subs are defined first, to avoid breakage when NYTProf isn't loaded).NYTPROF=start=no
in the environment.All of this is pretty clearly explained in the Devel::NYTProf docs.
You could try having your library conditionally load NYTProf, but the deal here is that only stuff compiled after NYTProf is loaded gets any tracepoints. That might sound perfectly okay, since you only want to profile your library, but it's not clear what will happen if your library calls out (or calls back) to any other code, and I didn't test it. It's probably a lot easier to make the simple version make do :)
我认为不能这样使用。但是您可以看看 Aspect::Library::Profiler或 Aspect::Library::Timer
I don't think it can be used that way. But you might take a look at Aspect::Library::Profiler or Aspect::Library::Timer