MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

performance.(module)

+88 -1
+6
include/modules/performance.h
··· 1 + #ifndef PERFORMANCE_H 2 + #define PERFORMANCE_H 3 + 4 + void init_performance_module(void); 5 + 6 + #endif
+1 -1
meson.build
··· 74 74 build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() 75 75 76 76 version_conf = configuration_data() 77 - version_conf.set('ANT_VERSION', '0.0.8.16') 77 + version_conf.set('ANT_VERSION', '0.0.8.17') 78 78 version_conf.set('ANT_GIT_HASH', git_hash) 79 79 version_conf.set('ANT_BUILD_DATE', build_date) 80 80
+2
src/main.c
··· 25 25 #include "modules/path.h" 26 26 #include "modules/ffi.h" 27 27 #include "modules/events.h" 28 + #include "modules/performance.h" 28 29 29 30 int js_result = EXIT_SUCCESS; 30 31 ··· 178 179 init_timer_module(); 179 180 init_process_module(); 180 181 init_events_module(); 182 + init_performance_module(); 181 183 182 184 ant_register_library("ant:fs", fs_library); 183 185 ant_register_library("ant:shell", shell_library);
+41
src/modules/performance.c
··· 1 + #include <stdio.h> 2 + #include <stdlib.h> 3 + #include <sys/time.h> 4 + #include <time.h> 5 + 6 + #include "runtime.h" 7 + #include "modules/performance.h" 8 + 9 + static double time_origin_ms = 0; 10 + 11 + static double get_current_time_ms(void) { 12 + struct timeval tv; 13 + gettimeofday(&tv, NULL); 14 + return (double)tv.tv_sec * 1000.0 + (double)tv.tv_usec / 1000.0; 15 + } 16 + 17 + // performance.now() 18 + static jsval_t js_performance_now(struct js *js, jsval_t *args, int nargs) { 19 + (void) args; (void) nargs; 20 + double now = get_current_time_ms() - time_origin_ms; 21 + return js_mknum(now); 22 + } 23 + 24 + // performance.timeOrigin 25 + static jsval_t js_performance_time_origin(struct js *js, jsval_t *args, int nargs) { 26 + (void) args; (void) nargs; 27 + return js_mknum(time_origin_ms); 28 + } 29 + 30 + void init_performance_module() { 31 + struct js *js = rt->js; 32 + jsval_t global = js_glob(js); 33 + 34 + time_origin_ms = get_current_time_ms(); 35 + 36 + jsval_t perf_obj = js_mkobj(js); 37 + js_set(js, perf_obj, "now", js_mkfun(js_performance_now)); 38 + js_set(js, perf_obj, "timeOrigin", js_mknum(time_origin_ms)); 39 + 40 + js_set(js, global, "performance", perf_obj); 41 + }
+38
tests/test_performance.cjs
··· 1 + // Test performance API 2 + console.log('Testing performance API...'); 3 + 4 + // Test that performance object exists 5 + console.log('performance exists:', typeof performance === 'object'); 6 + 7 + // Test performance.now() 8 + console.log('performance.now exists:', typeof performance.now === 'function'); 9 + 10 + const t0 = performance.now(); 11 + console.log('performance.now() returns number:', typeof t0 === 'number'); 12 + console.log('performance.now() >= 0:', t0 >= 0); 13 + 14 + // Test performance.timeOrigin 15 + console.log('performance.timeOrigin exists:', typeof performance.timeOrigin === 'number'); 16 + console.log('performance.timeOrigin > 0:', performance.timeOrigin > 0); 17 + 18 + // Test that now() increases over time 19 + const t1 = performance.now(); 20 + console.log('performance.now() increases:', t1 >= t0); 21 + 22 + // Test timing a simple operation 23 + const start = performance.now(); 24 + let sum = 0; 25 + for (let i = 0; i < 10000; i++) { 26 + sum += i; 27 + } 28 + const end = performance.now(); 29 + const elapsed = end - start; 30 + 31 + console.log('Timing test - elapsed ms:', elapsed); 32 + console.log('Timing test - elapsed >= 0:', elapsed >= 0); 33 + 34 + // Test that timeOrigin + now() gives current time (approximately) 35 + const currentTime = performance.timeOrigin + performance.now(); 36 + console.log('timeOrigin + now() is reasonable timestamp:', currentTime > 1700000000000); 37 + 38 + console.log('All performance tests completed!');