Skip to content

Commit

Permalink
-- run clang-format on all source files
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusBgm committed May 7, 2024
1 parent 1c88546 commit e3eec83
Show file tree
Hide file tree
Showing 523 changed files with 11,543 additions and 11,394 deletions.
56 changes: 26 additions & 30 deletions Demos/Benchmark/BenchmarkDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void PrintUsage(const std::string& executableName)
struct BenchmarkConfig
{
uint32_t numberOfSimulationRuns = 4;
std::chrono::seconds simulationDuration = 1s;
std::chrono::seconds simulationDuration = 1s;
uint32_t numberOfParticipants = 2;
uint32_t messageCount = 50;
uint32_t messageSizeInBytes = 1000;
Expand All @@ -94,12 +94,8 @@ bool Parse(int argc, char** argv, BenchmarkConfig& config)
std::vector<std::string> args;
std::copy((argv + 1), (argv + argc), std::back_inserter(args));

auto asNum = [](const auto& str) {
return static_cast<uint32_t>(std::stoul(str));
};
auto asStr = [](auto& a) {
return std::string{a};
};
auto asNum = [](const auto& str) { return static_cast<uint32_t>(std::stoul(str)); };
auto asStr = [](auto& a) { return std::string{a}; };

// test and remove the flag from args, returns true if flag was present
auto consumeFlag = [&args](const auto& namedOption) {
Expand Down Expand Up @@ -204,7 +200,9 @@ bool Parse(int argc, char** argv, BenchmarkConfig& config)
case 2:
config.simulationDuration = std::chrono::seconds(asNum(args.at(1)));
// [[fallthrough]]
case 1: config.numberOfSimulationRuns = asNum(args.at(0)); break;
case 1:
config.numberOfSimulationRuns = asNum(args.at(0));
break;
default:
if (haveUserOptions)
{
Expand Down Expand Up @@ -263,7 +261,7 @@ uint32_t relateParticipant(uint32_t idx, uint32_t numberOfParticipants)
}
else
{
return idx+1;
return idx + 1;
}
}

Expand Down Expand Up @@ -293,7 +291,7 @@ void ParticipantsThread(std::shared_ptr<SilKit::Config::IParticipantConfiguratio
const std::string topicSub =
"Topic" + std::to_string(relateParticipant(participantIndex, benchmark.numberOfParticipants));
SilKit::Services::PubSub::PubSubSpec dataSpec{topicPub, {}};
SilKit::Services::PubSub::PubSubSpec matchingDataSpec{topicSub, {}};
SilKit::Services::PubSub::PubSubSpec matchingDataSpec{topicSub, {}};
auto publisher = participant->CreateDataPublisher("PubCtrl1", dataSpec, 0);
participant->CreateDataSubscriber("SubCtrl1", matchingDataSpec, [&messageCounter](auto*, auto&) {
// this is handled in I/O thread, so no data races on counter.
Expand All @@ -303,24 +301,24 @@ void ParticipantsThread(std::shared_ptr<SilKit::Config::IParticipantConfiguratio
const auto isVerbose = participantIndex == 0;
timeSyncService->SetSimulationStepHandler(
[=, &publisher](std::chrono::nanoseconds now, const auto /*duration*/) {
if (now > benchmark.simulationDuration)
{
lifecycleService->Stop("Simulation done");
}
if (now > benchmark.simulationDuration)
{
lifecycleService->Stop("Simulation done");
}

if (isVerbose)
{
const auto simulationDurationInNs =
std::chrono::duration_cast<std::chrono::nanoseconds>(benchmark.simulationDuration);
const auto durationOfOneSimulationPercentile = simulationDurationInNs / 20;
if (isVerbose)
{
const auto simulationDurationInNs =
std::chrono::duration_cast<std::chrono::nanoseconds>(benchmark.simulationDuration);
const auto durationOfOneSimulationPercentile = simulationDurationInNs / 20;

if (now % durationOfOneSimulationPercentile < stepSize)
{
std::cout << ".";
}
if (now % durationOfOneSimulationPercentile < stepSize)
{
std::cout << ".";
}
PublishMessages(publisher, benchmark.messageCount, benchmark.messageSizeInBytes);
},
}
PublishMessages(publisher, benchmark.messageCount, benchmark.messageSizeInBytes);
},
stepSize);

auto lifecycleFuture = lifecycleService->StartLifecycle();
Expand Down Expand Up @@ -408,7 +406,7 @@ int main(int argc, char** argv)
registry->StartListening(benchmark.registryUri);

std::vector<size_t> messageCounts;
std::vector<std::chrono::nanoseconds> measuredRealDurations;
std::vector<std::chrono::nanoseconds> measuredRealDurations;

for (uint32_t simulationRun = 1; simulationRun <= benchmark.numberOfSimulationRuns; simulationRun++)
{
Expand Down Expand Up @@ -451,15 +449,13 @@ int main(int argc, char** argv)
auto endTimestamp = std::chrono::high_resolution_clock::now();
measuredRealDurations.emplace_back(endTimestamp - startTimestamp);
auto totalCount = std::accumulate(counters.begin(), counters.end(), size_t{0});
messageCounts.emplace_back(totalCount);
messageCounts.emplace_back(totalCount);
std::cout << " " << measuredRealDurations.back() << std::endl;
}

std::vector<double> measuredRealDurationsSeconds(measuredRealDurations.size());
std::transform(measuredRealDurations.begin(), measuredRealDurations.end(), measuredRealDurationsSeconds.begin(),
[](auto d) {
return static_cast<double>(d.count() / 1e9);
});
[](auto d) { return static_cast<double>(d.count() / 1e9); });

const auto averageDuration = mean_and_error(measuredRealDurationsSeconds);

Expand Down
104 changes: 54 additions & 50 deletions Demos/Benchmark/LatencyDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,8 @@ bool Parse(int argc, char** argv, BenchmarkConfig& config)
std::vector<std::string> args;
std::copy((argv + 1), (argv + argc), std::back_inserter(args));

auto asNum = [](const auto& str) {
return static_cast<uint32_t>(std::stoul(str));
};
auto asStr = [](auto& a) {
return std::string{a};
};
auto asNum = [](const auto& str) { return static_cast<uint32_t>(std::stoul(str)); };
auto asStr = [](auto& a) { return std::string{a}; };

// test and remove the flag from args, returns true if flag was present
auto consumeFlag = [&args](const auto& namedOption) {
Expand Down Expand Up @@ -188,7 +184,9 @@ bool Parse(int argc, char** argv, BenchmarkConfig& config)
case 2:
config.messageSizeInBytes = asNum(args.at(1));
// [[fallthrough]]
case 1: config.messageCount = asNum(args.at(0)); break;
case 1:
config.messageCount = asNum(args.at(0));
break;
default:
if (haveUserOptions)
{
Expand Down Expand Up @@ -245,7 +243,8 @@ void PrintParameters(BenchmarkConfig benchmark)
<< std::endl
<< "Running simulations with the following parameters:" << std::endl
<< std::endl
<< std::left << std::setw(38) << "- Is Receiver: " << (benchmark.isReceiver ? "True" : "False") << std::endl
<< std::left << std::setw(38) << "- Is Receiver: " << (benchmark.isReceiver ? "True" : "False")
<< std::endl
<< std::left << std::setw(38) << "- Total message count: " << benchmark.messageCount << std::endl
<< std::left << std::setw(38) << "- Message size (bytes): " << benchmark.messageSizeInBytes << std::endl
<< std::left << std::setw(38) << "- Registry URI: " << benchmark.registryUri << std::endl
Expand Down Expand Up @@ -315,38 +314,39 @@ int main(int argc, char** argv)
std::string topicPub = benchmark.isReceiver ? "Pong" : "Ping";
std::string topicSub = benchmark.isReceiver ? "Ping" : "Pong";
auto publisher = participant->CreateDataPublisher("PubCtrl1", {topicPub, {}}, 1);
participant->CreateDataSubscriber(
"SubCtrl1", {topicSub, {}},
[data, publisher, benchmark, &sendCount, &allSent, &allSentPromise,
participant->CreateDataSubscriber("SubCtrl1", {topicSub, {}},
[data, publisher, benchmark, &sendCount, &allSent, &allSentPromise,

&measuredRoundtrips, &sendTime, &startTimestamp](auto*, auto&) {
if (!allSent)
&measuredRoundtrips, &sendTime, &startTimestamp](auto*, auto&) {
if (!allSent)
{
if (!benchmark.isReceiver)
{
if (!benchmark.isReceiver)
if (sendCount == 0)
{
if (sendCount == 0)
{
startTimestamp = std::chrono::high_resolution_clock::now(); // Initial receive: Start runtime measurement
}
else
{
measuredRoundtrips.push_back(std::chrono::high_resolution_clock::now() - sendTime);
}
sendTime = std::chrono::high_resolution_clock::now();
startTimestamp =
std::chrono::high_resolution_clock::now(); // Initial receive: Start runtime measurement
}
publisher->Publish(data);
sendCount++;
if (benchmark.isReceiver && (benchmark.messageCount <= 20 || sendCount % (benchmark.messageCount / 20) == 0))
else
{
std::cout << ".";
}
if (sendCount >= benchmark.messageCount+1) // Initial publish has no timing, use +1
{
allSentPromise.set_value();
allSent = true;
measuredRoundtrips.push_back(std::chrono::high_resolution_clock::now() - sendTime);
}
sendTime = std::chrono::high_resolution_clock::now();
}
publisher->Publish(data);
sendCount++;
if (benchmark.isReceiver
&& (benchmark.messageCount <= 20 || sendCount % (benchmark.messageCount / 20) == 0))
{
std::cout << ".";
}
});
if (sendCount >= benchmark.messageCount + 1) // Initial publish has no timing, use +1
{
allSentPromise.set_value();
allSent = true;
}
}
});

if (!benchmark.isReceiver) // Initial publish without timing
{
Expand All @@ -371,13 +371,13 @@ int main(int argc, char** argv)
allDonePublisher->Publish(std::vector<uint8_t>{0});
}
participant->CreateDataSubscriber("SubCtrl2", {topicSubAllDone, {}},
[&syncParticipants, benchmark, allDonePublisher](auto*, auto&) {
if (!benchmark.isReceiver)
{
allDonePublisher->Publish(std::vector<uint8_t>{0});
}
syncParticipants.set_value();
});
[&syncParticipants, benchmark, allDonePublisher](auto*, auto&) {
if (!benchmark.isReceiver)
{
allDonePublisher->Publish(std::vector<uint8_t>{0});
}
syncParticipants.set_value();
});
auto syncParticipantsFuture = syncParticipants.get_future();
syncParticipantsFuture.wait();
std::cout << "... done." << std::endl;
Expand All @@ -394,8 +394,9 @@ int main(int argc, char** argv)
std::vector<double> measuredLatencySeconds(measuredRoundtrips.size());
std::transform(measuredRoundtrips.begin(), measuredRoundtrips.end(), measuredLatencySeconds.begin(),
[](auto d) {
return static_cast<double>(d.count() / 1.e3 * 0.5); // Convert to microseconds, factor 0.5 for latency from roundtrip
});
return static_cast<double>(d.count() / 1.e3
* 0.5); // Convert to microseconds, factor 0.5 for latency from roundtrip
});

const auto averageLatency = mean_and_error(measuredLatencySeconds);
std::ostringstream averageLatencyWithUnit;
Expand All @@ -417,16 +418,20 @@ int main(int argc, char** argv)
durationWithUnit.precision(3);
durationWithUnit << durationSeconds << " s";

std::cout << std::setw(38) << "- Realtime duration (runtime): " << std::setw(6) << durationWithUnit.str() << std::endl
<< std::setw(38) << "- Throughput (data size/runtime): " << std::setw(6) << throughputWithUnit.str() << std::endl
<< std::setw(38) << "- Latency: " << std::setw(6) << averageLatencyWithUnit.str() << " +/- " << averageLatency.second << std::endl
std::cout << std::setw(38) << "- Realtime duration (runtime): " << std::setw(6) << durationWithUnit.str()
<< std::endl
<< std::setw(38) << "- Throughput (data size/runtime): " << std::setw(6) << throughputWithUnit.str()
<< std::endl
<< std::setw(38) << "- Latency: " << std::setw(6) << averageLatencyWithUnit.str() << " +/- "
<< averageLatency.second << std::endl
<< std::endl;

if (benchmark.writeCsv != "")
{
std::stringstream csvHeader;
csvHeader << "# SilKitBenchmarkDemo, SIL Kit Version " << SilKit::Version::String();
const auto csvColumns = "messageSize; messageCount; runtime(s); throughput(MiB/s); latency(us); latency_err";
const auto csvColumns =
"messageSize; messageCount; runtime(s); throughput(MiB/s); latency(us); latency_err";
std::fstream csvFile;
csvFile.open(benchmark.writeCsv, std::ios_base::in | std::ios_base::out); // Try to open
bool csvValid{true};
Expand All @@ -453,10 +458,9 @@ int main(int argc, char** argv)
{
// Append data
csvFile.seekp(0, std::ios_base::end);
csvFile << benchmark.messageSizeInBytes << ";" << benchmark.messageCount << ";"
<< durationSeconds << ";"
<< throughput << ";"
<< averageLatency.first << ";" << averageLatency.second << std::endl;
csvFile << benchmark.messageSizeInBytes << ";" << benchmark.messageCount << ";" << durationSeconds
<< ";" << throughput << ";" << averageLatency.first << ";" << averageLatency.second
<< std::endl;
}
csvFile.close();
}
Expand Down
Loading

0 comments on commit e3eec83

Please sign in to comment.