[web] Added an option to display execution time

This commit is contained in:
Sidi Liang 2024-11-03 17:30:26 +08:00
parent 037ac9e79f
commit 95031f508b
No known key found for this signature in database
GPG Key ID: 9785F5EECFFA5311
2 changed files with 30 additions and 8 deletions

View File

@ -13,6 +13,7 @@
#include <cstdlib> #include <cstdlib>
#include <cstdio> #include <cstdio>
#include <stdexcept> #include <stdexcept>
#include <chrono>
struct NasalContext { struct NasalContext {
std::unique_ptr<nasal::vm> vm_instance; std::unique_ptr<nasal::vm> vm_instance;
@ -32,7 +33,10 @@ void nasal_cleanup(void* context) {
delete static_cast<NasalContext*>(context); delete static_cast<NasalContext*>(context);
} }
const char* nasal_eval(void* context, const char* code) { const char* nasal_eval(void* context, const char* code, int show_time) {
using clk = std::chrono::high_resolution_clock;
const auto den = clk::duration::period::den;
auto* ctx = static_cast<NasalContext*>(context); auto* ctx = static_cast<NasalContext*>(context);
try { try {
@ -89,16 +93,31 @@ const char* nasal_eval(void* context, const char* code) {
opt->do_optimization(parse.tree()); opt->do_optimization(parse.tree());
gen.compile(parse, ld, false, true).chkerr(); // enable limit_mode for safety gen.compile(parse, ld, false, true).chkerr(); // enable limit_mode for safety
// Run the code // Run the code with optional timing
const auto start = show_time ? clk::now() : clk::time_point();
ctx->vm_instance->run(gen, ld, {}); ctx->vm_instance->run(gen, ld, {});
const auto end = show_time ? clk::now() : clk::time_point();
// Restore stdout and stderr and get the outputs // Restore stdout and stderr and get the outputs
std::cout.rdbuf(old_cout); std::cout.rdbuf(old_cout);
std::cerr.rdbuf(old_cerr); std::cerr.rdbuf(old_cerr);
ctx->last_result = output.str() + error_output.str();
if (ctx->last_result.empty()) { std::stringstream result;
ctx->last_result = "Execution completed successfully."; result << output.str();
if (!error_output.str().empty()) {
result << error_output.str();
} }
if (result.str().empty()) {
result << "Execution completed successfully.\n";
}
// Add execution time if requested
if (show_time) {
double execution_time = static_cast<double>((end-start).count())/den;
result << "\nExecution time: " << execution_time << "s";
}
ctx->last_result = result.str();
// Remove the temporary file // Remove the temporary file
std::remove(temp_filename); std::remove(temp_filename);

View File

@ -1,4 +1,5 @@
#pragma once #ifndef __NASAL_WEB_H__
#define __NASAL_WEB_H__
#include "nasal.h" #include "nasal.h"
@ -15,9 +16,11 @@ extern "C" {
// Main API functions // Main API functions
NASAL_EXPORT void* nasal_init(); NASAL_EXPORT void* nasal_init();
NASAL_EXPORT void nasal_cleanup(void* context); NASAL_EXPORT void nasal_cleanup(void* context);
NASAL_EXPORT const char* nasal_eval(void* context, const char* code); NASAL_EXPORT const char* nasal_eval(void* context, const char* code, int show_time);
NASAL_EXPORT const char* nasal_get_error(void* context); NASAL_EXPORT const char* nasal_get_error(void* context);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif