-
Notifications
You must be signed in to change notification settings - Fork 37
Add documentation for profiling #287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Lotes
wants to merge
2
commits into
main
Choose a base branch
from
lotes/profiling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| --- | ||
| title: "Profiling" | ||
| weight: 100 | ||
| --- | ||
|
|
||
| Profiling is the process of measuring the performance of your application to identify bottlenecks and optimize resource usage. In this section, we will explore Langium's profiling tools and techniques that can help you analyze the performance of your parsing process. | ||
|
|
||
| ## Example file | ||
|
|
||
| Let's assume that we will be using the `Hello World` example grammar for this recipe. Furthermore, let's say you have a file `example.hello-world` with the following content: | ||
|
|
||
| ```plain | ||
| person Langium | ||
| person Langium2 | ||
| person Langium3 | ||
| person Langium4 | ||
| person Langium5 | ||
| Hello LangiumWrong! | ||
| Hello Langium2! | ||
| Hello Langium3! | ||
| Hello Langium4! | ||
| Hello Langium5! | ||
| Hello Langium! | ||
| Hello Langium! | ||
| Hello Langium! | ||
| ``` | ||
|
|
||
| ## What do we measure? | ||
|
|
||
| With Langium's built-in profilers, you can measure: | ||
|
|
||
| * the **parsing** process broken down by each grammar rule. Here, will see how much time was spent in each rule and how many times each rule was invoked | ||
|
|
||
| ```plain | ||
| Task parsing.hello-world executed in 1.69ms and ended at 2025-11-04T16:38:53.225Z | ||
| ┌─────────┬────────────────────────┬───────┬────────┬───────────┐ | ||
| │ (index) │ Element │ Count │ Self % │ Time (ms) │ | ||
| ├─────────┼────────────────────────┼───────┼────────┼───────────┤ | ||
| │ 0 │ 'hello-world.Model' │ 1 │ 48.54 │ 0.82 │ | ||
| │ 1 │ 'hello-world.Person' │ 5 │ 27.92 │ 0.47 │ | ||
| │ 2 │ 'hello-world.Greeting' │ 8 │ 20.41 │ 0.34 │ | ||
| │ 3 │ 'hello-world' │ 1 │ 3.14 │ 0.05 │ | ||
| └─────────┴────────────────────────┴───────┴────────┴───────────┘ | ||
| ``` | ||
|
|
||
| * the **linking** process broken down by each cross-reference. Here, we will see how much time was spent resolving each reference and how many times each reference was resolved. | ||
|
|
||
| ```plain | ||
| Task linking.hello-world executed in 0.92ms and ended at 2025-11-04T16:38:53.228Z | ||
| ┌─────────┬───────────────────────────────┬───────┬────────┬───────────┐ | ||
| │ (index) │ Element │ Count │ Self % │ Time (ms) │ | ||
| ├─────────┼───────────────────────────────┼───────┼────────┼───────────┤ | ||
| │ 0 │ 'hello-world.Greeting:person' │ 8 │ 67.22 │ 0.62 │ | ||
| │ 1 │ 'hello-world' │ 1 │ 32.78 │ 0.3 │ | ||
| └─────────┴───────────────────────────────┴───────┴────────┴───────────┘ | ||
| ``` | ||
|
|
||
| * the **validation** process broken down by each grammar rule. TODO what exactly is measured here? | ||
|
|
||
| ```plain | ||
| Task validating.hello-world executed in 0.19ms and ended at 2025-11-04T16:38:53.229Z | ||
| ┌─────────┬────────────────────────┬───────┬────────┬───────────┐ | ||
| │ (index) │ Element │ Count │ Self % │ Time (ms) │ | ||
| ├─────────┼────────────────────────┼───────┼────────┼───────────┤ | ||
| │ 0 │ 'hello-world.Person' │ 5 │ 60.83 │ 0.12 │ | ||
| │ 1 │ 'hello-world' │ 1 │ 17.21 │ 0.03 │ | ||
| │ 2 │ 'hello-world.Model' │ 1 │ 13.87 │ 0.03 │ | ||
| │ 3 │ 'hello-world.Greeting' │ 8 │ 8.09 │ 0.02 │ | ||
| └─────────┴────────────────────────┴───────┴────────┴───────────┘ | ||
| ``` | ||
|
|
||
| ## Enabling profiling | ||
|
|
||
| By default, profiling is disabled in Langium to avoid unnecessary performance overhead. To enable profiling, you need to modify your Langium services configuration. Here's how you can do it inside of the `module.ts`: | ||
|
|
||
| ```ts | ||
| import { DeepPartial, DefaultLangiumProfiler, type Module, inject } from 'langium'; | ||
|
|
||
| //... | ||
|
|
||
| // override your shared services | ||
| export const HelloWorldSharedModule: Module<HelloWorldSharedServices, DeepPartial<HelloWorldSharedServices>> = { | ||
| profilers: { | ||
| LangiumProfiler: () => new DefaultLangiumProfiler() | ||
| //use () => new DefaultLangiumProfiler(new Set(['parsing', 'linking', 'validating'])) | ||
| //to enable only specific profilers | ||
| } | ||
| } | ||
|
|
||
| //... | ||
| export function createHelloWorldServices(context: DefaultSharedModuleContext): { | ||
| shared: LangiumSharedServices, | ||
| HelloWorld: HelloWorldServices | ||
| } { | ||
| const shared = inject( | ||
| createDefaultSharedModule(context), | ||
| HelloWorldGeneratedSharedModule, | ||
| HelloWorldSharedModule //add this line | ||
| ); | ||
| //... | ||
| } | ||
| ``` | ||
|
|
||
| ## Using the profiler | ||
|
|
||
| Once profiling is enabled, the `LangiumProfiler` service is triggerd during the parsing, linking, and validation processes. After processing a file, the profiler will output the profiling results to the console as shown above. | ||
|
|
||
| ```ts | ||
| import { EmptyFileSystem } from "langium"; | ||
| import { parseHelper } from "langium/test"; | ||
| import type { Model } from "hello-world-language"; | ||
| import { createHelloWorldServices } from "hello-world-language"; | ||
|
|
||
| const services = createHelloWorldServices(EmptyFileSystem); | ||
| const profiler = services.shared.profilers.LangiumProfiler!; | ||
| const parse = parseHelper<Model>(services.HelloWorld); | ||
|
|
||
| const result = await parse(` | ||
| person Langium | ||
| person Langium2 | ||
| person Langium3 | ||
| person Langium4 | ||
| person Langium5 | ||
| Hello LangiumWrong! | ||
| Hello Langium2! | ||
| Hello Langium3! | ||
| Hello Langium4! | ||
| Hello Langium5! | ||
| Hello Langium! | ||
| Hello Langium! | ||
| Hello Langium! | ||
| `, { validation: true }); //do not forget to enable validation! | ||
|
|
||
| //<-- look at your console! | ||
|
|
||
| // Access profiling data programmatically | ||
| profiler.getRecords("parsing").forEach(r => console.log(`Parsing record: ${r.identifier} took ${r.duration} ms`)); | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
During the validation, the profiler measures the time each type of AST node takes to go through the validation.
Countwill keep track of how many nodes of a specific type have been validated. In this example, 5Personitems and 8Greetingitems have gone through the validation, taking 60% and 8% of the whole validation time respectively.