Skip to content

Commit

Permalink
Refactor code to include logger in ProgressBar class and update usage…
Browse files Browse the repository at this point in the history
… in AgingScenario class
  • Loading branch information
janjurca committed May 4, 2024
1 parent 2e2bff1 commit b4f1230
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
14 changes: 12 additions & 2 deletions include/filestorm/utils/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <chrono>
#include <iostream>
#include <map>

class ProgressBar {
int total;
Expand All @@ -20,6 +21,8 @@ class ProgressBar {

UnitType unit_type;

std::map<std::string, std::string> metas = {};

public:
ProgressBar(int total, const std::string label) : total(total), label(label) {
unit_type = UnitType::Count;
Expand All @@ -29,13 +32,20 @@ class ProgressBar {
unit_type = UnitType::Time;
print_bar();
}
ProgressBar(const std::string label) : total(0), label(label) {}

void clear_line();
void clear_line(bool overwrite = true);
void disable() { active = false; }

void update(int current);
void update(std::chrono::seconds current) { update(current.count()); }
bool is_active() { return active; }
void print_bar();
void print_bar(bool clear = false);
void set_meta(const std::string &key, const std::string &value) { metas[key] = value; }
void get_meta(const std::string &key) { metas[key]; }
void clear_meta(const std::string &key) { metas.erase(key); }
void set_total(int total);
void set_total(std::chrono::seconds total);

ProgressBar &operator++();
ProgressBar operator++(int);
Expand Down
14 changes: 12 additions & 2 deletions source/scenarios/aging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,15 @@ void AgingScenario::run() {
Result result;

///////// LOGGER settings
ProgressBar bar(getParameter("iterations").get_int(), "Aging Scenario");

ProgressBar bar("Aging Scenario");
if (getParameter("iterations").get_int() != -1) {
logger.warn("Iterations are set to {}", getParameter("iterations").get_int());
bar.set_total(getParameter("iterations").get_int());
} else {
logger.warn("Time is set to {}", getParameter("time").get_string());
bar.set_total(std::chrono::duration_cast<std::chrono::seconds>(max_time));
}
logger.set_progress_bar(&bar);

while ((iteration < getParameter("iterations").get_int() || getParameter("iterations").get_int() == -1)
Expand Down Expand Up @@ -351,7 +359,9 @@ void AgingScenario::run() {
throw std::runtime_error("Null pointer found");
}
iteration++;
bar.update(iteration);
bar.set_meta("extents", fmt::format("{}", tree.total_extents_count));
bar.set_meta("f-count", fmt::format("{}", tree.all_files.size()));
bar.update(std::chrono::duration_cast<std::chrono::seconds>(std::chrono::high_resolution_clock::now() - start));
break;
default:
break;
Expand Down
41 changes: 28 additions & 13 deletions source/utils/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

FilestormLogger logger;

void ProgressBar::clear_line() {
void ProgressBar::clear_line(bool overwrite) {
std::cout << "\r";
for (int i = 0; i < width; i++) {
std::cout << " ";
if (overwrite) {
for (int i = 0; i < width; i++) {
std::cout << " ";
}
std::cout << "\r";
}
std::cout << "\r";
}

void ProgressBar::update(int current) {
this->current = current;
clear_line();
print_bar();
print_bar(true);
}

void ProgressBar::print_bar() {
void ProgressBar::print_bar(bool clear) {
if (!is_active()) {
return;
}
Expand All @@ -42,15 +43,18 @@ void ProgressBar::print_bar() {
}
float progress = (float)current / total;
std::string infos = "";
for (auto meta : metas) {
infos += fmt::format("[{}={}]", meta.first, meta.second);
}
if (unit_type == UnitType::Count) {
std::chrono::duration<double> remaining = std::chrono::duration<double>((total - current) / (current / (elapsed / 1000.0)));
double it_per_s = current / (elapsed / 1000.0);

infos = fmt::format("[{}/{}][{:.1f} it/s][{:.1f}s Remaining]", current, total, it_per_s, remaining.count());
} else {
infos += fmt::format("[{}/{}][{:.1f} it/s][ Remaining]", current, total, it_per_s, fmt::format("{:%H:%M:%S}", remaining));
} else if (unit_type == UnitType::Time) {
std::chrono::duration<int> done = std::chrono::duration<int>(current);
std::chrono::duration<int> total_duration = std::chrono::duration<int>(total);
infos = fmt::format("[{}/{}]", fmt::format("{:%H:%M:%S}", done), fmt::format("{:%H:%M:%S}", total_duration));
infos += fmt::format("[{}/{}]", fmt::format("{:%H:%M:%S}", done), fmt::format("{:%H:%M:%S}", total_duration));
}
std::string prefix_infos = fmt::format("{} {} %", label, (int)(progress * 100));
int bar_width = width - prefix_infos.size() - infos.size() - 2;
Expand All @@ -62,6 +66,9 @@ void ProgressBar::print_bar() {
for (int i = progress_width; i < bar_width; i++) {
bar += " ";
}
if (clear) {
clear_line(false);
}
std::cout << prefix_infos << "[" << bar << "]" << infos;
std::cout.flush();
if (current == total) {
Expand All @@ -72,13 +79,21 @@ void ProgressBar::print_bar() {

ProgressBar& ProgressBar::operator++() {
current++;
clear_line();
print_bar();
print_bar(true);
return *this;
}

ProgressBar ProgressBar::operator++(int) {
ProgressBar temp = *this;
++*this;
return temp;
}
}

void ProgressBar::set_total(int total) {
this->total = total;
unit_type = UnitType::Count;
}
void ProgressBar::set_total(std::chrono::seconds total) {
this->total = total.count();
unit_type = UnitType::Time;
}

0 comments on commit b4f1230

Please sign in to comment.