Adds logging for library build times

This commit is contained in:
Deukhoofd 2021-08-28 16:05:31 +02:00
parent 01b2f29db6
commit 73d50ab98b
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
2 changed files with 103 additions and 46 deletions

99
BuildLibrary.cpp Normal file
View File

@ -0,0 +1,99 @@
#include "BuildLibrary.hpp"
#include <PkmnLib/ScriptResolving/AngelScript/AngelScriptResolver.hpp>
using namespace PkmnLib::Battling;
size_t TypeTimeMs;
size_t NaturesTimeMs;
size_t MovesTimeMs;
size_t SpeciesTimeMs;
size_t ItemsTimeMs;
size_t GrowthRateTimeMs;
size_t ScriptsTimeMs;
void BuildLibrary::LogBuildTimes() {
std::cout << "\tType Library Time: " << TypeTimeMs << "ms" << std::endl;
std::cout << "\tNatures Library Time: " << NaturesTimeMs << "ms" << std::endl;
std::cout << "\tMoves Library Time: " << MovesTimeMs << "ms" << std::endl;
std::cout << "\tSpecies Library Time: " << SpeciesTimeMs << "ms" << std::endl;
std::cout << "\tItems Library Time: " << ItemsTimeMs << "ms" << std::endl;
std::cout << "\tGrowth rates Library Time: " << GrowthRateTimeMs << "ms" << std::endl;
std::cout << "\tScripts Time: " << ScriptsTimeMs << "ms" << std::endl;
}
BattleLibrary* BuildLibrary::Build(const std::string& pathString,
std::function<void(ScriptResolver*)> onScriptInitialize) {
auto path = std::filesystem::path(pathString);
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
auto* typesLibrary = BuildTypes::Build(path / "Types.csv");
TypeTimeMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count();
begin = std::chrono::steady_clock::now();
auto* natureLibrary = BuildNatures::Build(path / "Natures.csv");
NaturesTimeMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count();
begin = std::chrono::steady_clock::now();
auto* movesLibrary = BuildMoves::Build(path / "Moves.json", typesLibrary);
MovesTimeMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count();
begin = std::chrono::steady_clock::now();
auto* speciesLibrary = BuildSpecies::BuildLibrary(path / "Pokemon.json", typesLibrary, movesLibrary);
SpeciesTimeMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count();
begin = std::chrono::steady_clock::now();
auto* itemsLibrary = BuildItems::Build(path / "Items.json");
ItemsTimeMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count();
begin = std::chrono::steady_clock::now();
auto* growthRates = GrowthRatesBuilder::Build(path / "GrowthRates.json");
GrowthRateTimeMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count();
auto scriptsPath = path / "Scripts";
if (typesLibrary == nullptr || speciesLibrary == nullptr || natureLibrary == nullptr || movesLibrary == nullptr ||
itemsLibrary == nullptr || growthRates == nullptr) {
return nullptr;
}
auto settings = new PkmnLib::Library::LibrarySettings(100, 4, 4096);
auto staticLibrary = new PkmnLib::Library::PokemonLibrary(settings, speciesLibrary, movesLibrary, itemsLibrary,
growthRates, typesLibrary, natureLibrary);
begin = std::chrono::steady_clock::now();
auto scriptResolver = BattleLibrary::CreateScriptResolver();
auto battleLib = new BattleLibrary(staticLibrary, new StatCalculator(), new DamageLibrary(false),
new ExperienceLibrary(), scriptResolver, new MiscLibrary(GetTime));
scriptResolver->Initialize(battleLib);
if (onScriptInitialize) {
onScriptInitialize(scriptResolver);
}
auto asScriptResolver = dynamic_cast<AngelScriptResolver*>(scriptResolver);
for (const auto& dirEntry : std::filesystem::recursive_directory_iterator(scriptsPath)) {
if (dirEntry.is_directory())
continue;
if (dirEntry.path().parent_path().stem() == "Interfaces")
continue;
if (dirEntry.path().extension() != ".as")
continue;
std::ifstream in(dirEntry.path());
std::string contents((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
asScriptResolver->CreateScript(dirEntry.path().c_str(), contents.c_str());
}
asScriptResolver->FinalizeModule();
ScriptsTimeMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count();
return battleLib;
}

View File

@ -14,54 +14,12 @@ class BuildLibrary {
static PkmnLib::Library::TimeOfDay GetTime() { return PkmnLib::Library::TimeOfDay::Morning; }
public:
static void LogBuildTimes();
static PkmnLib::Battling::BattleLibrary*
Build(const std::string& pathString,
std::function<void(PkmnLib::Battling::ScriptResolver*)> onScriptInitialize = {}) {
auto path = std::filesystem::path(pathString);
auto* typesLibrary = BuildTypes::Build(path / "Types.csv");
auto* natureLibrary = BuildNatures::Build(path / "Natures.csv");
auto* movesLibrary = BuildMoves::Build(path / "Moves.json", typesLibrary);
auto* speciesLibrary = BuildSpecies::BuildLibrary(path / "Pokemon.json", typesLibrary, movesLibrary);
auto* itemsLibrary = BuildItems::Build(path / "Items.json");
auto* growthRates = GrowthRatesBuilder::Build(path / "GrowthRates.json");
auto scriptsPath = path / "Scripts";
if (typesLibrary == nullptr || speciesLibrary == nullptr || natureLibrary == nullptr ||
movesLibrary == nullptr || itemsLibrary == nullptr || growthRates == nullptr) {
return nullptr;
}
auto settings = new PkmnLib::Library::LibrarySettings(100, 4, 4096);
auto staticLibrary = new PkmnLib::Library::PokemonLibrary(settings, speciesLibrary, movesLibrary, itemsLibrary,
growthRates, typesLibrary, natureLibrary);
auto scriptResolver = PkmnLib::Battling::BattleLibrary::CreateScriptResolver();
auto battleLib = new PkmnLib::Battling::BattleLibrary(
staticLibrary, new PkmnLib::Battling::StatCalculator(), new PkmnLib::Battling::DamageLibrary(false),
new PkmnLib::Battling::ExperienceLibrary(), scriptResolver, new PkmnLib::Battling::MiscLibrary(GetTime));
scriptResolver->Initialize(battleLib);
if (onScriptInitialize) {
onScriptInitialize(scriptResolver);
}
auto asScriptResolver = dynamic_cast<AngelScriptResolver*>(scriptResolver);
for (const auto& dirEntry : std::filesystem::recursive_directory_iterator(scriptsPath)) {
if (dirEntry.is_directory())
continue;
if (dirEntry.path().parent_path().stem() == "Interfaces")
continue;
if (dirEntry.path().extension() != ".as")
continue;
std::ifstream in(dirEntry.path());
std::string contents((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
asScriptResolver->CreateScript(dirEntry.path().c_str(), contents.c_str());
}
asScriptResolver->FinalizeModule();
return battleLib;
}
std::function<void(PkmnLib::Battling::ScriptResolver*)> onScriptInitialize = {});
};
#endif // PKMNLIB_AI_BUILDLIBRARY_HPP