Advertisement
Guest User

bench.c

a guest
Jan 19th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5. #include <assert.h>
  6. #include <time.h>
  7.  
  8. #include <lua.h>
  9. #include <lauxlib.h>
  10. #include <lualib.h>
  11.  
  12. double
  13. gettime(void)
  14. {
  15.     struct timespec ts;
  16.     if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
  17.         perror("clock_getres");
  18.         exit(EXIT_FAILURE);
  19.     }
  20.     return ts.tv_sec + 1e-9 * ts.tv_nsec;
  21. }
  22.  
  23. void
  24. bench_forks(int nspawns, char *const *args)
  25. {
  26.     double timer = gettime();
  27.     for (int i = 0; i < nspawns; ++i) {
  28.         pid_t pid = fork();
  29.         if (pid < 0) {
  30.             perror("fork");
  31.             exit(EXIT_FAILURE);
  32.         } else if (pid == 0) {
  33.             execvp(args[0], args);
  34.             perror("execvp");
  35.             exit(127);
  36.         } else {
  37.             int status;
  38.             if (waitpid(pid, &status, 0) < 0) {
  39.                 perror("waitpid");
  40.                 exit(EXIT_FAILURE);
  41.             }
  42.             if (!WIFEXITED(status)) {
  43.                 fprintf(stderr, "child process terminated in an abnormal way\n");
  44.                 exit(EXIT_FAILURE);
  45.             }
  46.             if (WEXITSTATUS(status) != 0) {
  47.                 fprintf(stderr, "child process exited with non-zero exit code %d\n",
  48.                         WEXITSTATUS(status));
  49.                 exit(EXIT_FAILURE);
  50.             }
  51.         }
  52.     }
  53.     timer = gettime() - timer;
  54.     printf("%d spawns in %.2lf seconds: %.2lf forks per second\n", nspawns, timer, nspawns / timer);
  55. }
  56.  
  57. void
  58. check_lua_call(lua_State *L, int retval)
  59. {
  60.     if (retval == 0) {
  61.         return;
  62.     }
  63.     const char *msg = lua_tostring(L, -1);
  64.     fprintf(stderr, "Lua error: %s\n", msg ? msg : "(error object can't be converted to string)");
  65.     exit(EXIT_FAILURE);
  66. }
  67.  
  68. void
  69. bench_lua(int ncalls, const char *filename, const char *func, const char *arg)
  70. {
  71.     lua_State *L = luaL_newstate();
  72.     assert(L); // L: -
  73.     luaL_openlibs(L); // L: -
  74.     check_lua_call(L, luaL_dofile(L, filename)); // L: -
  75.  
  76. #if LUA_VERSION_NUM >= 502
  77.     lua_pushglobaltable(L);
  78. #else
  79.     lua_pushvalue(L, LUA_GLOBALSINDEX);
  80. #endif
  81.     // L: _G
  82.  
  83.     double timer = gettime();
  84.  
  85.     lua_getfield(L, -1, func); // L: _G func
  86.     int ref = luaL_ref(L, LUA_REGISTRYINDEX); // L: _G
  87.     lua_pop(L, 1); // L: -
  88.  
  89.     for (int i = 0; i < ncalls; ++i) {
  90.         lua_rawgeti(L, LUA_REGISTRYINDEX, ref); // L: func
  91.         lua_pushstring(L, arg); // L: func arg
  92.         check_lua_call(L, lua_pcall(L, 1, 0, 0)); // L: -
  93.     }
  94.  
  95.     timer = gettime() - timer;
  96.     printf("%d calls in %.2lf seconds: %.2lf calls per second\n", ncalls, timer, ncalls / timer);
  97.  
  98.     lua_close(L);
  99. }
  100.  
  101. int
  102. main()
  103. {
  104.     bench_forks(1000, (char *const[]) {"./my_true", NULL});
  105.     bench_lua(1000, "./file.lua", "my_func", "hello");
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement