测量“繁忙度”事件调度线程的
我想测量我的事件调度线程的“繁忙度”。一种可能的想法是设置一个后台线程来执行以下操作:
while(true) {
final long[] end = new long[1]; // Array to get stuff out from Runnable.
long start = System.nanoTime();
EventQueue.invokeAndWait(new Runnable() {
public void run() {
end[0] = System.nanoTime();
}
});
long queueTimeNs = end[0] - start;
// Report the queue time somewhere.
Thread.sleep(100); // Poll the EDT < 10 times/s.
}
这个想法是测量从将事件发送到 EDT 到它被分派所花费的时间。这可以粗略地了解 UI 的响应能力。
这有什么意义吗?有没有更标准的方法来做类似的事情?
I would like to measure the "busyness" of my Event Dispatching Thread. One possible idea is to set up a background thread that does something like:
while(true) {
final long[] end = new long[1]; // Array to get stuff out from Runnable.
long start = System.nanoTime();
EventQueue.invokeAndWait(new Runnable() {
public void run() {
end[0] = System.nanoTime();
}
});
long queueTimeNs = end[0] - start;
// Report the queue time somewhere.
Thread.sleep(100); // Poll the EDT < 10 times/s.
}
The idea is to measure how long it takes from sending an event to the EDT to it getting dispatched. This would give a rough idea about the responsiveness of the UI.
Does this make any sense? Is there some more standard way to do something similar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我为此使用了一个出色的工具:SwingExplorer。它允许您检查 Swing 组件、查看它们是如何绘制的、检测 EDT 违规以及检测 EDT 的挂起。基本上,您输入一个以毫秒为单位的持续时间值,然后使用您的应用程序。当 EDT 挂起时间超过此持续时间时,该挂起会记录在工具的 UI 中。
官方网站是 https://swingexplorer.dev.java.net,但它似乎在在我写下这个答案的那一刻。您可以找到 Eclipse 和 NetBeans 的插件,如果您使用 maven,您还可以在 Maven 存储库上找到 swingexplorer(抱歉,我暂时找不到链接)
至少存储库仍然可用:
cvs -d :pserver:guest:[电子邮件受保护]:/shared/ data/ccvs/repository co swingexplorer
编辑
我查看了 Swing explorer 的源代码,看来他们编写了一个自定义的
EventQueue
来检查 EDT 行为。该代码似乎与另一个项目 SwingHelper 相关。编辑 2
该项目的站点很快就会恢复: http://java.net/projects/swingexplorer
I use an excellent tool for this : SwingExplorer. It allows you to inspect your Swing components, to see how they are drawn, to detect the EDT violations, and to detect the hangs of the EDT. Basically, you enter a duration value in milliseconds, and then you play with your app. When the EDT hangs for more than this duration, the hang is logged in the tool's UI.
The official site is https://swingexplorer.dev.java.net, but it seems to be down at the moment I write this answer. You can find a plugin for Eclipse and NetBeans, and you can also find swingexplorer on a maven repository, if you use maven (sorry, I can't find a link for the moment)
At least the repository is still available :
cvs -d :pserver:guest:[email protected]:/shared/data/ccvs/repository co swingexplorer
Edit
I looked at the source code of Swing explorer, and it appears that they wrote a custom
EventQueue
to inspect the EDT behavior. The code seems to be related to another project, SwingHelper.Edit 2
The project's site will come back soon at http://java.net/projects/swingexplorer
我认为你的方法在衡量“响应性”方面接近理想,因为它考虑了 EDT 中完成的实际工作量(在设计糟糕的应用程序中可能太多)和机器完成这项工作的能力。
顺便说一句,我曾经尝试替换/重新路由 EDT,但几个小时后我发现即使不顾一切地使用反射来访问实现类的私有字段也是不可能的。最后,一切都取决于正在等待的本地对象,而该对象是不可能获得的。
我怀疑出于同样的原因,不可能拦截 EDT 来获取处理的事件数量等信息(这是我对可以使用的指标的第一个想法)。
I think your appraoch is close to ideal in measuring "responsiveness" since it takes into account both the actual amount of work done in the EDT (possibly way too much in a badly-designed app) and the machine's ability to do that work.
BTW, I once tried to replace/reroute the EDT, but after many hours I found that it was impossible even when recklessly using reflection to access private fields of implementation classes. In the end, everything hinged on a local object being waited on, which was impossible to get a hold of.
I suspect that for the same reason it's not possible to intercept the EDT to get information such as the number of events processed (which was my first idea for a metric you could use).