~ps10gel/ubuntu/xenial/trafficserver/6.2.0

« back to all changes in this revision

Viewing changes to test/SDKtest/client/api/RequestList.c

  • Committer: Bazaar Package Importer
  • Author(s): Arno Toell
  • Date: 2011-01-13 11:49:18 UTC
  • Revision ID: james.westby@ubuntu.com-20110113114918-vu422h8dknrgkj15
Tags: upstream-2.1.5-unstable
ImportĀ upstreamĀ versionĀ 2.1.5-unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** @file
 
2
 
 
3
  A brief file description
 
4
 
 
5
  @section license License
 
6
 
 
7
  Licensed to the Apache Software Foundation (ASF) under one
 
8
  or more contributor license agreements.  See the NOTICE file
 
9
  distributed with this work for additional information
 
10
  regarding copyright ownership.  The ASF licenses this file
 
11
  to you under the Apache License, Version 2.0 (the
 
12
  "License"); you may not use this file except in compliance
 
13
  with the License.  You may obtain a copy of the License at
 
14
 
 
15
      http://www.apache.org/licenses/LICENSE-2.0
 
16
 
 
17
  Unless required by applicable law or agreed to in writing, software
 
18
  distributed under the License is distributed on an "AS IS" BASIS,
 
19
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
20
  See the License for the specific language governing permissions and
 
21
  limitations under the License.
 
22
 */
 
23
 
 
24
/* RequestList.c
 
25
 *
 
26
 *
 
27
 * Description
 
28
 *   - Generate requests to the web servers listed in the files specified
 
29
 *     in the request lists, specified in the configuration file.
 
30
 *     Also use the ratio specified with the request lists to generate the
 
31
 *     right distribution of requests.
 
32
 *
 
33
 *
 
34
 * Added Options in SDKtest_client.config -
 
35
 *   request_lists : full path of the file[s] that contain the
 
36
 *                   request lists. Also need to specify the
 
37
 *                   request ratio. Example :
 
38
 *                   request_lists=/home/bob/list1:20,/home/bob/list2:80
 
39
 *                   Note: comma is the seperator. do not leave space or tabs inbetween
 
40
 *                   This will cause 20 % of requests to go from list1 and
 
41
 *                   80 % from list2.
 
42
 *                   Note : the ratios MUST add upto 100.
 
43
 */
 
44
 
 
45
#include "ClientAPI.h"
 
46
#include <stdio.h>
 
47
#include <string.h>
 
48
#include <stdlib.h>
 
49
#include <ctype.h>
 
50
 
 
51
#define TRUE 1
 
52
#define FALSE 0
 
53
#define MAX_URL_SIZE 256
 
54
#define MAX_LISTS 10
 
55
#define MAX_FILE_NAME_SIZE 512
 
56
#define MAX_SMALL_NAME 10
 
57
 
 
58
typedef enum
 
59
{
 
60
  ALLOWED,
 
61
  FORBIDDEN
 
62
} URL_TYPE;
 
63
 
 
64
/* helper functions */
 
65
void read_host(FILE * url, char *buffer, int buf_size);
 
66
int select_url_catagory();
 
67
 
 
68
typedef struct
 
69
{
 
70
  int header_bytes;
 
71
} User;
 
72
 
 
73
typedef struct
 
74
{
 
75
  /* for determining if we submit request to TS or not */
 
76
  int direct;
 
77
  char *target_host;
 
78
  char *target_port;
 
79
 
 
80
  /* for request lists */
 
81
  FILE *list_fp[MAX_LISTS];
 
82
  char *list_str[MAX_LISTS];
 
83
  double list_ratio[MAX_LISTS];
 
84
  int list_requests[MAX_LISTS];
 
85
  int nlist;
 
86
 
 
87
  /* for reporting data */
 
88
  long requests;
 
89
 
 
90
  long successful_documents;
 
91
  long unfinished_documents;
 
92
  long other_failed_documents;
 
93
  long total_bytes_received;
 
94
 
 
95
} RequestListPlugin;
 
