Skip to content

Commit 5a006d2

Browse files
authored
Merge pull request #40249 from rintaro/sourcekid-repl-time
[sourcekitd-repl] 'time' requests
2 parents 709d9b9 + 86158c6 commit 5a006d2

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

tools/SourceKit/tools/sourcekitd-repl/sourcekitd-repl.cpp

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414

1515
#include "llvm/ADT/ArrayRef.h"
1616
#include "llvm/Support/CommandLine.h"
17-
#include "llvm/Support/raw_ostream.h"
18-
#include "llvm/Support/Signals.h"
19-
#include "llvm/Support/FileSystem.h"
20-
#include "llvm/Support/Process.h"
2117
#include "llvm/Support/ConvertUTF.h"
18+
#include "llvm/Support/FileSystem.h"
19+
#include "llvm/Support/FormatVariadic.h"
2220
#include "llvm/Support/Mutex.h"
23-
#include <unistd.h>
21+
#include "llvm/Support/Process.h"
22+
#include "llvm/Support/Signals.h"
23+
#include "llvm/Support/raw_ostream.h"
2424
#include <histedit.h>
25+
#include <unistd.h>
2526
using namespace llvm;
2627

2728

@@ -605,11 +606,21 @@ static bool printResponse(sourcekitd_response_t Resp) {
605606

606607
static bool handleRequest(StringRef ReqStr, std::string &ErrorMessage) {
607608
bool UseAsync = false;
608-
ReqStr = ReqStr.ltrim();
609-
if (ReqStr.startswith("async")) {
610-
UseAsync = true;
611-
ReqStr = ReqStr.substr(strlen("async"));
612-
}
609+
bool UseTimer = false;
610+
while (true) {
611+
ReqStr = ReqStr.ltrim();
612+
if (ReqStr.startswith("async")) {
613+
UseAsync = true;
614+
ReqStr = ReqStr.substr(strlen("async"));
615+
continue;
616+
}
617+
if (ReqStr.startswith("time")) {
618+
UseTimer = true;
619+
ReqStr = ReqStr.substr(strlen("time"));
620+
continue;
621+
}
622+
break;
623+
};
613624

614625
SmallString<64> Str(ReqStr);
615626
char *Err = nullptr;
@@ -626,22 +637,33 @@ static bool handleRequest(StringRef ReqStr, std::string &ErrorMessage) {
626637

627638
bool IsError = false;
628639

640+
auto startTime = std::chrono::steady_clock::now();
641+
auto printRequestTime = [UseTimer, startTime](llvm::raw_ostream &OS) {
642+
if (!UseTimer)
643+
return;
644+
std::chrono::duration<float, std::milli> delta(
645+
std::chrono::steady_clock::now() - startTime);
646+
OS << "request time: " << llvm::formatv("{0:ms+f3}", delta) << "\n";
647+
};
648+
649+
llvm::raw_fd_ostream OS(STDOUT_FILENO, /*shouldClose=*/false);
629650
if (UseAsync) {
630651
static unsigned AsyncReqCount = 0;
631652
static llvm::sys::Mutex AsynRespPrintMtx;
632653

633654
unsigned CurrReqCount = ++AsyncReqCount;
634-
llvm::raw_fd_ostream OS(STDOUT_FILENO, /*shouldClose=*/false);
635655
OS << "send async request #" << CurrReqCount << '\n';
636656
sourcekitd_send_request(Req, nullptr, ^(sourcekitd_response_t Resp) {
637657
llvm::sys::ScopedLock L(AsynRespPrintMtx);
638658
llvm::raw_fd_ostream OS(STDOUT_FILENO, /*shouldClose=*/false);
639659
OS << "received async response #" << CurrReqCount << '\n';
660+
printRequestTime(OS);
640661
printResponse(Resp);
641662
});
642663

643664
} else {
644665
sourcekitd_response_t Resp = sourcekitd_send_request_sync(Req);
666+
printRequestTime(OS);
645667
IsError = printResponse(Resp);
646668
}
647669

0 commit comments

Comments
 (0)