Skip to content

Commit

Permalink
Speed up file writing
Browse files Browse the repository at this point in the history
  • Loading branch information
shilangyu committed Nov 26, 2023
1 parent 137a269 commit 910f94b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
6 changes: 3 additions & 3 deletions benchmark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ set -e

BUILD_TYPE=Release ./build.sh &> /dev/null

MESSAGES=100
PROCESSES=100
MESSAGES=20
PROCESSES=128
RUN_FOLDER=$(mktemp -d)
HOSTS_FILE=$RUN_FOLDER/hosts
OUTPUTS=$RUN_FOLDER/outputs
CONFIG_FILE=$RUN_FOLDER/fifo-broadcast.config
SLEEP_TIME_S=3
SLEEP_TIME_S=10

mkdir $OUTPUTS

Expand Down
8 changes: 7 additions & 1 deletion src/include/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ class Perf {
public:
Perf() {}

template <class result_t = std::chrono::nanoseconds,
template <class clock_t = std::chrono::steady_clock,
class duration_t = std::chrono::nanoseconds>
inline auto mark() -> std::chrono::time_point<clock_t, duration_t> {
return clock_t::now();
}

template <class result_t = std::chrono::milliseconds,
class clock_t = std::chrono::steady_clock,
class duration_t = std::chrono::nanoseconds>
inline auto since(std::string name,
Expand Down
7 changes: 5 additions & 2 deletions src/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <csignal>
#include <iostream>
#include <mutex>
#include <sstream>
#include <thread>
#include <vector>
#include "common.hpp"
Expand Down Expand Up @@ -38,19 +39,21 @@ struct Logger {

inline auto write() -> void {
if (_output.is_open()) {
std::stringstream ss;
const auto& [sent_amount, delivered_size] = _frozen.value_or(
std::make_tuple(static_cast<std::uint32_t>(_sent_amount),
static_cast<std::uint32_t>(_delivered_size)));

for (SendType n = _sent_amount_logged + 1; n <= sent_amount; n++) {
_output << "b " << n << std::endl;
ss << "b " << n << std::endl;
}
_sent_amount_logged = sent_amount;

for (size_t i = 0; i < delivered_size; i++) {
auto& [process_id, msg] = _delivered_buffer[i];
_output << "d " << +process_id << " " << msg << std::endl;
ss << "d " << +process_id << " " << msg << std::endl;
}
_output << ss.str();
}
}

Expand Down

0 comments on commit 910f94b

Please sign in to comment.