Skip to content

Commit

Permalink
Simplify code for timer (#2032)
Browse files Browse the repository at this point in the history
close  #2014

- Remove `IDurationCollector` and `NullDurationCollector`, keep only
`DurationCollector`
- Add new syntax
- Add unit tests

---------

Co-authored-by: Florian OMNES <[email protected]>
Co-authored-by: Florian Omnès <[email protected]>
  • Loading branch information
3 people authored May 6, 2024
1 parent b623f7f commit 9beae3d
Show file tree
Hide file tree
Showing 34 changed files with 309 additions and 133 deletions.
2 changes: 1 addition & 1 deletion src/libs/antares/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ import_std_libs(antares-core)

install(DIRECTORY include/antares
DESTINATION "include"
)
)
31 changes: 30 additions & 1 deletion src/libs/antares/benchmarking/DurationCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,38 @@ void DurationCollector::toFileContent(FileContent& file_content)
{
for (const auto& [name, durations] : duration_items_)
{
const int64_t duration_sum = accumulate(durations.begin(), durations.end(), (int64_t)0);
const int64_t duration_sum = accumulate(durations.begin(), durations.end(), 0);

file_content.addDurationItem(name, (unsigned int)duration_sum, (int)durations.size());
}
}

DurationCollector::OperationTimer DurationCollector::operator()(const std::string& key)
{
return OperationTimer(*this, key);
}

void DurationCollector::OperationTimer::addDuration(int64_t duration_ms) const
{
const std::scoped_lock lock(collector.mutex_);
collector.duration_items_[key].push_back(duration_ms);
}

void operator<<(const DurationCollector::OperationTimer& op, const std::function<void(void)>& f)
{
using clock = std::chrono::steady_clock;
auto start_ = clock::now();
f();
auto end_ = clock::now();
auto duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end_ - start_).count();
op.addDuration(duration_ms);
}

int64_t DurationCollector::getTime(const std::string& name) const
{
const auto& v = duration_items_.at(name);

return accumulate(v.begin(), v.end(), 0);
}

} // namespace Benchmarking
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
#pragma once

#include <functional>
#include <chrono>
#include <vector>
#include <map>
#include <string>
Expand All @@ -30,31 +32,27 @@
namespace Benchmarking
{

class IDurationCollector
class DurationCollector
{
public:
virtual ~IDurationCollector() = default;
virtual void addDuration(const std::string& name, int64_t duration) = 0;
};
void toFileContent(FileContent& file_content);
void addDuration(const std::string& name, int64_t duration);

class NullDurationCollector : public IDurationCollector
{
public:
NullDurationCollector() = default;
virtual ~NullDurationCollector() = default;
void addDuration(const std::string& /* name */, int64_t /* duration */) override
{ /* Do nothing */
}
};
struct OperationTimer
{
OperationTimer(DurationCollector& collector, const std::string& key):
collector(collector), key(key) {}
void addDuration(int64_t duration_ms) const;

class DurationCollector : public IDurationCollector
{
public:
DurationCollector() = default;
virtual ~DurationCollector() = default;
DurationCollector& collector;
const std::string key;
};

void toFileContent(FileContent& file_content);
void addDuration(const std::string& name, int64_t duration) override;
OperationTimer operator()(const std::string& key);

friend void operator<<(const OperationTimer& op, const std::function<void(void)>& f);

int64_t getTime(const std::string& name) const;

private:
std::map<std::string, std::vector<int64_t>> duration_items_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#ifndef __ANTARES_LIBS_TIME_ELAPSED__TIME_ELAPSED_H__
#define __ANTARES_LIBS_TIME_ELAPSED__TIME_ELAPSED_H__

#include <yuni/yuni.h>
#include <cstdint>

namespace Benchmarking
{
Expand Down
2 changes: 1 addition & 1 deletion src/libs/antares/checks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ target_link_libraries(checks

install(DIRECTORY include/antares
DESTINATION "include"
)
)
2 changes: 1 addition & 1 deletion src/libs/antares/study/include/antares/study/fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ class Sets;

namespace Benchmarking
{
class IDurationCollector;
class DurationCollector;
}

#endif // __ANTARES_LIBS_STUDY_FWD_H__
24 changes: 22 additions & 2 deletions src/libs/antares/writer/in_memory_writer.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
/*
** Copyright 2007-2024, RTE (https://www.rte-france.com)
** See AUTHORS.txt
** SPDX-License-Identifier: MPL-2.0
** This file is part of Antares-Simulator,
** Adequacy and Performance assessment for interconnected energy networks.
**
** Antares_Simulator is free software: you can redistribute it and/or modify
** it under the terms of the Mozilla Public Licence 2.0 as published by
** the Mozilla Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** Antares_Simulator is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** Mozilla Public Licence 2.0 for more details.
**
** You should have received a copy of the Mozilla Public Licence 2.0
** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
*/
#include <algorithm>

#include <antares/writer/in_memory_writer.h>
Expand All @@ -23,7 +43,7 @@ void logErrorAndThrow [[noreturn]] (const std::string& errorMessage)
const std::string& entryPath,
ContentT& content,
std::mutex& mutex,
Benchmarking::IDurationCollector& duration_collector)
Benchmarking::DurationCollector& duration_collector)
{
std::string entryPathSanitized = entryPath;
std::replace(entryPathSanitized.begin(),
Expand All @@ -44,7 +64,7 @@ void logErrorAndThrow [[noreturn]] (const std::string& errorMessage)
}


InMemoryWriter::InMemoryWriter(Benchmarking::IDurationCollector& duration_collector) : pDurationCollector(duration_collector) {}
InMemoryWriter::InMemoryWriter(Benchmarking::DurationCollector& duration_collector) : pDurationCollector(duration_collector) {}

InMemoryWriter::~InMemoryWriter() = default;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class InMemoryWriter : public IResultWriter
{
public:
using MapType = std::map<std::string, std::string, std::less<>>;
explicit InMemoryWriter(Benchmarking::IDurationCollector& duration_collector);
explicit InMemoryWriter(Benchmarking::DurationCollector& duration_collector);
virtual ~InMemoryWriter();
void addEntryFromBuffer(const std::string& entryPath, Yuni::Clob& entryContent) override;
void addEntryFromBuffer(const std::string& entryPath, std::string& entryContent) override;
Expand All @@ -49,7 +49,7 @@ class InMemoryWriter : public IResultWriter
// minizip-ng requires a void* as a zip handle.
MapType pEntries;
// State, to allow/prevent new jobs being added to the queue
Benchmarking::IDurationCollector& pDurationCollector;
Benchmarking::DurationCollector& pDurationCollector;
};
} // namespace Antares::Solver

Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
#include "result_format.h"

namespace Benchmarking {
class IDurationCollector;
class DurationCollector;
}

namespace Antares::Solver
{
IResultWriter::Ptr resultWriterFactory(Antares::Data::ResultFormat fmt,
const YString& folderOutput,
std::shared_ptr<Yuni::Job::QueueService> qs,
Benchmarking::IDurationCollector& duration_collector);
Benchmarking::DurationCollector& duration_collector);
}

