| 47 | } |
| 48 | |
| 49 | void LuaRoot::shutdown() { |
| 50 | clearScriptCache(); |
| 51 | |
| 52 | if (!m_luaEngine) |
| 53 | return; |
| 54 | |
| 55 | auto profile = m_luaEngine->getProfile(); |
| 56 | if (!profile.empty()) { |
| 57 | profile.sort([](auto const& a, auto const& b) { |
| 58 | return a.totalTime > b.totalTime; |
| 59 | }); |
| 60 | |
| 61 | std::function<Json (LuaProfileEntry const&)> jsonFromProfileEntry = [&](LuaProfileEntry const& entry) -> Json { |
| 62 | JsonObject profile; |
| 63 | profile.set("function", entry.name.value("<function>")); |
| 64 | profile.set("scope", entry.nameScope.value("?")); |
| 65 | profile.set("source", strf("{}:{}", entry.source, entry.sourceLine)); |
| 66 | profile.set("self", entry.selfTime); |
| 67 | profile.set("total", entry.totalTime); |
| 68 | List<LuaProfileEntry> calls; |
| 69 | for (auto p : entry.calls) |
| 70 | calls.append(*p.second); |
| 71 | profile.set("calls", calls.sorted([](auto const& a, auto const& b) { return a.totalTime > b.totalTime; }).transformed(jsonFromProfileEntry)); |
| 72 | return profile; |
| 73 | }; |
| 74 | |
| 75 | String profileSummary = Json(profile.transformed(jsonFromProfileEntry)).repr(1); |
| 76 | |
| 77 | if (!File::isDirectory(m_storageDirectory)) { |
| 78 | Logger::info("Creating lua storage directory"); |
| 79 | File::makeDirectory(m_storageDirectory); |
| 80 | } |
| 81 | |
| 82 | String filename = strf("{}.luaprofile", Time::printCurrentDateAndTime("<year>-<month>-<day>-<hours>-<minutes>-<seconds>-<millis>")); |
| 83 | String path = File::relativeTo(m_storageDirectory, filename); |
| 84 | Logger::info("Writing lua profile {}", filename); |
| 85 | File::writeFile(profileSummary, path); |
| 86 | } |
| 87 | |
| 88 | m_luaEngine.reset(); |
| 89 | } |
| 90 | |
| 91 | LuaContext LuaRoot::createContext(String const& script) { |
| 92 | return createContext(StringList{script}); |