• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

TestDNS.cc

Go to the documentation of this file.
00001 /** @file
00002 
00003   A brief file description
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 /****************************************************************************
00025 
00026   TestDns.cc
00027 
00028   Test module to test the DNS processor
00029 
00030   The main state machine is encapulated in the StateMachine instance.
00031 
00032 
00033  ****************************************************************************/
00034 
00035 #include "DNS.h"
00036 #include <iostream.h>
00037 #include <fstream.h>
00038 #include <string.h>
00039 #include "VConnection.h"
00040 #include "stdio.h"
00041 #include "libts.h"
00042 #define N_STATE_MACHINES 1000
00043 #define MEASUREMENT_INTERVAL 100
00044 class TestDnsStateMachine;
00045 
00046 //////////////////////////////////////////////////////////////////////////////
00047 //
00048 //      Globals
00049 //
00050 //////////////////////////////////////////////////////////////////////////////
00051 
00052 unsigned g_host_ip = 0;
00053 char *in_file_name = "test_dns.in";
00054 char *out_file_name = "test_dns.out";
00055 char *rate_file_name = "rates.out";
00056 char *rate_misc_file_name = "rates.misc.out";
00057 ofstream fout;
00058 ofstream fout_rate, fout_rate_misc;
00059 FILE *fin;
00060 
00061 
00062 //////////////////////////////////////////////////////////////////////////////
00063 //
00064 //      TestDnsStateMachine
00065 //
00066 //  An instance of TestDnsStateMachine is created for each host
00067 //
00068 //////////////////////////////////////////////////////////////////////////////
00069 class TestDnsStateMachine:public Continuation
00070 {
00071 public:
00072   TestDnsStateMachine(char *ahost, size_t size);
00073    ~TestDnsStateMachine()
00074   {
00075 //        cout << "StateMachine::~StateMachine(). Terminating ... " << endl;
00076     return;
00077   }
00078 
00079   const char *currentStateName();
00080 
00081   int processEvent(int event, void *data);
00082 
00083   enum
00084   { START, DNS_LOOKUP };
00085 
00086   int m_state;
00087   char host[100];
00088 };
00089 
00090 TestDnsStateMachine::TestDnsStateMachine(char *ahost, size_t size)
00091   :
00092 Continuation(new_ProxyMutex())
00093 {
00094   ink_strlcpy(host, ahost, size);
00095   m_state = START;
00096   SET_HANDLER(processEvent);
00097   return;
00098 }
00099 
00100 
00101 inline const char *
00102 TestDnsStateMachine::currentStateName()
00103 {
00104   switch (m_state) {
00105   case START:
00106     return ("START");
00107   case DNS_LOOKUP:
00108     return ("DNS_LOOKUP");
00109   default:
00110     return ("unknown state");
00111   }
00112 }
00113 
00114 //////////////////////////////////////////////////////////////////////////////
00115 //
00116 //      TestDnsStateMachine::processEvent()
00117 //
00118 //      This routine is the main callback entry point of the TestTunnel
00119 //      state machine.
00120 //
00121 //////////////////////////////////////////////////////////////////////////////
00122 
00123 int
00124 TestDnsStateMachine::processEvent(int event, void *data)
00125 {
00126   int rvalue = VC_EVENT_DONE;
00127   void complete();
00128 
00129 //    printf("<<< state machine processEvent() called in state '%s' >>>\n",currentStateName());
00130 
00131   switch (m_state) {
00132   case START:
00133     {
00134 //        cout << "  started up TestDnsStateMachine" << endl;
00135 //        cout << "  dns lookup for <" << host << ">" <<  endl;
00136 
00137       //
00138       // asynchronously do DNS, calling <this> back when done
00139       //
00140 
00141       m_state = DNS_LOOKUP;
00142       dnsProcessor.gethostbyname(this, host);
00143       break;
00144     }
00145   case DNS_LOOKUP:
00146     {
00147       ink_assert(event == DNS_EVENT_LOOKUP);
00148       if (!host)
00149         ink_assert(!"Error - host has no value\n");
00150       if (data) {
00151         HostEnt *ent = (HostEnt *) data;
00152         g_host_ip = ((struct in_addr *) ent->h_addr_list[0])->s_addr;
00153 //        cout << "  dns lookup is done <" << g_host_ip << ">" << endl;
00154 //          cout << "<" << host << "> <" << g_host_ip << ">\n";
00155         fout << "<" << host << "> <" << g_host_ip << ">\n";
00156 //        cout << "    finishing up" << endl;
00157 //        printf("*** NOTE: We Need To Somehow Free 'this' Here!  How?\n");
00158       } else {
00159         fout << "<" << host << "> <>\n";
00160       }
00161       fout.flush();
00162       rvalue = VC_EVENT_DONE;
00163       m_state = 99;             // Some Undefined state value
00164       complete();
00165       delete this;
00166       break;
00167     }
00168   default:
00169     {
00170       ink_assert(!"unexpected m_state");
00171       break;
00172     }
00173   }
00174   return (rvalue);
00175 }
00176 
00177 int state_machines_created, state_machines_finished, measurement_interval;
00178 ink_hrtime start_time, last_measurement_time;
00179 // Following function is called to measure the throughput
00180 void
00181 complete()
00182 {
00183   float throughput, cumul_throughput;
00184   ink_hrtime now;
00185   state_machines_finished++;
00186   if (!(state_machines_finished % measurement_interval)) {
00187     now = ink_get_hrtime();
00188     cumul_throughput = state_machines_finished * 1.0 * HRTIME_SECOND / (now - start_time);
00189     throughput = measurement_interval * 1.0 * HRTIME_SECOND / (now - last_measurement_time);
00190     last_measurement_time = now;
00191 //    cout << state_machines_finished << ": " <<
00192 //    "Cumul. Thrput " << cumul_throughput <<
00193 //    " per sec; Thrput for last " << measurement_interval << " requests: "
00194 //    << throughput << " per sec\n";
00195 //    cout.flush();
00196 //    fout_rate << state_machines_finished << ": " <<
00197 //    "Cumul. Thrput " << cumul_throughput <<
00198 //    " per sec; Thrput for last " << measurement_interval << " requests: "
00199 //    << throughput << " per sec\n";
00200     fout_rate << (now - start_time) * 1.0 / HRTIME_SECOND << " " << state_machines_finished << " " << cumul_throughput
00201       << " " << throughput << "\n";
00202     fout_rate.flush();
00203   }
00204   if (state_machines_finished == state_machines_created) {
00205     now = ink_get_hrtime();
00206     fout_rate_misc << (now - start_time) * 1.0 / HRTIME_SECOND << "\n";
00207     fout_rate_misc.flush();
00208     fout.close();
00209     fout_rate.close();
00210     fout_rate_misc.close();
00211     cout << "Dns Testing Complete\n";
00212     exit(0);
00213   }
00214 //  printf("%d Hosts left \n",state_machines_unfinished);
00215 }
00216 
00217 //////////////////////////////////////////////////////////////////////////////
00218 //
00219 //      test
00220 //
00221 //  Main entry point for DNS tests
00222 //
00223 //////////////////////////////////////////////////////////////////////////////
00224 
00225 void
00226 test()
00227 {
00228   char host[100];
00229   ink_hrtime now;
00230   int i;
00231   TestDnsStateMachine *test_dns_state_machine;
00232   printf("removing file '%s'\n", out_file_name);
00233   unlink(out_file_name);
00234   printf("removing file '%s'\n", rate_file_name);
00235   unlink(rate_file_name);
00236   printf("removing file '%s'\n", rate_misc_file_name);
00237   unlink(rate_misc_file_name);
00238   fin = fopen(in_file_name, "r");       // STDIO OK
00239   fout.open(out_file_name);
00240   fout_rate.open(rate_file_name);
00241   fout_rate_misc.open(rate_misc_file_name);
00242   i = 0;
00243   state_machines_created = N_STATE_MACHINES;
00244   state_machines_finished = 0;
00245   measurement_interval = MEASUREMENT_INTERVAL;
00246   start_time = ink_get_hrtime();
00247   last_measurement_time = ink_get_hrtime();
00248   while ((fscanf(fin, "%s", host) != EOF) && (i < state_machines_created)) {
00249     test_dns_state_machine = new TestDnsStateMachine(host, sizeof(host));
00250     test_dns_state_machine->handleEvent();
00251     i++;
00252   }
00253   now = ink_get_hrtime();
00254   cout << "Finished creating all Continuations at " <<
00255     (now - start_time) / HRTIME_SECOND << " sec and " << (now - start_time) % HRTIME_SECOND << "nanosec\n";
00256   fout_rate_misc << (now - start_time) * 1.0 / HRTIME_SECOND << "\n";
00257   fout_rate_misc.flush();
00258 }

Generated by  doxygen 1.7.1