8 changes: 4 additions & 4 deletions src/libs/antares/writer/private/zip_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ZipWriteJob
ZipWriteJob(ZipWriter& writer,
std::string entryPath,
ContentT& content,
Benchmarking::IDurationCollector& duration_collector);
Benchmarking::DurationCollector& duration_collector);
void writeEntry();
void operator()() {
writeEntry();
Expand All @@ -70,15 +70,15 @@ class ZipWriteJob
// Content of the new file
ContentT pContent;
// Benchmarking. How long do we wait ? How long does the zip write take ?
Benchmarking::IDurationCollector& pDurationCollector;
Benchmarking::DurationCollector& pDurationCollector;
};

class ZipWriter : public IResultWriter
{
public:
ZipWriter(std::shared_ptr<Yuni::Job::QueueService> qs,
const char* archivePath,
Benchmarking::IDurationCollector& duration_collector);
Benchmarking::DurationCollector& duration_collector);
virtual ~ZipWriter();
void addEntryFromBuffer(const std::string& entryPath, Yuni::Clob& entryContent) override;
void addEntryFromBuffer(const std::string& entryPath, std::string& entryContent) override;
Expand All @@ -101,7 +101,7 @@ class ZipWriter : public IResultWriter
// Absolute path to the archive
const std::string pArchivePath;
// Benchmarking. Passed to jobs
Benchmarking::IDurationCollector& pDurationCollector;
Benchmarking::DurationCollector& pDurationCollector;

Concurrency::FutureSet pendingTasks_;

