33#include < filesystem>
44#include < vector>
55#include < fstream>
6+ #include < chrono>
67#include " CodeService/LuaFormatter.h"
78#include " Util/format.h"
89#include " Util/CommandLine.h"
910#include " Util/StringUtil.h"
1011#include " CodeService/LuaEditorConfig.h"
1112
13+ using namespace std ::chrono;
1214
1315bool TestFormatted (std::string input, const std::string& shouldBe, std::shared_ptr<LuaCodeStyleOptions> options)
1416{
@@ -24,6 +26,21 @@ bool TestFormatted(std::string input, const std::string& shouldBe, std::shared_p
2426 return StringUtil::TrimSpace (formattedText) == StringUtil::TrimSpace (shouldBe);
2527}
2628
29+ uint64_t TestPerformance (std::string input, std::shared_ptr<LuaCodeStyleOptions> options)
30+ {
31+ auto start = steady_clock::now ();
32+ auto parser = LuaParser::LoadFromBuffer (std::move (input));
33+ parser->BuildAstWithComment ();
34+
35+ LuaFormatter formatter (parser, *options);
36+
37+ formatter.BuildFormattedElement ();
38+
39+ volatile auto formattedText = formatter.GetFormattedText ();
40+
41+ return duration_cast<milliseconds>(steady_clock::now () - start).count ();
42+ }
43+
2744bool TestGrammar (std::string input)
2845{
2946 auto parser = LuaParser::LoadFromBuffer (std::move (input));
@@ -114,6 +131,7 @@ int main(int argc, char* argv[])
114131 commandLine.AddTarget (" CheckGrammar" );
115132 commandLine.AddTarget (" CheckFormatResult" );
116133 commandLine.AddTarget (" CheckFormatResultByOption" );
134+ commandLine.AddTarget (" Performance" );
117135
118136 commandLine.Add <std::string>(" work-directory" , " w" , " special base work directory" );
119137 commandLine.Add <std::string>(" formatted-work-directory" , " f" , " special formatted work directory" );
@@ -206,6 +224,21 @@ int main(int argc, char* argv[])
206224 std::cout << format (" test check grammar {} ... {}" , path, passed ? " passed" : " false" ) << std::endl;
207225 }
208226 }
227+ else if (target == " Performance" )
228+ {
229+ auto options = std::make_shared<LuaCodeStyleOptions>();
230+ #ifndef _WIN32
231+ options->end_of_line = " \n " ;
232+ #endif
233+ for (auto & path : luaFiles)
234+ {
235+ auto waitFormattingFilePath = workRoot / path;
236+ auto waitFormattingText = ReadFile (waitFormattingFilePath.string ());
237+
238+ auto time = TestPerformance (waitFormattingText, options);
239+ std::cout << format (" test format Performance {} ...it cost {}ms" , path, time ) << std::endl;
240+ }
241+ }
209242
210243
211244 return success ? 0 : -1 ;
0 commit comments