"Fossies" - the Fresh Open Source Software Archive

Member "httperf-0.9.0/src/stat/sess_stat.c" (7 Apr 2007, 7431 Bytes) of package /linux/www/old/httperf-0.9.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "sess_stat.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2     httperf -- a tool for measuring web server performance
    3     Copyright 2000-2007 Hewlett-Packard Company and Contributors listed in
    4     AUTHORS file. Originally contributed by David Mosberger-Tang
    5 
    6     This file is part of httperf, a web server performance measurment
    7     tool.
    8 
    9     This program is free software; you can redistribute it and/or
   10     modify it under the terms of the GNU General Public License as
   11     published by the Free Software Foundation; either version 2 of the
   12     License, or (at your option) any later version.
   13     
   14     In addition, as a special exception, the copyright holders give
   15     permission to link the code of this work with the OpenSSL project's
   16     "OpenSSL" library (or with modified versions of it that use the same
   17     license as the "OpenSSL" library), and distribute linked combinations
   18     including the two.  You must obey the GNU General Public License in
   19     all respects for all of the code used other than "OpenSSL".  If you
   20     modify this file, you may extend this exception to your version of the
   21     file, but you are not obligated to do so.  If you do not wish to do
   22     so, delete this exception statement from your version.
   23 
   24     This program is distributed in the hope that it will be useful,
   25     but WITHOUT ANY WARRANTY; without even the implied warranty of
   26     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   27     General Public License for more details.
   28 
   29     You should have received a copy of the GNU General Public License
   30     along with this program; if not, write to the Free Software
   31     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
   32     02110-1301, USA
   33 */
   34 
   35 /* Session statistics collector.  */
   36 
   37 #include <assert.h>
   38 #include <float.h>
   39 #include <stdio.h>
   40 #include <stdlib.h>
   41 #include <string.h>
   42 
   43 #include <httperf.h>
   44 #include <call.h>
   45 #include <event.h>
   46 #include <session.h>
   47 #include <stats.h>
   48 
   49 static struct
   50   {
   51     u_int num_rate_samples;
   52     u_int num_completed_since_last_sample;
   53     Time rate_sum;
   54     Time rate_sum2;
   55     Time rate_min;
   56     Time rate_max;
   57 
   58     u_int num_completed;
   59     Time lifetime_sum;
   60 
   61     u_int num_failed;
   62     Time failtime_sum;
   63 
   64     u_int num_conns;    /* total # of connections on successful sessions */
   65 
   66     /* session-length histogram: */
   67     u_int longest_session;
   68     u_int len_hist_alloced;
   69     u_int *len_hist;
   70   }
   71 st;
   72 
   73 #define SESS_PRIVATE_DATA(c)                        \
   74   ((Sess_Private_Data *) ((char *)(c) + sess_private_data_offset))
   75 
   76 typedef struct Sess_Private_Data
   77   {
   78     u_int num_calls_completed;  /* how many calls completed? */
   79     u_int num_conns;        /* # of connections on this session */
   80     Time birth_time;        /* when this session got created */
   81   }
   82 Sess_Private_Data;
   83 
   84 static size_t sess_private_data_offset = -1;
   85 
   86 
   87 static void
   88 perf_sample (Event_Type et, Object *obj, Any_Type reg_arg, Any_Type call_arg)
   89 {
   90   Time weight = call_arg.d;
   91   double rate;
   92 
   93   assert (et == EV_PERF_SAMPLE);
   94 
   95   rate = weight*st.num_completed_since_last_sample;
   96   st.num_completed_since_last_sample = 0;
   97 
   98   if (verbose)
   99     printf ("session-rate = %-8.1f\n", rate);
  100 
  101   ++st.num_rate_samples;
  102   st.rate_sum += rate;
  103   st.rate_sum2 += SQUARE (rate);
  104   if (rate < st.rate_min)
  105     st.rate_min = rate;
  106   if (rate > st.rate_max)
  107     st.rate_max = rate;
  108 }
  109 
  110 static void
  111 sess_created (Event_Type et, Object *obj, Any_Type regarg, Any_Type callarg)
  112 {
  113   Sess_Private_Data *priv;
  114   Sess *sess;
  115 
  116   assert (et == EV_SESS_NEW && object_is_sess (obj));
  117   sess = (Sess *) obj;
  118   priv = SESS_PRIVATE_DATA (sess);
  119   priv->birth_time = timer_now ();
  120 }
  121 
  122 static void
  123 sess_destroyed (Event_Type et, Object *obj, Any_Type regarg, Any_Type callarg)
  124 {
  125   size_t old_size, new_size;
  126   Sess_Private_Data *priv;
  127   Sess *sess;
  128   Time delta, now = timer_now ();
  129 
  130   assert (et == EV_SESS_DESTROYED && object_is_sess (obj));
  131   sess = (Sess *) obj;
  132   priv = SESS_PRIVATE_DATA (sess);
  133 
  134   delta = (now - priv->birth_time);
  135   if (sess->failed)
  136     {
  137       ++st.num_failed;
  138       st.failtime_sum += delta;
  139     }
  140   else
  141     {
  142       st.num_conns += priv->num_conns;
  143       ++st.num_completed_since_last_sample;
  144       ++st.num_completed;
  145       st.lifetime_sum += delta;
  146     }
  147 
  148   if (priv->num_calls_completed > st.longest_session)
  149     {
  150       st.longest_session = priv->num_calls_completed;
  151 
  152       if (st.longest_session >= st.len_hist_alloced)
  153     {
  154       old_size = st.len_hist_alloced*sizeof (st.len_hist[0]);
  155       st.len_hist_alloced = st.longest_session + 16;
  156       new_size = st.len_hist_alloced*sizeof (st.len_hist[0]);
  157 
  158       st.len_hist = realloc (st.len_hist, new_size);
  159       if (!st.len_hist)
  160         {
  161           fprintf (stderr, "%s.sess_stat: Out of memory\n", prog_name);
  162           exit (1);
  163         }
  164       memset ((char *) st.len_hist + old_size, 0, new_size - old_size);
  165     }
  166     }
  167   ++st.len_hist[priv->num_calls_completed];
  168 }
  169 
  170 static void
  171 conn_connected (Event_Type et, Object *obj, Any_Type regarg, Any_Type callarg)
  172 {
  173   Sess_Private_Data *priv;
  174   Sess *sess;
  175   Conn *conn;
  176 
  177   assert (et == EV_CONN_CONNECTED && object_is_conn (obj));
  178   conn = (Conn *) obj;
  179   sess = session_get_sess_from_conn (conn);
  180   priv = SESS_PRIVATE_DATA (sess);
  181   ++priv->num_conns;
  182 }
  183 
  184 static void
  185 call_done (Event_Type et, Object *obj, Any_Type regarg, Any_Type callarg)
  186 {
  187   Sess_Private_Data *priv;
  188   Sess *sess;
  189   Call *call;
  190 
  191   assert (et == EV_CALL_RECV_STOP && object_is_call (obj));
  192   call = (Call *) obj;
  193   sess = session_get_sess_from_call (call);
  194   priv = SESS_PRIVATE_DATA (sess);
  195   ++priv->num_calls_completed;
  196 }
  197 
  198 static void
  199 init (void)
  200 {
  201   Any_Type arg;
  202   size_t size;
  203 
  204   sess_private_data_offset = object_expand (OBJ_SESS,
  205                         sizeof (Sess_Private_Data));
  206   st.len_hist_alloced = 16;
  207   size = st.len_hist_alloced*sizeof (st.len_hist[0]);
  208   st.len_hist = malloc (size);
  209   memset (st.len_hist, 0, size);
  210 
  211   st.rate_min = DBL_MAX;
  212 
  213   if (!st.len_hist)
  214     {
  215       fprintf (stderr, "%s.sess_stat: Out of memory\n", prog_name);
  216       exit (1);
  217     }
  218   arg.l = 0;
  219   event_register_handler (EV_PERF_SAMPLE, perf_sample, arg);
  220   event_register_handler (EV_SESS_NEW, sess_created, arg);
  221   event_register_handler (EV_SESS_DESTROYED, sess_destroyed, arg);
  222 
  223   event_register_handler (EV_CONN_CONNECTED, conn_connected, arg);
  224 
  225   event_register_handler (EV_CALL_RECV_STOP, call_done, arg);
  226 }
  227 
  228 static void
  229 dump (void)
  230 {
  231   double min, avg, stddev, delta;
  232   int i;
  233 
  234   delta = test_time_stop - test_time_start;
  235 
  236   avg = 0;
  237   stddev = 0;
  238   if (delta > 0)
  239     avg = st.num_completed / delta;
  240   if (st.num_rate_samples > 1)
  241     stddev = STDDEV (st.rate_sum, st.rate_sum2, st.num_rate_samples);
  242 
  243   if (st.num_rate_samples > 0)
  244     min = st.rate_min;
  245   else
  246     min = 0.0;
  247   printf ("\nSession rate [sess/s]: min %.2f avg %.2f max %.2f "
  248       "stddev %.2f (%u/%u)\n", min, avg, st.rate_max, stddev,
  249       st.num_completed, st.num_completed + st.num_failed);
  250 
  251   printf ("Session: avg %.2f connections/session\n",
  252       st.num_completed > 0 ? st.num_conns/(double) st.num_completed : 0.0);
  253 
  254   avg = 0.0;
  255   if (st.num_completed > 0)
  256     avg = st.lifetime_sum/st.num_completed;
  257   printf ("Session lifetime [s]: %.1f\n", avg);
  258 
  259   avg = 0.0;
  260   if (st.num_failed > 0)
  261     avg = st.failtime_sum/st.num_failed;
  262   printf ("Session failtime [s]: %.1f\n", avg);
  263 
  264   printf ("Session length histogram:");
  265   for (i = 0; i <= st.longest_session; ++i)
  266     printf (" %u", st.len_hist[i]);
  267   putchar ('\n');
  268 }
  269 
  270 Stat_Collector session_stat =
  271   {
  272     "collects session-related statistics",
  273     init,
  274     no_op,
  275     no_op,
  276     dump
  277   };