Skip to content
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

Allow @WarmUp annotation to prevent bleeding of undesired events #140

Closed
PKJosh opened this issue Apr 1, 2022 · 3 comments
Closed

Allow @WarmUp annotation to prevent bleeding of undesired events #140

PKJosh opened this issue Apr 1, 2022 · 3 comments

Comments

@PKJosh
Copy link

PKJosh commented Apr 1, 2022

For my testing purposes, I wish to limit certain IO events and prevent regression against these metrics. Consider the following example:

@Test
@EnableEvent("jdk.FileRead", threshold = 0)
fun `file is buffered no more than 10 times`() {
    operateOnFile(file)
    jfrEvents.awaitEvents()

    val readCount = jfrEvents.filter(JfrEventTypes.FILE_READ).count()
   assertThat(readCount).isAtMost(10)
}

Every FILE_READ event will be counted, including IO triggered by class loading. This is probably desired from jfrunit’s perspective but can make writing tests a bit cumbersome.

Filtering by jfrEvents by class is possible but the test should probably remain agnostic as to where the IO originates, especially when reads are delegated to an external dependency.

This forces a workaround like the following:

@BeforeAll
fun setup() {
    
    operateOnFile(dummyFile)

}

@Test
@EnableEvent("jdk.FileRead", threshold = 0)
fun `file is buffered no more than 10 times`() {
    operateOnFile(file)
    jfrEvents.awaitEvents()
}



@AfterAll
fun doActualAssertions() {
    val readCount = jfrEvents.filter(JfrEventTypes.FILE_READ).count()
   assertThat(readCount).isAtMost(10)

}


The alternative is handling the boilerplate warmup and jfrEvents.reset() calls in the test itself, a process that would be better handled by an annotation (especially once #126 comes to fruition).

@gunnarmorling
Copy link
Member

Hey, that's an interesting proposal. Instead of adding a new annotation like @WarmUp, how about we add support for @EnableEvent() to the existing @Before/@BeforeAll annotations? A PR for implementing this (as for #126) would be more than welcome.

@phejl
Copy link
Contributor

phejl commented Apr 14, 2022

Perhaps I'm missing something but can't you just filter events by file path? Something like (writing without IDE so not 100% sure)

val readCount = jfrEvents.filter(JfrEventTypes.FILE_READ.withPath(file.getAbsolutePath())).count()

BTW I think you don't need jfrEvents.awaitEvents() as when you invoke filter or contains the method will await for events implicitly.

@PKJosh
Copy link
Author

PKJosh commented May 10, 2022

After this discussion, I definitely did not seek out enough alternative solutions within the API. The withPath filter combined with jfrEvents.reset() definitely gives me everything I need.

Thanks for the help.

@PKJosh PKJosh closed this as completed May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants