Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I profile the EDT in Swing?

I have an application that I'm building in Swing. It has a scrollable and zoomable chart component which I can pan and zoom in. The whole thing is smooth except that sometimes the UI will pause for about 750 ms and I don't know why. This doesn't always happen - but sometimes something happens in the application and it starts pausing like this once every 6-8 seconds.

It seems pretty clear that there's some event being placed on the EDT that's taking 750 ms or so to run, which shouldn't be happening.

How do I profile the EDT specifically like this? What I would really like to do is get something that will output to a log or System.out every time an event runs on the EDT with the total amount of time the event took. Is there a way to do this?

Or is there some tool that will do this for me and give me a log of everything that runs on the EDT and how long it takes?

I'd like to go through this log, look at everything that's taking a long time, and find the problem.

like image 291
Erick Robertson Avatar asked Jan 21 '23 02:01

Erick Robertson


1 Answers

Take a look at this question. It describes a simple log on the EDT.

Create a class like this:

public class TimedEventQueue extends EventQueue {
    @Override
    protected void dispatchEvent(AWTEvent event) {
        long startNano = System.nanoTime();
        super.dispatchEvent(event);
        long endNano = System.nanoTime();

        if (endNano - startNano > 50000000)
            System.out.println(((endNano - startNano) / 1000000)+"ms: "+event);
    }
}

Then replace the default EventQueue with the custom class:

Toolkit.getDefaultToolkit().getSystemEventQueue().push(new TimedEventQueue());
like image 141
rancidfishbreath Avatar answered Jan 29 '23 21:01

rancidfishbreath