Advertisement
Guest User

Profiler filter for Mongrel2

a guest
Dec 19th, 2011
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.91 KB | None | 0 0
  1. #include <filter.h>
  2. #include <dbg.h>
  3.  
  4. #include <sys/time.h>
  5.  
  6. typedef struct ProfilerSession {
  7.     struct timeval * profilerTimeStart;
  8.     struct timeval * profilerTimeEnd;
  9. } ProfilerSession;
  10.  
  11. ProfilerSession *session = NULL;
  12.  
  13. StateEvent filter_transition(StateEvent state, Connection *conn, tns_value_t *config) {
  14.     if (session == NULL) {
  15.         session = (ProfilerSession *)malloc(sizeof(ProfilerSession));
  16.         session->profilerTimeStart = malloc(sizeof(struct timeval));
  17.         session->profilerTimeEnd = malloc(sizeof(struct timeval));
  18.     }
  19.    
  20.     if (state == REQ_RECV) {
  21.         debug("[Profiler:OPEN] Connection opened. Sampling time with gettimeofday.");
  22.         int rc = gettimeofday(session->profilerTimeStart, NULL);
  23.         check(rc == 0, "Something went REALLY wrong with Profiler filter");
  24.         struct timeval startTime = *(session->profilerTimeStart);
  25.         debug("[Profiler:OPEN] Start of request %ldms", startTime.tv_sec * 10000 + startTime.tv_usec);
  26.     } else if (state == RESP_SENT) {
  27.         gettimeofday(session->profilerTimeEnd, NULL);
  28.         struct timeval endTime = *(session->profilerTimeEnd);
  29.         debug("[Profiler:CLOSE] End of request at %ldms", endTime.tv_sec * 10000 + endTime.tv_usec);
  30.  
  31.         struct timeval startTime = *(session->profilerTimeStart);
  32.         long int diff = (endTime.tv_sec * 10000 + endTime.tv_usec) - (startTime.tv_sec * 10000 + startTime.tv_usec);
  33.         debug("[Profiler:CLOSE] Request took %ldms.", diff);
  34.     }
  35.     return state;
  36.    
  37. error:
  38.     return CLOSE;
  39. }
  40.  
  41. StateEvent *filter_init(Server *srv, bstring load_path, int *out_nstates) {
  42.     StateEvent states[] = {REQ_RECV, RESP_SENT};
  43.     *out_nstates = Filter_states_length(states);
  44.     check(*out_nstates == 2, "Wrong states array length.");
  45.    
  46.     debug("[Profiler:filter_init] initted filter with needed states:");
  47.     size_t i;
  48.     for (i = 0; i < *out_nstates; i++) {
  49.         debug("[Profiler:filter_init] state: %d", states[i]);
  50.     }
  51.  
  52.     return Filter_state_list(states, *out_nstates);
  53.  
  54. error:
  55.     return NULL;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement