@@ -503,6 +503,56 @@ pub fn human_readable_size(size: usize) -> String {
503503 format ! ( "{value:.1} {unit}" )
504504}
505505
506+ /// Present count in human-readable form with K, M, B, T suffixes
507+ pub fn human_readable_count ( count : usize ) -> String {
508+ let count = count as u64 ;
509+ let ( value, unit) = {
510+ if count >= 1_000_000_000_000 {
511+ ( count as f64 / 1_000_000_000_000.0 , " T" )
512+ } else if count >= 1_000_000_000 {
513+ ( count as f64 / 1_000_000_000.0 , " B" )
514+ } else if count >= 1_000_000 {
515+ ( count as f64 / 1_000_000.0 , " M" )
516+ } else if count >= 1_000 {
517+ ( count as f64 / 1_000.0 , " K" )
518+ } else {
519+ return count. to_string ( ) ;
520+ }
521+ } ;
522+
523+ // Format with appropriate precision
524+ // For values >= 100, show 1 decimal place (e.g., 123.4 K)
525+ // For values < 100, show 2 decimal places (e.g., 10.12 K)
526+ if value >= 100.0 {
527+ format ! ( "{value:.1}{unit}" )
528+ } else {
529+ format ! ( "{value:.2}{unit}" )
530+ }
531+ }
532+
533+ /// Present duration in human-readable form with 2 decimal places
534+ pub fn human_readable_duration ( nanos : u64 ) -> String {
535+ const NANOS_PER_SEC : f64 = 1_000_000_000.0 ;
536+ const NANOS_PER_MILLI : f64 = 1_000_000.0 ;
537+ const NANOS_PER_MICRO : f64 = 1_000.0 ;
538+
539+ let nanos_f64 = nanos as f64 ;
540+
541+ if nanos >= 1_000_000_000 {
542+ // >= 1 second: show in seconds
543+ format ! ( "{:.2}s" , nanos_f64 / NANOS_PER_SEC )
544+ } else if nanos >= 1_000_000 {
545+ // >= 1 millisecond: show in milliseconds
546+ format ! ( "{:.2}ms" , nanos_f64 / NANOS_PER_MILLI )
547+ } else if nanos >= 1_000 {
548+ // >= 1 microsecond: show in microseconds
549+ format ! ( "{:.2}µs" , nanos_f64 / NANOS_PER_MICRO )
550+ } else {
551+ // < 1 microsecond: show in nanoseconds
552+ format ! ( "{nanos}ns" )
553+ }
554+ }
555+
506556#[ cfg( test) ]
507557mod tests {
508558 use super :: * ;
@@ -599,4 +649,57 @@ mod tests {
599649 assert_eq ! ( r2. size( ) , 25 ) ;
600650 assert_eq ! ( pool. reserved( ) , 28 ) ;
601651 }
652+
653+ #[ test]
654+ fn test_human_readable_count ( ) {
655+ // Test small numbers (< 1000) - should display as-is
656+ assert_eq ! ( human_readable_count( 0 ) , "0" ) ;
657+ assert_eq ! ( human_readable_count( 1 ) , "1" ) ;
658+ assert_eq ! ( human_readable_count( 999 ) , "999" ) ;
659+
660+ // Test thousands (K)
661+ assert_eq ! ( human_readable_count( 1_000 ) , "1.00 K" ) ;
662+ assert_eq ! ( human_readable_count( 10_100 ) , "10.10 K" ) ;
663+ assert_eq ! ( human_readable_count( 1_532 ) , "1.53 K" ) ;
664+ assert_eq ! ( human_readable_count( 99_999 ) , "100.00 K" ) ;
665+
666+ // Test millions (M)
667+ assert_eq ! ( human_readable_count( 1_000_000 ) , "1.00 M" ) ;
668+ assert_eq ! ( human_readable_count( 1_532_000 ) , "1.53 M" ) ;
669+ assert_eq ! ( human_readable_count( 99_000_000 ) , "99.00 M" ) ;
670+ assert_eq ! ( human_readable_count( 123_456_789 ) , "123.5 M" ) ;
671+
672+ // Test billions (B)
673+ assert_eq ! ( human_readable_count( 1_000_000_000 ) , "1.00 B" ) ;
674+ assert_eq ! ( human_readable_count( 1_532_000_000 ) , "1.53 B" ) ;
675+ assert_eq ! ( human_readable_count( 999_999_999_999 ) , "1000.0 B" ) ;
676+
677+ // Test trillions (T)
678+ assert_eq ! ( human_readable_count( 1_000_000_000_000 ) , "1.00 T" ) ;
679+ assert_eq ! ( human_readable_count( 42_000_000_000_000 ) , "42.00 T" ) ;
680+ }
681+
682+ #[ test]
683+ fn test_human_readable_duration ( ) {
684+ // Test nanoseconds (< 1µs)
685+ assert_eq ! ( human_readable_duration( 0 ) , "0ns" ) ;
686+ assert_eq ! ( human_readable_duration( 1 ) , "1ns" ) ;
687+ assert_eq ! ( human_readable_duration( 999 ) , "999ns" ) ;
688+
689+ // Test microseconds (1µs to < 1ms)
690+ assert_eq ! ( human_readable_duration( 1_000 ) , "1.00µs" ) ;
691+ assert_eq ! ( human_readable_duration( 1_234 ) , "1.23µs" ) ;
692+ assert_eq ! ( human_readable_duration( 999_999 ) , "1000.00µs" ) ;
693+
694+ // Test milliseconds (1ms to < 1s)
695+ assert_eq ! ( human_readable_duration( 1_000_000 ) , "1.00ms" ) ;
696+ assert_eq ! ( human_readable_duration( 11_295_377 ) , "11.30ms" ) ;
697+ assert_eq ! ( human_readable_duration( 1_234_567 ) , "1.23ms" ) ;
698+ assert_eq ! ( human_readable_duration( 999_999_999 ) , "1000.00ms" ) ;
699+
700+ // Test seconds (>= 1s)
701+ assert_eq ! ( human_readable_duration( 1_000_000_000 ) , "1.00s" ) ;
702+ assert_eq ! ( human_readable_duration( 1_234_567_890 ) , "1.23s" ) ;
703+ assert_eq ! ( human_readable_duration( 42_000_000_000 ) , "42.00s" ) ;
704+ }
602705}
0 commit comments