comparison OrthancFramework/Sources/Toolbox.cpp @ 5430:b83192e7ad10

Now displaying timings when reading from/writing to disk in the verbose logs
author Alain Mazy <am@osimis.io>
date Mon, 20 Nov 2023 17:01:48 +0100
parents 1c3b0cf341f0
children 48b8dae6dc77
comparison
equal deleted inserted replaced
5429:d40ac28b9702 5430:b83192e7ad10
2512 { 2512 {
2513 value = value.substr(1, value.size() - 2); 2513 value = value.substr(1, value.size() - 2);
2514 } 2514 }
2515 } 2515 }
2516 2516
2517 Toolbox::ElapsedTimer::ElapsedTimer()
2518 {
2519 Restart();
2520 }
2521
2522 void Toolbox::ElapsedTimer::Restart()
2523 {
2524 start_ = boost::posix_time::microsec_clock::universal_time();
2525 }
2526
2527 uint64_t Toolbox::ElapsedTimer::GetElapsedMilliseconds()
2528 {
2529 return GetElapsedNanoseconds() / 1000000;
2530 }
2531
2532 uint64_t Toolbox::ElapsedTimer::GetElapsedMicroseconds()
2533 {
2534 return GetElapsedNanoseconds() / 1000;
2535 }
2536
2537 uint64_t Toolbox::ElapsedTimer::GetElapsedNanoseconds()
2538 {
2539 boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
2540 boost::posix_time::time_duration diff = now - start_;
2541 return static_cast<uint64_t>(diff.total_nanoseconds());
2542 }
2543
2544 std::string Toolbox::ElapsedTimer::GetHumanElapsedDuration()
2545 {
2546 return Toolbox::GetHumanDuration(GetElapsedNanoseconds());
2547 }
2548
2549 // in "full" mode, returns " 26.45MB in 2.25s = 94.04Mbps"
2550 // else, returns "94.04Mbps"
2551 std::string Toolbox::ElapsedTimer::GetHumanTransferSpeed(bool full, uint64_t sizeInBytes)
2552 {
2553 return Toolbox::GetHumanTransferSpeed(full, sizeInBytes, GetElapsedNanoseconds());
2554 }
2555
2517 Toolbox::ElapsedTimeLogger::ElapsedTimeLogger(const std::string& message) 2556 Toolbox::ElapsedTimeLogger::ElapsedTimeLogger(const std::string& message)
2518 : message_(message), 2557 : message_(message),
2519 logged_(false) 2558 logged_(false)
2520 { 2559 {
2521 Restart(); 2560 Restart();
2529 } 2568 }
2530 } 2569 }
2531 2570
2532 void Toolbox::ElapsedTimeLogger::Restart() 2571 void Toolbox::ElapsedTimeLogger::Restart()
2533 { 2572 {
2534 start_ = boost::posix_time::microsec_clock::universal_time(); 2573 timer_.Restart();
2535 } 2574 }
2536 2575
2537 void Toolbox::ElapsedTimeLogger::StopAndLog() 2576 void Toolbox::ElapsedTimeLogger::StopAndLog()
2538 { 2577 {
2539 boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time(); 2578 LOG(WARNING) << "ELAPSED TIMER: " << message_ << " (" << timer_.GetElapsedMicroseconds() << " us)";
2540 boost::posix_time::time_duration diff = now - start_;
2541 LOG(WARNING) << "ELAPSED TIMER: " << message_ << " (" << diff.total_microseconds() << " us)";
2542 logged_ = true; 2579 logged_ = true;
2580 }
2581
2582 std::string Toolbox::GetHumanFileSize(uint64_t sizeInBytes)
2583 {
2584 if (sizeInBytes < 1024)
2585 {
2586 std::ostringstream oss;
2587 oss << sizeInBytes << "bytes";
2588 return oss.str();
2589 }
2590 else
2591 {
2592 static const char* suffixes[] = {"KB", "MB", "GB", "TB"};
2593 static const int suffixesCount = sizeof(suffixes) / sizeof(suffixes[0]);
2594
2595 int i = 0;
2596 double size = static_cast<double>(sizeInBytes)/1024.0;
2597
2598 while (size >= 1024.0 && i < suffixesCount - 1)
2599 {
2600 size /= 1024.0;
2601 i++;
2602 }
2603
2604 std::ostringstream oss;
2605 oss << std::fixed << std::setprecision(2) << size << suffixes[i];
2606 return oss.str();
2607 }
2608 }
2609
2610 std::string Toolbox::GetHumanDuration(uint64_t durationInNanoseconds)
2611 {
2612 if (durationInNanoseconds < 1024)
2613 {
2614 std::ostringstream oss;
2615 oss << durationInNanoseconds << "ns";
2616 return oss.str();
2617 }
2618 else
2619 {
2620 static const char* suffixes[] = {"ns", "us", "ms", "s"};
2621 static const int suffixesCount = sizeof(suffixes) / sizeof(suffixes[0]);
2622
2623 int i = 0;
2624 double duration = static_cast<double>(durationInNanoseconds);
2625
2626 while (duration >= 1000.0 && i < suffixesCount - 1)
2627 {
2628 duration /= 1000.0;
2629 i++;
2630 }
2631
2632 std::ostringstream oss;
2633 oss << std::fixed << std::setprecision(2) << duration << suffixes[i];
2634 return oss.str();
2635 }
2636 }
2637
2638 std::string Toolbox::GetHumanTransferSpeed(bool full, uint64_t sizeInBytes, uint64_t durationInNanoseconds)
2639 {
2640 // in "full" mode, returns " 26.45MB in 2.25s = 94.04Mbps"
2641 // else, return "94.04Mbps"
2642
2643 if (full)
2644 {
2645 std::ostringstream oss;
2646 oss << Toolbox::GetHumanFileSize(sizeInBytes) << " in " << Toolbox::GetHumanDuration(durationInNanoseconds) << " = " << GetHumanTransferSpeed(false, sizeInBytes, durationInNanoseconds);
2647 return oss.str();
2648 }
2649
2650 double throughputInBps = 8.0 * 1000000000.0 * static_cast<double>(sizeInBytes) / static_cast<double>(durationInNanoseconds);
2651
2652 if (throughputInBps < 1000.0)
2653 {
2654 std::ostringstream oss;
2655 oss << throughputInBps << "bps";
2656 return oss.str();
2657 }
2658 else
2659 {
2660 throughputInBps /= 1000.0;
2661 static const char* suffixes[] = {"kbps", "Mbps", "Gbps"};
2662 static const int suffixesCount = sizeof(suffixes) / sizeof(suffixes[0]);
2663
2664 int i = 0;
2665
2666 while (throughputInBps >= 1000.0 && i < suffixesCount - 1)
2667 {
2668 throughputInBps /= 1000.0;
2669 i++;
2670 }
2671
2672 std::ostringstream oss;
2673 oss << std::fixed << std::setprecision(2) << throughputInBps << suffixes[i];
2674 return oss.str();
2675 }
2543 } 2676 }
2544 2677
2545 2678
2546 } 2679 }
2547 2680