]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Dont output logs when benchmarking 2024-08-fix-bench-logging
authorMatt Corallo <git@bluematt.me>
Fri, 30 Aug 2024 17:38:37 +0000 (17:38 +0000)
committerMatt Corallo <git@bluematt.me>
Fri, 30 Aug 2024 18:42:04 +0000 (18:42 +0000)
In 11ab302087622b546d116fc9679f601667d18c4d we accidentally removed
the `not(ldk_bench)` bound before outputting logs to stderr, which
we restore here. Instead of simply ignoring logs in benchmarks,
which we did previously, we instead format logs (in a way that LLVM
will not optimize out).

lightning/src/util/test_utils.rs

index 7a0f75a57c439f5edd22138001a57d85e0a37076..7252162e269015a295546b7fd29537ac361b4f60 100644 (file)
@@ -1174,10 +1174,25 @@ impl TestLogger {
 
 impl Logger for TestLogger {
        fn log(&self, record: Record) {
-               *self.lines.lock().unwrap().entry((record.module_path, format!("{}", record.args))).or_insert(0) += 1;
-               *self.context.lock().unwrap().entry((record.module_path, record.peer_id, record.channel_id)).or_insert(0) += 1;
-               let pfx = format!("{} {} [{}:{}]", self.id, record.level.to_string(), record.module_path, record.line);
-               println!("{:<55}{}", pfx, record.args);
+               let s = format!("{:<55} {}",
+                       format_args!("{} {} [{}:{}]", self.id, record.level.to_string(), record.module_path, record.line),
+                       record.args
+               );
+               #[cfg(ldk_bench)] {
+                       // When benchmarking, we don't actually want to print logs, but we do want to format
+                       // them. To make sure LLVM doesn't skip the above entirely we push it through a
+                       // volitile read. This may not be super fast, but it shouldn't be worse than anything a
+                       // user actually does with a log
+                       let s_bytes = s.as_bytes();
+                       for i in 0..s.len() {
+                               let _ = unsafe { core::ptr::read_volatile(&s_bytes[i]) };
+                       }
+               }
+               #[cfg(not(ldk_bench))] {
+                       *self.lines.lock().unwrap().entry((record.module_path, format!("{}", record.args))).or_insert(0) += 1;
+                       *self.context.lock().unwrap().entry((record.module_path, record.peer_id, record.channel_id)).or_insert(0) += 1;
+                       println!("{}", s);
+               }
        }
 }