Integrating a Web-Based Profiler Into IntelliJ IDEA
Read full transcript 32 segments
-
I'm Johannes Bechberger. I work at SAP. I'm working on one of the best OpenJDK distributions SapMachine. We're one of many cool source projects here, but without further ado. So how did I end up here? I asked myself this quite a lot of times, but how it did end up here was that a couple of years ago, I did something related to debugging. I want to make debugging faster. In the end, it didn't work out, but during this I had a problem. I had things like these, they made sense in the project context and I had these tests and I want to see why they are so slow. So I thought, let's do some profiling. I only had IntelliJ's Community Edition there at my hand. So it was like, how could I improve this because what I wanted was just to right click and say like, hey, profile this with JFR or profile this with async-profiler.
-
So there are two modern profilers in the Java world. One is JFR, which is like built in. It's a JDK flight recorder. And the other is async-profiler, which attaches to the JVM. And both are pretty nice, but I want to use them directly. And so that's what I wanted to have, but didn't have them. So a question here and I hope this works. There should be a poll popping up. Who have you used to Profiler before? So to whom am I preaching about profilers now? Because when I asked this question at conferences, most people haven't heard of Profilers, sadly. I will ask the question if no one does, right? And I will report back in one second. Okay. No worries. You have given this talk at a couple of conferences.
-
Or not this talk, but talks on Profilers. Essentially when you use Profilers, when your program is slow and you want to know who's the culprit, it's a bit like detective work. This is where they're really beneficial. While I'm waiting for the answers, I might just continue. So the following, the show you is essentially a talk that they gave before, that they gave at the Firefox profile and that's the end of what to come at the Firefox dev room at FOSDEM, I think it was 2023 or 2024. Went to and gave a talk to them about how to integrate a web based profiler into IntelliJ, which was quite interesting, essentially telling them what I did there. So my question on time was, one was trying to profile myself.
-
So what am I working on? What are the actual options in the open source world? And I found two, one of them is a bigger Eclipse based application, the other one is a flame graph. Flame graphs are cool because to quote Mario Fusco, "I love flame graphs when you do something stupid, it punches you in your face. It's impossible not to see it." So the idea of the flame graph is essentially you see how you see essentially stack traces and you combine them. And so in a way that the lowest bar has full width and it says, okay, that's my main method run all the time. And then the main method calls method A this brands like 50% of the times of the bias, 50% bind and you do this whole stack. You can essentially see which parts of the program took how much time. These are pretty simple views. Many tools generate them, I think profile generates them by default. The cool thing is they are pretty easy to use, but the problem is they don't have much features like you can't dig much deeper into.
-
So there's also another open source tool and it's called JDK Mission Control, which is essentially the polar opposite. This is Eclipse based tool that's out there for quite a long time. And it allows you to really dig deep into the data. The only problem is it's not as really easy to use. So when I talk with people about this tool, they're more like, "It's the best we have." And that's a problem. And that's where I come in because I was like, I wanna have a tool in the middle. Like I wanna have the tool that's hopefully well integrated into the IDE that I'm using and a tool that gives me more than just a flame graph but it isn't as complicated to use as the other tools. So yeah, I had to write my own. I asked my boss about this and we saw that also other people internally found it interesting. Some of us was like, "Yeah, sure."
-
You can work on this, especially because it was August and vacation season. So there wasn't that much going on. So I looked around. I initially thought like, "Hey, I could write this myself." But then I realized and remembered that I'm not the biggest UI developer at the time I didn't have much knowledge into UI technologies. So I thought about different visualization options. And there was one, for example, this. There was speedscope, which is essentially a slightly better flame graph. It's cool. I looked into a flame for your plugin. I think this was actually an IntelliJ plugin and it was interesting. And of course there was also intelligence on profile but breaking the open source base. So I couldn't use it because it's only available. I think still only available with the ultimate versions.
-
And then I found Firefox Profiler. Firefox Profiler is the profiler used in Firefox. So if you ever wanna profile something in the web, you might've used Firefox Profiler for this. And the cool thing is it's a mature profiler. That's cool. It has also a dedicated team of like two to three people working there. It has many of the features that I want. And so I was like, "Hmm, couldn't I just like use this Firefox Profiler and put it into an IDE extended with some of the features they want to have. And then I have a fully functioning profiler without going through the hassle of developing my own." Of course, I thought it was simple. And then in the end, I like contributed for half a year to the Firefox Profiler projects to get in all the features they needed for this into Firefox Profiler, which was quite fun. That's the way I learned React just by making pull requests and getting them rejected and learning in this way.
-
So what are the features of this tool? This is how it looks like when you use it as it was intended to be used for developing that applications. And it can even show you screenshots in the timeline can show you the code below. It's also quite handy and can do much more. So it has many, many advantages as I already tells you. Now the question is how do I actually integrate it into IntelliJ? And that's also this talk is about later with small example application. So what I do, I take the JFR file and then I convert it into the specific Firefox profile using a Kotlin script. And then I package this with a server because I need to serve this, this web-based UI. I need to somehow make it available.
-
So you combine this prebuilt Firefox Profiler UI with the converter. And then what I can do, I can put this into an IntelliJ plugin. So my IntelliJ plugin is essentially wrap around the server and the service of wrap around the best UI and the converter. It's a bit complicated. It feels like shells. It feels like an onion with so many shells, but it works. The cool thing is I can test every part alone without testing the other stuff. So how does the file format look like? Just for if you have some profiler and you wanna export it to Firefox Profiler, it's pretty simple. You have some metadata, then for every thread, you get the samples because essentially what more than profiles do, they ask the JVM in our case every 10 milliseconds, hey, what are you currently doing? What are you currently doing? And then we record this.
-
So we get the stack traces, the frames, and the frames of course linked to functions. And this is a quite an optimized format which makes it quite compact. The only problem is it makes it also quite hard to emit. It works, but spent like two or three weeks on it, but it worked and helped to also work on the documentation. That's the cool thing with open source. You can just change things that aren't working. And that's where my Java JVlogging comes in. This is the whole plugin that I built this way and it essentially just wraps server that serves you the Firefox profiler with the converter and offers you convenience functions around it and also integrates into your JVM. You can download it. There should be hopefully a link in the chat or you can just type in Java JFR Profiler.
-
And I'm glad that people like it. And it has like 37,000 downloads, which is pretty nice. And I'm developing on it. So I fixed bugs, I had new bugs and new features all the time. For example, last week, I just released another version I just never managed to increment the buffer patch version. But yeah, it's still usable and I use it regularly. And what you can do with this is consider here with a simple main method that runs some Fibonacci computation. You can just tell it like, hey, profile this for me. And then it opens the window. And what you can also do is you can see more stuff. For example, you can get an overview. Hey, how many times was this method interpreted? How many times was it compiled? And the cool thing is it integrates with your IDE. So what it can do it's and you see it, hopefully you can see it here.
-
This plugin also just calls here the server and tells it like, hey, could you please give me the source code for this method? And then post-process it and gives you here the amount of hits, the amount of times this line was hit during the execution and shows it directly here because it's totally integrated. So you can also double-click on a method and it jumps you back into the IDE because even though the Firefox Profiler was never meant to be integrated into in general, it's not hard to make it this way, especially when you extend it a bit, but essentially what the Firefox profiler does, it's I pass it in the beginning some arguments where it held like, hey, please, every time the user double-clicks to this action, pass it some parameters and this then goes to the server and the server decides what to do. And the server runs in the context of the plugin.
-
We can do flame graphs and everything. And it's like, it was one of, I think it was the first profiler plugin for IntelliJ that was open source, which I'm quite happy with. Yeah, you can essentially dig deeper into anything. And nowadays it even has Trevry support. Trevry is another kind of visualization, just build it in. And now to the implementation. Of course, this is like the basic way I did it, but for the following, I'll work with something different. I'll show you how I use a simpler plugin because the problem is the Firefox profile are bringing true over time. I even added things like MCP support that you can use with other JetBrains AI plugins.
-
And what this does is quite simple. I can show it to you in a demo if everything works. And you hopefully have a link later to view this and use this plugin as the basis for your own endeavors. Because essentially I fix a lot of bugs for you. So what it does, you can just type in a class, just type in a pattern, and then it shows you all classes that match either by package or by name, and it shows you here the source code. You can jump back and forth. Like you can jump into the event bus or into the event, or you can also jump to methods. And the cool thing here is this here is a web browser that's embedded. This here is an IntelliJ IDE, a normal IDE, and you can't really see the difference. Let's go back to the slides.
-
So how it works is as I said, as I told you, this here is web, this is a Chromium, and what we then typically use for web applications here is just a normal web frameworks, and this goes, it was Vue, you can use React, you can use anything. The Chromium doesn't care. And the cool thing is when we look into this, and I hope it works, you can even open the developer tools here, and like do all the normal stuff. It's just JavaScript all the way down. Of course, with all the problems, but I'll get to this in a bit. So how it works, it's pretty simple. We have GET requests. So the IDE can, so our plugin, our web frontend can work, can interact with the IDE by using the built-in server, and can, for example, ask, hey, give me these classes, or do something like navigate to this location in IDE, but we wanna talk back, like when our IDE wants to tell the web frontend something, we use WebSockets.
-
And that's not hard. That's like what most of the web applications are built with, and that's pretty cool because we can use standard Java server frameworks. You could use Spring Boot. I'm using here Javalin just because it's simpler. And that's cool because many developers know already how to build basic web applications. Now with their embedded Chromium, they can build their own web applications as an IDE plugin. Of course, here are a word of caution to quote IntelliJ or JetBrains plugin documentation. It is recommended to implement UI in the default IntelliJ Platform UI framework, which is Swing. Consider using the JCEF approach, so the approach with the embedded Chromium, only in cases when the plugin needs to display HTML documents or the standard approach for creating UI is not enough. So I found this also during my testing. There are some bugs with Chromium.
-
Doesn't work as well. It's not as fluent, but it's just simpler to develop, at least for me. And you can find it here, the embedded Chromium, but it's nothing that specific for IntelliJ. It's also used by other projects. So why would you use a powder? Of course, there are many ways. UIs are normally web apps. Sadly, nobody, almost nobody is a swing nowadays for UIs, so it's just easier. You have all the CSS layouting and animations and everything that works quite well, but we have toolings and frameworks. We have a large ecosystem of frontend libraries with all the modern UI development tooling that we want. And so it's essentially far, far, far easier to develop UIs this way, at least in my opinion.
-
But I might be biased because I never did large-scale Swing development. Of course, there are some caveats the JetBrains plugin documentation telling you to be cautious. And besides having two separate tech stacks because we essentially now have to build a whole different application of that base application and ship it with it, which of course, incurs costs, for example, because we still need to launch an embedded Chromium, which might be bigger, and we have to now develop essentially a full stack application. What you also get, so is, I think, non-availability in Android Studio, so it's not available everywhere. The bugs, during my development of Firefox Profiler over the years, during my development of my plugin, I found enough of them. There are some problems with keyboard focus. There's some problems sometimes with scrolling. It doesn't work sometimes on Linux.
-
Well, it requires more testing. And you have an indirectional server, so it's not as smooth because we have to send everything through a HTTP connection. This is far slower than just calling a method. Also, a problem is that the Chromium version of the embedded Chromium lags usually behind half a year to a year, which might not be a problem, but because we're not running arbitrary JavaScript code in these embedded Chromium browsers because we control everything, but it's still something to keep in mind. So how does the plugin work in principle? So we have on the one side the IntelliJ IDE, like the Java part, essentially, or the Kotlin part, depending on what you use, and we have to prep from that. And as I told you, they communicate either the HTTP, HTTP calls or via WebSocket.
-
Since you want WebSockets already, as far as I understand, it's long running HTTP connections where both sides can just send a message. And usually this message is just a JSON. And our class browser here, our class processor in the plugin, connects our web frontend via these API calls with the Intel chain channels. For example, the PSIs and also PSR listener to also inform the back front end when some source code changed, or when the user clicked on, "Hey, please show me this class in the class browser." And you have a basic API. The cool thing with this API, you can test it and you can test both the frontend and the Java part independently. For example, for the back frontend, you can just mock these APIs. Makes it quite nice. And then in the end, you just have a browser window that launches this here.
-
And the other cool thing that I haven't shown you, I hope this works, is that of course what you can do here is you can also use Firefox Profiler or your back frontend directly in the internet. So what I did is that I have a version of my Firefox Profiler that's running in your IDE, that's just running on the internet and that you can use the same way. Of course you don't have all the goodies of IDE integration, like getting access to the source code, but it also works. This is nice because it means that you can easily integrate the UI into a different IDE, for example, into VS Code, or let users use it without using any IDE. So you're essentially reducing the amount of effort that you have to do. I think it's probably similar to what people do with Android and iOS applications where they write it using React Native.
-
And that's pretty cool. And how does a basic call here works, the user types into a filter box, then we do some debouncing because users can type fast. We don't wanna ask the intelligent live in too often. We don't wanna hog CPU. And then our app asks, hey, please, get users to get request. Like, hey, give me the classes that match the full model user, paste it in, then our class process server asks the IDE, like, hey, please do it for me. Give me the information and then we pass all the information back first into our world as a list and then later as a JSON array using all the common JSON serialization libraries that you know. And that makes it a bit harder to understand because like it's more steps. Usually when you have a Swing application, it's just one step. Like you would call something that just updates the UI, but here you have a couple, but yeah, there are pros and cons.
-
Now let's look at the code for the last four minutes or so. One important part that you need to know is that you need to really have two tech stacks and integrating them is doable. I think here you have the build script for Gradle. You have to install Node, you have to learn about the whole tech stack and you have to then build it. So essentially what I'm doing, I have a script that make sure that Node is there and then to build the front end and then put the build front end essentially somewhere in the resources. And it works, but also it means that the build now takes far longer and that they have new build dependencies and it makes your life easier or harder. But now to the core part, this is the server, but you see here, it's not hard.
-
It gets the IDE project. We ask our report. And then of course we need to tell our, and then our background, it needs to know where the things are located, but we usually say, oh, it's just at the same port of localhost. And yeah, then we just have route. Simple route is like, hey, what's the theme? So that we can theme match between the two. You see if we just use travel in and tell like, hey, the theme returns is still a dark theme then not a lot. It's quite easy to develop. It's quite easy to debug. It's quite easy to make something wrong. That's always the problem when you're mixing tech stacks, but I still think it's worth it. Of course, then we have the window. There you see some of the problems that you can have with embedded Chromium. That there are some promises embedded chromium sound dropping the first load you all call.
-
So what we're doing here, we're telling the embedded browser in the beginning, hey, please load us the URL, but it then sometimes doesn't do it. So we need to sleep and let the check like, oh, did you load it? So half the code is essentially moving around the things that are some rough edges here, but besides the rough edges, I would say it's pretty cool and it's fun to use. Of course there are always problems, but hey, you can write your own plugin without needing to know Swing without depending on all the IDE APIs. It's very nice. So yeah, thanks for the IntelliJ and the JetBrains team for having me here. It's a pleasure to talk about the tool. I hope you check it out. It helps me a lot when I work.
-
I hope you like the plugin. You find myself mostly nowadays on Mastodon as part of my GitHub, but you also find all the tools that I'm developing because currently I'm working on a heap dump analyzer that will be out soon. And I blog sometimes on the internet and you find my team, the SapMachine team at sapmachine.io or on Twitter where we sometimes post, I'm just happy to be able to do this train work. So yeah, thanks for your attention here and I hope there are some questions. Yeah, thank you Johannes. Yeah, that was a very complex stack of technologies that brought forward a very useful and deep set of functionalities. I've got two questions about the pain points and I see that maybe one of the pain points was the conversion between two formats and the target format for Firefox Profiler.
-
Would LLM help in that aspect or AI agents to ease with the conversion between two Tensor and OPEC formats? It's an interesting question. I think it's important to note that when I built this IntelliJ plugin, it was before LLMs were big. So the plugin, most of the plugin is still like two years old. But of course, yes, it helps. Nowadays it would be much easier but the problem is LLMs still have problems with recursive data types, but still would help. And the core thing where I used LLMs for this is that of course I have a couple of pull requests that aren't merged in Firefox Profiler. So after two years of not working on the plugin, I needed to migrate these products. I need to rebase. And normally like with two people full time working on Firefox Profiler, this would be a gargantuan task.
-
I like pushed it for two years in front of me and I'm like, I'm not doing this. And then I asked Claude to do it. And yeah, it was, I think it was a day that it worked on like half a day and I needed to start it two times because it got stuck, but it rebased and it finished and now I'm working with a modern Firefox Profiler version. So yeah, it helps definitely with development. Good. And my second question about maybe even more, bigger pain point was the JCEF. Do you aim for the compatibility for multiple IDE versions? I'm asking because I have personally seen that JCEF library tends to do quite, they did breaking changes in the API and even we at JetBrains have encountered that. So do you aim for compatibility or like do you go in the brave mode?
-
The quick answer, no. I do two things to alleviate the pain points in this. I use the most simple API, just use the API of like, please launch it and log URL. I don't use any other API because I know that it changes and I didn't want to deal with this because guess, you can ask the Chromium like, hey, please execute this JavaScript or push data into one like no, Javalin is a pretty nice Java server implementation, I can use HTTP calls, there's no API changes. Regarding IDEs, I'm currently targeting like 25, IntelliJ IDEA and 26, I'm just dropping old releases when it's too cumbersome to work around stuff because in the end, I think most people should upgrade to newer versions just because they fixed IntelliJ, get some new features, some new bug fixes.
-
So I only focus on the new stuff. Please upgrade your stuff. Oh yes, the, yeah, I can answer Christian directly, yes, because of the API changes, it's a pain, I try to use it, it's a pain. Just don't, if you like yourself, if you like your sanity, just use an HTTP server, they're well understood, you can debug them and the other stuff. Oh, don't ask me, I spent too many days on trying to get it to work. Yeah. The thing is when you, the thing is to keep in mind when you're developing a plugin that you think, oh yeah, now we can use this fancy tech. The problem is this plugin will be around for a couple of years. Yeah, use the simplest technology, use the simplest technology, stack with the best understood API. That makes it so much easier. Have we addressed the questions on screen, Robert?
-
I think yes, that was the question about the custom URL handle of HTTP. And obviously we feel your emotions. Can I ask one more question? Sure. Yeah, so imagine this, I have a plugin and inside this plugin I'm using a JCEF browser window just for displaying markdown and it needs smooth scrolling. So I use the profiler but outside of the plugin with the Chrome tools to find the best way to make this really smooth. And now I use it in JCEF and it's not as good. How do I profile that? Yeah, is the answer down the good answer? So I found that I found similar issue. So for one, for example, what Firefox profile does and why works are smooth is that they have their own renderance for everything.
-
So Firefox Profiler is just hardcore optimized by the Firefox Profiler folks and kudos to them. My plugin only works that fast because they like optimize it. So optimize your web application and then just pray because yes, optimizing web applications yourself is much harder than Java applications. You have to build in a Chromium profiler there, but yeah. Okay. Okay, it's one of the disadvantages. Yes. Okay. Okay, thank you. Well, and let's thank Johannes again for the nice presentation. Lovely to have you here. I think we have time or we need to make time for one or two ruffle questions.
No summary available yet.
View original episode ↗