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

feat: log duration of adding new block #130

Merged
merged 2 commits into from
Apr 9, 2024

Conversation

dan-da
Copy link
Collaborator

@dan-da dan-da commented Apr 6, 2024

closes #128

Adds a macro log_duration that logs execution time of a standard function and also log_duration_async for async functions.

The latter macro is then used to log the duration of GlobalState::store_block_internal()

The macros will make it easy to add timings for anything we care about, just by wrapping any existing fn with the appropriate macro.

The log-level is Debug, which is presently hard-coded into the macros. Rationale: I don't think users need to see this info in normal operation and also I wanted the macros to be dead-simple to use for wrapping existing functions (so I didn't make a log-level param).

Example log output

2024-04-06T05:54:59.783708912Z DEBUG ThreadId(01) neptune_core::models::state: at src/models/state/mod.rs:1189
-- executed async expression --
store_block_internal_worker(self, new_block, coinbase_utxo_info)
 -- duration: 0.117111901 secs --

@Sword-Smith is this what you had in mind? I chose to log execution time of the entire GlobalState::store_block_internal() method rather than the two archival_state specific calls. Pls let me know if you want a different behavior.

closes Neptune-Crypto#128

Adds a macro `log_duration` that logs execution time of a standard
function and also `log_duration_async` for async functions.

The latter macro is then used to log the duration of
`GlobalState::store_block_internal()`
@dan-da dan-da requested a review from Sword-Smith April 6, 2024 06:04
Copy link
Member

@Sword-Smith Sword-Smith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Can we make the log on the info level instead of the debug level? Getting a new block only happens about every 10 minutes, so I think it won't make the info log output too big.

Excited to see what the results are and how they scale on our next testnet. I'd probably just have used a simpler machinery of adding a few log output and perhaps manually handled a timer, but this works too.

In my opinion you're measuring the right thing. It's worth noting that the relevant measure, which is also what this PR measures, is how long it takes to apply one new block (and not a batch of block). Batch downloading happens rarely.

Is this new functionaly something that can be tested?

Is the 0.1 second reported for an empty wallet btw.?

@dan-da
Copy link
Collaborator Author

dan-da commented Apr 8, 2024

Looks good. Can we make the log on the info level instead of the debug level? Getting a new block only happens about every 10 minutes, so I think it won't make the info log output too big.

Sure info level seems fine for this case, but probably not every case we want to track as we start to care more about perf/optimization. So I will either add a log-level param to the macro or maybe just make different macro versions. I've also considered adding a description param.

Anyway, I'll try and come up with something that's flexible while remaining simple to use.

Excited to see what the results are and how they scale on our next testnet. I'd probably just have used a simpler machinery of adding a few log output and perhaps manually handled a timer, but this works too.

I already had a fn for doing the timing, so this just adds macros that make it simpler to time + log. I'm envisioning we will end up doing this in a bunch of places, so I'd like to keep it simple at the usage sites. Also using a macro enables printing the file:line info and the exact expression, which a regular fn call wouldn't do (or at least I don't know how).

In my opinion you're measuring the right thing.

good, thx.

Is this new functionaly something that can be tested?

not something I would think to test really. There are basically two bits of functionality: timing a fn call and logging the results. How would I test timing a call, since I don't know the answer until I get the result, and it will vary from execution to execution and machine to machine? I guess one could test it is inside a certain sanity window, but... worth the effort? Likewise logging seems a bit hard/pointless to test. diminishing returns, etc.

Is the 0.1 second reported for an empty wallet btw.?

yes.

@Sword-Smith
Copy link
Member

Sword-Smith commented Apr 8, 2024

Sure info level seems fine for this case, but probably not every case we want to track as we start to care more about perf/optimization. So I will either add a log-level param to the macro or maybe just make different macro versions. I've also considered adding a description param.

Both options seem good to me.

not something I would think to test really

Makes sense. Don't bother then.

Changes:

 * Moves the duration macros into new module crate::macros
 * adds macros duration_info!, duration_debug!,
   duration_async_info!, and duration_async_debug!
 * duration! and duration_async! now use log-level TRACE by default.
 * adds an optional message string to each macro.
 * adds tests to ensure that each usage variant will compile.

In total, the caller can now:
 *  log duration to trace, debug, info log-levels
 *  include an optional descriptive message.
@dan-da
Copy link
Collaborator Author

dan-da commented Apr 9, 2024

Ok, it is logging at Info level now.

I also added a couple tests just to ensure that the macros compile cleanly when used, including each param variant, because it is very easy to have a macro that compiles if unused, but breaks once it is used.

Details:

I added more macros to logging durations more flexible. So now we have duration!(), duration_info!(), and duration_debug!(), each of which accept an optional message. There is also an async variant of each.

To keep things organized, I moved these into a new crate::macros module.

(I don't intend to get carried away with writing macros, but they were useful for this purpose, and fit well with other logging macros.)

@dan-da dan-da merged commit e61ef78 into Neptune-Crypto:master Apr 9, 2024
3 checks passed
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

Successfully merging this pull request may close these issues.

Measure and (log-info) output the time it takes to apply a new block to the archival state
2 participants