Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This script read js scripts and measure memory footprint
- * parameters: js file, main js function to execute
- */
- /* processlines.c */
- #include <stdio.h>
- #include <string.h>
- #include <malloc.h>
- #include <stdlib.h>
- #include <string.h>
- #include "duktape.h"
- /* We assume a JS maximum file size of 17kB. */
- static void push_file_as_string(duk_context *ctx, const char *filename) {
- FILE *f;
- size_t len;
- char buf[17384];
- f = fopen(filename, "rb");
- if (f) {
- len = fread((void *) buf, 1, sizeof(buf), f);
- fclose(f);
- duk_push_lstring(ctx, (const char *) buf, (duk_size_t) len);
- } else {
- duk_push_undefined(ctx);
- }
- }
- static int
- get_total_size_of_memory_occupied(void)
- {
- struct mallinfo mi;
- mi = mallinfo();
- return mi.uordblks;
- }
- int main(int argc, const char *argv[]) {
- /* Receive 3 arguments: (0) name of C code,
- (1) name of JS script to benchmark
- (2) name of JS function to execute ("." = none")
- */
- if ( argc == 3){
- duk_context *ctx = NULL;
- (void) argc; (void) argv;
- /*Create a new Duktape heap and return an initial context (thread)*/
- ctx = duk_create_heap_default();
- if (!ctx) {
- printf("Failed to create a Duktape heap.\n");
- exit(1);
- }
- /* We push to Duktape heap the JS file*/
- push_file_as_string(ctx, argv[1]);
- if (duk_peval(ctx) != 0) {
- printf("Error: %s\n", duk_safe_to_string(ctx, -1));
- goto finished;
- }
- duk_pop(ctx); /* ignore result */
- /* when the js function argument is different from 0,
- * we should call the function specified by the argument.*/
- if (strcmp(argv[2],".")!=0){
- duk_push_global_object(ctx); //push the global object to the stack
- duk_get_prop_string(ctx, -1 /*index*/, argv[2]); //push the js function to the stack
- }
- /* Call target function with n arguments in the stack
- * The function and its arguemnts are replaced by a
- * single return value or single error
- */
- if (duk_pcall(ctx, 0 /*nargs*/) != 0) {
- printf("Error: %s\n", duk_safe_to_string(ctx, -1));
- }
- duk_pop(ctx); /* pop result/error */
- int JS_MemoryUsed = get_total_size_of_memory_occupied();
- printf("%d",JS_MemoryUsed );
- finished:
- duk_destroy_heap(ctx);
- }
- else {
- printf("You need to provide: (1) the name of the js to run;\
- (2) the js function to execute");
- }
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement