From 14c33f90da322d97a05fe476e0ea2e34511fec57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthijs=20Gr=C3=BCnbauer?= Date: Thu, 5 Sep 2024 15:47:32 +0200 Subject: [PATCH] Make benchmark tool work --- adagucserverEC/CImageDataWriter.cpp | 3 +++ adagucserverEC/CRequest.cpp | 4 ++++ adagucserverEC/Definitions.h | 2 +- adagucserverEC/adagucserver.cpp | 32 ++++++++++++------------- python/lib/adaguc/CGIRunner.py | 36 +++++++++++++++++++++-------- 5 files changed, 50 insertions(+), 27 deletions(-) diff --git a/adagucserverEC/CImageDataWriter.cpp b/adagucserverEC/CImageDataWriter.cpp index b1e67590..b0c6d6b3 100644 --- a/adagucserverEC/CImageDataWriter.cpp +++ b/adagucserverEC/CImageDataWriter.cpp @@ -2563,6 +2563,9 @@ int CImageDataWriter::end() { resetErrors(); printf("%s", resultHTML.c_str()); + + fflush(stdout); + fflush(stderr); } /*End of text html */ /* Text XML */ diff --git a/adagucserverEC/CRequest.cpp b/adagucserverEC/CRequest.cpp index 4e615e4f..33e60c37 100644 --- a/adagucserverEC/CRequest.cpp +++ b/adagucserverEC/CRequest.cpp @@ -794,6 +794,7 @@ int CRequest::process_wms_getcap_request() { ; if (status == CXMLGEN_FATAL_ERROR_OCCURED) return 1; } + const char *pszADAGUCWriteToFile = getenv("ADAGUC_WRITETOFILE"); if (pszADAGUCWriteToFile != NULL) { CReadFile::write(pszADAGUCWriteToFile, XMLdocument.c_str(), XMLdocument.length()); @@ -802,6 +803,9 @@ int CRequest::process_wms_getcap_request() { printf("%s", XMLdocument.c_str()); } + fflush(stdout); + fflush(stderr); + return 0; } diff --git a/adagucserverEC/Definitions.h b/adagucserverEC/Definitions.h index c8a65aba..74e33a56 100755 --- a/adagucserverEC/Definitions.h +++ b/adagucserverEC/Definitions.h @@ -126,7 +126,7 @@ // #define CImgWarpBilinear_DEBUG // #define CImgWarpBilinear_TIME -#define MEASURETIME +// #define MEASURETIME // #define CDATAREADER_DEBUG // #define CCDFNETCDFIO_DEBUG diff --git a/adagucserverEC/adagucserver.cpp b/adagucserverEC/adagucserver.cpp index 3b176452..f9b023b4 100644 --- a/adagucserverEC/adagucserver.cpp +++ b/adagucserverEC/adagucserver.cpp @@ -383,16 +383,16 @@ int _main(int argc, char **argv, char **) { // setWarningFunction(serverWarningFunction); // setDebugFunction(serverDebugFunction); -#ifdef MEASURETIME + // #ifdef MEASURETIME StopWatch_Start(); -#endif + // #endif status = runRequest(); /* Display errors if any */ readyerror(); -#ifdef MEASURETIME + // #ifdef MEASURETIME StopWatch_Stop("Ready!!!"); -#endif + // #endif return getStatusCode(); } @@ -476,20 +476,18 @@ void handle_client(int client_socket, int argc, char **argv, char **envp) { char recv_buf[recv_buf_len]; memset(recv_buf, 0, recv_buf_len * sizeof(char)); - char *cmd_query = "QUERY_STRING="; int data_recv = recv(client_socket, recv_buf, recv_buf_len, 0); if (data_recv > 0) { - if (strncmp(recv_buf, cmd_query, strlen(cmd_query)) == 0) { - dup2(client_socket, STDOUT_FILENO); - // dup2(client_socket, STDERR_FILENO); + dup2(client_socket, STDOUT_FILENO); + setenv("QUERY_STRING", recv_buf, 1); - CT::string cmd(recv_buf); - std::vector cmds = cmd.splitToStack("\""); - setenv("QUERY_STRING", cmds[1].c_str(), 1); + int status = do_work(argc, argv, envp); + // fprintf(stderr, "exiting, status=%d", status); - int status = do_work(argc, argv, envp); - exit(status); - } + fflush(stdout); + fflush(stderr); + + exit(status); } } @@ -521,7 +519,6 @@ int run_server(int argc, char **argv, char **envp) { while (1) { unsigned int sock_len = 0; - printf("Waiting for connection.... \n"); if ((client_socket = accept(listen_socket, (struct sockaddr *)&remote, &sock_len)) == -1) { printf("Error on accept() call \n"); @@ -540,6 +537,9 @@ int run_server(int argc, char **argv, char **envp) { } int main(int argc, char **argv, char **envp) { + setvbuf(stdout, NULL, _IONBF, 0); // turn off buffering + setvbuf(stderr, NULL, _IONBF, 0); // turn off buffering + const char *ADAGUC_FORK = getenv("ADAGUC_FORK"); if (ADAGUC_FORK != NULL) { int server_status = run_server(argc, argv, envp); @@ -547,6 +547,4 @@ int main(int argc, char **argv, char **envp) { // normal flow without unix socket server/fork return do_work(argc, argv, envp); } - - // setenv("QUERY_STRING", query_string.c_str()); } diff --git a/python/lib/adaguc/CGIRunner.py b/python/lib/adaguc/CGIRunner.py index 5fce21e2..952349a9 100644 --- a/python/lib/adaguc/CGIRunner.py +++ b/python/lib/adaguc/CGIRunner.py @@ -43,24 +43,42 @@ async def run(self, cmds, url, output, env=[], path=None, isCGI=True, timeout=30 localenv["REQUEST_URI"] = "/myscriptname/" + path localenv.update(env) + # print("# QUERY STRING:", url) + # print("# LOCAL ENV:", localenv) + # Execute adaguc-server binary ON_POSIX = "posix" in sys.builtin_module_names async with sem: - process_output = "" + # process_output = "" - client = socket.socket(socket.AF_UNIX) - client.connect("/tmp/adaguc.socket") - client.send(f"QUERY_STRING={url}".encode()) + # client = socket.socket(socket.AF_UNIX) + # client.connect("/tmp/adaguc.socket") + # client.send(url.encode()) process_output = bytearray() - while data := client.recv(4096): - # print(data) + # while data := client.recv(4096): + # # print(data) + # process_output.extend(data) + + # process_error = "" + # status = 0 + + # process_error = process_error.encode() + + reader, writer = await asyncio.open_unix_connection("/tmp/adaguc.socket") + writer.write(url.encode()) + await writer.drain() + + while data := await reader.read(4096): + # print(f"Received: {data}") process_output.extend(data) - process_error = "" - status = 0 + print("Close the connection") + writer.close() + await writer.wait_closed() - process_error = process_error.encode() + status = 0 + process_error = "".encode() # process = await asyncio.create_subprocess_exec( # *cmds,