Expand Down
2 changes: 1 addition & 1 deletion src/libs/antares/writer/writer_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Antares::Solver
IResultWriter::Ptr resultWriterFactory(Antares::Data::ResultFormat fmt,
const YString& folderOutput,
std::shared_ptr<Yuni::Job::QueueService> qs,
Benchmarking::IDurationCollector& duration_collector)
Benchmarking::DurationCollector& duration_collector)
{
using namespace Antares::Data;
switch (fmt)
Expand Down
4 changes: 2 additions & 2 deletions src/libs/antares/writer/zip_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ template<class ContentT>
ZipWriteJob<ContentT>::ZipWriteJob(ZipWriter& writer,
std::string entryPath,
ContentT& content,
Benchmarking::IDurationCollector& duration_collector) :
Benchmarking::DurationCollector& duration_collector) :
pZipHandle(writer.pZipHandle),
pZipMutex(writer.pZipMutex),
pState(writer.pState),
Expand Down Expand Up @@ -110,7 +110,7 @@ void ZipWriteJob<ContentT>::writeEntry()
// Class ZipWriter
ZipWriter::ZipWriter(std::shared_ptr<Yuni::Job::QueueService> qs,
const char* archivePath,
Benchmarking::IDurationCollector& duration_collector) :
Benchmarking::DurationCollector& duration_collector) :
pQueueService(qs),
pState(ZipState::can_receive_data),
pArchivePath(std::string(archivePath) + ".zip"),
Expand Down
2 changes: 1 addition & 1 deletion src/packaging/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/AntaresConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/AntaresConfigVersion.cmake"
DESTINATION lib/cmake/Antares
)
)
18 changes: 8 additions & 10 deletions src/solver/application/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ void Application::resetLogFilename() const
}

void Application::prepareWriter(const Antares::Data::Study& study,
Benchmarking::IDurationCollector& duration_collector)
Benchmarking::DurationCollector& duration_collector)
{
ioQueueService = std::make_shared<Yuni::Job::QueueService>();
ioQueueService->maximumThreadCount(1);
Expand Down Expand Up @@ -324,14 +324,13 @@ void Application::readDataForTheStudy(Data::StudyLoadOptions& options)
std::exception_ptr loadingException;
try
{
if (study.loadFromFolder(pSettings.studyFolder, options))
{
logs.info() << "The study is loaded.";
logs.info() << LOG_UI_DISPLAY_MESSAGES_OFF;
}

timer.stop();
pDurationCollector.addDuration("study_loading", timer.get_duration());
pDurationCollector("study_loading") << [&] {
if (study.loadFromFolder(pSettings.studyFolder, options))
{
logs.info() << "The study is loaded.";
logs.info() << LOG_UI_DISPLAY_MESSAGES_OFF;
}
};

if (study.areas.empty())
{
Expand Down Expand Up @@ -460,7 +459,6 @@ void Application::writeExectutionInfo()
if (!pStudy)
return;

// Last missing duration to get : measure of total simulation duration
pTotalTimer.stop();
pDurationCollector.addDuration("total", pTotalTimer.get_duration());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class Application final : public Yuni::IEventObserver<Application, Yuni::Policy:
IResultWriter::Ptr resultWriter = nullptr;

void prepareWriter(const Antares::Data::Study& study,
Benchmarking::IDurationCollector& duration_collector);
Benchmarking::DurationCollector& duration_collector);

void writeComment(Data::Study& study);
void startSimulation(Data::StudyLoadOptions& options);
Expand Down
4 changes: 2 additions & 2 deletions src/solver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ void logAbortion()
*/
int main(int argc, char** argv)
{
try {

try
{
logs.info(ANTARES_LOGO);
logs.info(MPL_ANNOUNCEMENT);
// Name of the running application for the logger
Expand Down
49 changes: 49 additions & 0 deletions src/solver/main/economy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
** Copyright 2007-2024, RTE (https://www.rte-france.com)
** See AUTHORS.txt
** SPDX-License-Identifier: MPL-2.0
** This file is part of Antares-Simulator,
** Adequacy and Performance assessment for interconnected energy networks.
**
** Antares_Simulator is free software: you can redistribute it and/or modify
** it under the terms of the Mozilla Public Licence 2.0 as published by
** the Mozilla Foundation, either version 2 of the License, or
** (at your option) any later version.
**
** Antares_Simulator is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** Mozilla Public Licence 2.0 for more details.
**
** You should have received a copy of the Mozilla Public Licence 2.0
** along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
*/


#include <antares/benchmarking/DurationCollector.h>
#include <antares/logs/logs.h>
#include "antares/application/application.h"
#include "antares/solver/simulation/solver.h"
#include "antares/solver/simulation/economy.h"

namespace Antares::Solver
{
void Application::runSimulationInEconomicMode()
{
// Type of the simulation
typedef Solver::Simulation::ISimulation<Solver::Simulation::Economy> SimulationType;
SimulationType simulation(*pStudy, pSettings, pDurationCollector, *resultWriter);
simulation.checkWriter();
simulation.run();

if (!(pSettings.noOutput || pSettings.tsGeneratorsOnly))
{
pDurationCollector("synthesis_export") << [&simulation] {
simulation.writeResults(/*synthesis:*/ true);
};

this->pOptimizationInfo = simulation.getOptimizationInfo();
}
}
} // namespace Antares::Solver

2 changes: 1 addition & 1 deletion src/solver/simulation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ target_link_libraries(antares-solver-simulation

install(DIRECTORY include/antares
DESTINATION "include"
)
)
Loading

0 comments on commit 9beae3d

Please sign in to comment.