Advertisement
rodMorMTL

harness

Jul 22nd, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.53 KB | None | 0 0
  1. /*
  2.  *  This script read js scripts and measure memory footprint
  3.  *  parameters: js file, main js function to execute
  4.  */
  5.  
  6. /* processlines.c */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <malloc.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "duktape.h"
  13.  
  14. /* We assume a  JS maximum file size of 17kB. */
  15. static void push_file_as_string(duk_context *ctx, const char *filename) {
  16.     FILE *f;
  17.     size_t len;
  18.     char buf[17384];
  19.  
  20.     f = fopen(filename, "rb");
  21.     if (f) {
  22.         len = fread((void *) buf, 1, sizeof(buf), f);
  23.         fclose(f);
  24.         duk_push_lstring(ctx, (const char *) buf, (duk_size_t) len);
  25.     } else {
  26.         duk_push_undefined(ctx);
  27.     }
  28. }
  29. static int
  30.        get_total_size_of_memory_occupied(void)
  31.        {
  32.            struct mallinfo mi;
  33.            mi = mallinfo();
  34.            return mi.uordblks;
  35.        }
  36.  
  37. int main(int argc, const char *argv[]) {
  38.     /* Receive 3 arguments: (0) name of C code,
  39.        (1) name of JS script to benchmark
  40.        (2) name of JS function to execute ("." = none")
  41.     */
  42.     if ( argc == 3){
  43.         duk_context *ctx = NULL;
  44.            
  45.         (void) argc; (void) argv;
  46.         /*Create a new Duktape heap and return an initial context (thread)*/
  47.         ctx = duk_create_heap_default();
  48.         if (!ctx) {
  49.             printf("Failed to create a Duktape heap.\n");
  50.             exit(1);
  51.         }
  52.         /* We push to Duktape heap the JS file*/
  53.         push_file_as_string(ctx, argv[1]);
  54.         if (duk_peval(ctx) != 0) {
  55.             printf("Error: %s\n", duk_safe_to_string(ctx, -1));
  56.             goto finished;
  57.         }
  58.         duk_pop(ctx);  /* ignore result */
  59.         /* when the js function argument is different from 0,
  60.         *  we should call the function specified by the argument.*/
  61.         if (strcmp(argv[2],".")!=0){
  62.             duk_push_global_object(ctx); //push the global object to the stack
  63.             duk_get_prop_string(ctx, -1 /*index*/, argv[2]); //push the js function to the stack
  64.         }
  65.         /* Call target function with n arguments in the stack
  66.          * The function and its arguemnts are replaced by a
  67.          * single return value or single error
  68.         */
  69.         if (duk_pcall(ctx, 0 /*nargs*/) != 0) {
  70.                 printf("Error: %s\n", duk_safe_to_string(ctx, -1));
  71.                 }
  72.         duk_pop(ctx);  /* pop result/error */
  73.    
  74.         int JS_MemoryUsed = get_total_size_of_memory_occupied();
  75.         printf("%d",JS_MemoryUsed );
  76.        
  77.         finished:
  78.         duk_destroy_heap(ctx);    
  79.     }
  80.     else {
  81.         printf("You need to provide: (1) the name of the js to run;\
  82.             (2) the js function to execute");
  83.     }
  84.     exit(0);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement