scalene
scalene copied to clipboard
`force_print` argument for output behaves strange
https://github.com/plasma-umass/scalene/blob/6980cb0c41df6ae724a2ce8d4686398b4b058853/scalene/scalene_json.py#L109C12-L109C15
Shouldn't it be if force_print and not ...? The printing will happen if force_print=False.
I agree that this looks unintuitive but that line of code has been in the codebase for 2 years. Can you point to a bug here?
I think the issue is that there seems to be no way to output only the functions I care about (but maybe there's another way?), only the full file. The output_profile_line never returns a False, which in turn makes output print every line.
So I see that there is some code that is always executing now that output_profile_line returns a "null" entry instead of False (which led to some issues, addressed by https://github.com/plasma-umass/scalene/commit/cadb8f463e3fc11d57b20795743c40af5e5d0b69). Are you using @profile to select the functions you care about? You should definitely not be using Scalene's internal functions.
I see. No, I use scalene_profiler.Scalene.profile to wrap the function in runtime, since adding and removing @profile everywhere is a hassle:) I was under impression that using @profile and Scalene.profile should give the same results. It seems that there is some other mechanism for filtering lines in addition to profile_this_code.
It's the same mechanism, but it was never intended to be called directly. I like the idea of listing a bunch of functions to profile without selecting them one by one. Can you give me an example of how you are doing this? Might make sense to expose an API.
The usecase is that I have profile.py script that imports a library that I want to profile. It's simply calling Scalene.profile on whatever function I want to profile. If I run scalene profile.py it works, but requires the library files to be within working directory tree (can workaround with --profile-only).
for func in [mylib.func1, mylib.Class1.method1]:
scalene_profiler.Scalene.profile(func)
logger.log(10, f"{scalene_profiler.Scalene.__dict__['_Scalene__functions_to_profile']=}")
scalene_profiler.Scalene.start()
func_to_run(*args, **kwargs)
scalene_profiler.Scalene.stop()
Unrelated, but something that I found useful is to start scalene from within a python script with pre-configured parameters. This removes the need to know or remember to run the script as scalene [params] profile.py: you can run python profile.py and get the same result. Example