96
 
 
97
RequestListPlugin my_plugin;
 
98
 
 
99
void
 
100
TSPluginInit(int client_id)
 
101
{
 
102
  my_plugin.requests = 0;
 
103
  my_plugin.successful_documents = 0;
 
104
  my_plugin.unfinished_documents = 0;
 
105
  my_plugin.other_failed_documents = 0;
 
106
  my_plugin.total_bytes_received = 0;
 
107
 
 
108
  /* setup the callbacks */
 
109
  TSFuncRegister(TS_FID_OPTIONS_PROCESS);
 
110
  TSFuncRegister(TS_FID_OPTIONS_PROCESS_FINISH);
 
111
  TSFuncRegister(TS_FID_CONNECTION_FINISH);
 
112
  TSFuncRegister(TS_FID_PLUGIN_FINISH);
 
113
  TSFuncRegister(TS_FID_REQUEST_CREATE);
 
114
  TSFuncRegister(TS_FID_HEADER_PROCESS);
 
115
  TSFuncRegister(TS_FID_PARTIAL_BODY_PROCESS);
 
116
  TSFuncRegister(TS_FID_REPORT);
 
117
}
 
118
 
 
119
 
 
120
void
 
121
TSOptionsProcess(char *option, char *value)
 
122
{
 
123
  int i;
 
124
  int rsum;
 
125
 
 
126
  if (strcmp(option, "target_host") == 0) {
 
127
    my_plugin.target_host = strdup(value);
 
128
  } else if (strcmp(option, "target_port") == 0) {
 
129
    my_plugin.target_port = strdup(value);
 
130
  } else if (strcmp(option, "request_lists") == 0) {
 
131
 
 
132
    char my_value[2046];
 
133
    strcpy(my_value, value);
 
134
 
 
135
    my_plugin.nlist = 0;
 
136
    my_plugin.list_str[my_plugin.nlist] = strtok(my_value, ",");
 
137
 
 
138
    while (my_plugin.list_str[my_plugin.nlist] != NULL) {
 
139
      my_plugin.nlist++;
 
140
      my_plugin.list_str[my_plugin.nlist] = strtok(NULL, ",");
 
141
    }
 
142
 
 
143
    rsum = 0;
 
144
    for (i = 0; i < my_plugin.nlist; i++) {
 
145
      char tmp_s[MAX_FILE_NAME_SIZE];
 
146
      char *fname;
 
147
      char *tmp;
 
148
 
 
149
      my_plugin.list_requests[i] = 0;
 
150
      strcpy(tmp_s, my_plugin.list_str[i]);
 
151
 
 
152
      fname = strtok(tmp_s, ":");
 
153
      tmp = strtok(NULL, ":");
 
154
      my_plugin.list_ratio[i] = (double) (atoi(tmp));
 
155
      rsum += my_plugin.list_ratio[i];
 
156
 
 
157
      if (!(my_plugin.list_fp[i] = fopen(fname, "r"))) {
 
158
        fprintf(stderr, "Open URL file %s failed\n", fname);
 
159
        exit(1);
 
160
      }
 
161
    }
 
162
 
 
163
    if (rsum != 100) {
 
164
      fprintf(stderr, "Sum of ratios [%d] != 100", rsum);
 
165
      exit(1);
 
166
    }
 
167
 
 
168
  }
 
169
}
 
170
 
 
171
 
 
172
void
 
173
TSOptionsProcessFinish()
 
174
{
 
175
  if ((strlen(my_plugin.target_host) == 0) || (strlen(my_plugin.target_port) == 0)) {
 
176
    my_plugin.direct = 1;
 
177
  } else {
 
178
    my_plugin.direct = 0;
 
179
  }
 
180
}
 
181
 
 
182
void
 
183
TSConnectionFinish(void *req_id, TSConnectionStatus conn_status)
 
184
{
 
185
  if (conn_status == TS_TIME_EXPIRE) {
 
186
    my_plugin.unfinished_documents++;
 
187
  }
 
188
  free(req_id);
 
189
}
 
190
 
 
191
 
 
192
void
 
193
TSPluginFinish()
 
194
{
 
195
  /* do all cleanup here */
 
196
  int i;
 
197
  free(my_plugin.target_host);
 
198
  free(my_plugin.target_port);
 
199
 
 
200
  for (i = 0; i < my_plugin.nlist; i++) {
 
201
    fclose(my_plugin.list_fp[i]);
 
202
  }
 
203
}
 
204
 
 
205
 
 
206
int
 
207
TSRequestCreate(char *origin_server_host /* return */ , int max_hostname_size,
 
208
                 char *origin_server_port /* return */ , int max_portname_size,
 
209
                 char *request_buf /* return */ , int max_request_size,
 
210
                 void **req_id /* return */ )
 
211
{
 
212
  /* char *portname = "80"; */
 
213
  char *portname;
 
214
  char *hostname = (char *) malloc(max_hostname_size + 1);
 
215
  char *tail = (char *) malloc(max_hostname_size + 1);
 
216
 
 
217
  int type = select_url_catagory();
 
218
 
 
219
  strcpy(tail, "index.html");
 
220
  if (type != -1) {
 
221
 
 
222
    char *tmp_h1 = (char *) malloc(max_hostname_size + 1);
 
223
    char *tmp_h2 = (char *) malloc(max_hostname_size + 1);
 
224
 
 
225
    my_plugin.list_requests[type]++;
 
226
    read_host(my_plugin.list_fp[type], hostname, max_hostname_size);
 
227
 
 
228
    strcpy(tmp_h1, hostname);
 
229
    tmp_h2 = strchr(tmp_h1, ':');
 
230
 
 
231
    if (tmp_h2 != NULL) {
 
232
      int lport, ltail;
 
233
      char *tmp_h3 = (char *) malloc(max_hostname_size + 1);
 
234
 
 
235
      lport = tmp_h2 - tmp_h1;
 
236
      *(hostname + lport) = '\0';
 
237
      portname = hostname + lport + 1;
 
238
 
 
239
      tmp_h3 = strchr(tmp_h1, '/');
 
240
 
 
241
      if (tmp_h3 != NULL) {
 
242
        ltail = tmp_h3 - tmp_h1;
 
243
        *(hostname + ltail) = '\0';
 
244
        tail = hostname + ltail + 1;
 
245
      }
 
246
 
 
247
    } else {
 
248
      portname = strdup("80");
 
249
    }
 
250
  } else {
 
251
    fprintf(stderr, "ERROR: unable to select url list; select_url_catagory returned -1\n");
 
252
    exit(1);
 
253
  }
 
254
 
 
255
  if (my_plugin.direct) {
 
256
    strcpy(origin_server_host, hostname);
 
257
    strcpy(origin_server_port, portname);
 
258
    sprintf(request_buf,
 
259
            "GET /%s HTTP/1.0\r\nAccept: */*\r\nHost: %s:%s\r\n\r\n", tail, origin_server_host, origin_server_port);
 
260
  } else {
 
261
    strcpy(origin_server_host, my_plugin.target_host);
 
262
    strcpy(origin_server_port, my_plugin.target_port);
 
263
    sprintf(request_buf, "GET %s:%s/%s HTTP/1.0\r\nAccept: */*\r\n\r\n", hostname, portname, tail);
 
264
  }
 
265
  *req_id = malloc(sizeof(User));
 
266
  my_plugin.requests++;
 
267
  return TRUE;
 
268
}
 
269
 
 
270
 
 
271
TSRequestAction
 
272
TSHeaderProcess(void *req_id, char *header, int length, char *request_str)
 
273
{
 
274
  ((User *) req_id)->header_bytes = length;
 
275
 
 
276
  if (strstr(header, "200 OK")) {
 
277
    return TS_KEEP_GOING;
 
278
  } else {
 
279
    my_plugin.other_failed_documents++;
 
280
    return TS_STOP_FAIL;
 
281
  }
 
282
}
 
