Consider adding a system-sleep disabler
Petr Janeček
JanecekPetr at seznam.cz
Sun Jan 30 22:23:53 UTC 2022
Hello!
When benchmarking serious (work) code I always try to run it on the hardware
it will eventually on, usually server-class machines.
However, when I benchmark code for fun (personal side-projects, code to
prove somebody on the internet wrong...), I often run it
on my MacBook Pro. Please don't judge me, my company supplied it, I had no
choice :).
When doing so, the following has proven very useful as a base class for my
JMH-at-home benchmarks. It tries to disable screen
sleep, and, in effect, system sleep: https://gist.github.com/JanecekPetr/3c
9606dd6406c9c10672bd4b03eafa67
Would you consider adding similar functionality into JMH itself? It's
annoying when I kick off a 30-minute benchmark only to come
back in an hour and find the benchmark was interrupted in half and the
system went to sleep.
Oh, and sometimes I need to subclass something else. Is there a way to use
composition instead?
Thanks,
PJ
FAQ:
Q: WTH, why don't you disable system sleep in your system preferences?
A: Yeah I could, but I forget. And I do not want it to be completely
disabled because I sometimes start benchmarks and go to sleep. Unneeded
energy consumption and stuff.. And yes, I could set up a timer to shut the
computer down after the benchmarks are done, but as I said, I forget.
Q: Should such a functionality even be in JMH?!
A: Not sure, you tell me! I think of JMH as a runtime for long-running
tasks, and as such this functionality might be warranted, maybe?
Q: Argh, okay, maybe. Any surprises there?
A: Yeah, one *tiny* thing. On Mac (I only tested Windows and Mac) the
program above triggers a Permission required notification for Terminal.
Yikes. Interestingly enough, it still works if I deny the permission. Huh.
But still, your Terminal probably should not ask for Accessibility
permissions when running a JMH benchmark, so this should not be on by
default. And if it is hidden behind a CLI option, I'll forget to use it...
Q: Running an extra timing thread with a background task sucks for very
sensitive benchmarks.
A: Very sensitive benchmarks should not be running on systems with screen
sleep. But yeah, I agree, maybe it'd be better to just wiggle the mouse
between iterations if the iteration is expected to be shorter than a minute,
which it should almost always. I also tried using `Desktop.getDesktop().
addAppEventListener(new ScreenSleepListener() { ... });`, and the same with
`SystemSleepListener` but at least on Mac using Java 17.0.1 this does not
prevent the upcoming sleep. Weird.
More information about the jmh-dev
mailing list