283
 
 
284
 
 
285
TSRequestAction
 
286
TSPartialBodyProcess(void *req_id, void *partial_content, int partial_length, int accum_length)
 
287
{
 
288
  if (partial_length == 0) {
 
289
    my_plugin.successful_documents++;
 
290
    my_plugin.total_bytes_received += (accum_length + ((User *) req_id)->header_bytes);
 
291
  }
 
292
  return TS_KEEP_GOING;
 
293
}
 
294
 
 
295
 
 
296
void
 
297
TSReport()
 
298
{
 
299
 
 
300
  int i;
 
301
 
 
302
  TSReportSingleData("Total Requests", "count", TS_SUM, (double) my_plugin.requests);
 
303
  TSReportSingleData("Successful Documents", "count", TS_SUM, (double) my_plugin.successful_documents);
 
304
  TSReportSingleData("Unfinished Documents", "count", TS_SUM, (double) my_plugin.unfinished_documents);
 
305
  TSReportSingleData("Other Failed Documents", "count", TS_SUM, (double) my_plugin.other_failed_documents);
 
306
 
 
307
  for (i = 0; i < my_plugin.nlist; i++) {
 
308
    char s[MAX_FILE_NAME_SIZE + 30];
 
309
    sprintf(s, "Total Requests from file %d", i);
 
310
    TSReportSingleData(s, "count", TS_SUM, (double) my_plugin.list_requests[i]);
 
311
  }
 
312
 
 
313
  TSReportSingleData("Total Bytes Received", "count", TS_SUM, (double) my_plugin.total_bytes_received);
 
314
}
 
315
 
 
316
 
 
317
/******************** ADDED FUNCTIONS ****************************/
 
318
 
 
319
/*   Reads a host from the file pointer, url, and stores the host
 
320
 *   it read into the buffer.  If no more host is read, it starts
 
321
 *   to read the host name from the beginning of the file
 
322
 */
 
323
void
 
324
read_host(FILE * url, char *buffer, int buf_size)
 
325
{
 
326
  int c, i;
 
327
  while (1) {
 
328
    i = 0;
 
329
    while ((c = fgetc(url)) != EOF) {
 
330
      if (i < buf_size) {
 
331
        if (c == '\n' && strcmp(buffer, "")) {
 
332
          buffer[i] = '\0';
 
333
          return;
 
334
        }
 
335
        if (!isspace(c))
 
336
          buffer[i++] = c;
 
337
      }
 
338
      /* skip those host names that are longer than buf_size */
 
339
      else {
 
340
        while (((c = fgetc(url)) != EOF) && c != '\n');
 
341
        /* if hits the end, go back to the front */
 
342
        if (c == EOF) {
 
343
          if (fseek(url, 0, SEEK_SET)) {
 
344
            fprintf(stderr, "ERROR in fseek");
 
345
          }
 
346
        }
 
347
      }
 
348
    }
 
349
    /* if hits the end, go back to the front */
 
350
    if (c == EOF) {
 
351
      if (fseek(url, 0, SEEK_SET)) {
 
352
        fprintf(stderr, "ERROR in fseek");
 
353
      }
 
354
    }
 
355
  }
 
356
}
 
357
 
 
358
 
 
359
/*   Randomly choose what catagory of url we should generate
 
360
 *   based on the ratio for the file specified in SDKtest_client.config
 
361
 */
 
362
int
 
363
select_url_catagory()
 
364
{
 
365
 
 
366
  double rand;
 
367
  double x;
 
368
  int i;
 
369
 
 
370
  rand = drand48();
 
371
 
 
372
  x = 0;
 
373
  for (i = 0; i < my_plugin.nlist; i++) {
 
374
    x = x + (double) my_plugin.list_ratio[i] / 100;
 
375
    if (rand <= x) {
 
376
      return i;
 
377
    }
 
378
  }
 
379
  return -1;
 
380
}