68 #ifndef _Y_BROWSER_EMULATION_C_ 69 #define _Y_BROWSER_EMULATION_C_ 104 #define MAX_BROWSER_LIST_LENGTH 1000 126 lr_error_message(
"y_browser_emulation.c: Attempt to log content of NULL browser. Ignoring.");
130 lr_log_message(
"y_browseremulation.c: browser: %s, chance %d, max_conn_per_host %d, max_conn %d, user agent string: %s",
133 browser->max_connections_per_host,
134 browser->max_connections,
135 browser->user_agent_string);
158 lr_error_message(
"y_browser_emulation.c: Attempt to store the content of NULL browser into parameters. Aborting.");
163 lr_save_string(browser->name,
"browser_name");
164 lr_save_int(browser->chance,
"browser_chance");
165 lr_save_int(browser->max_connections_per_host,
"browser_max_connections_per_host");
166 lr_save_int(browser->max_connections,
"browser_max_connections");
167 lr_save_string(browser->user_agent_string,
"browser_user_agent_string");
211 const char* browser_chance_param,
212 const char* browser_max_connections_per_host_param,
213 const char* browser_max_connections_param,
214 const char* browser_user_agent_string_param)
217 y_browser* previous_browser = NULL;
224 if( browser_name == NULL )
226 lr_error_message(
"Browser name parameter %s does not exist. Aborting browser emulation setup.", browser_name_param);
229 if(
strcmp(browser_name,
"END") == 0)
232 lr_log_message(
"y_browseremulation.c: End of browser list initialisation");
237 browser = (y_browser*)
y_mem_alloc(
sizeof browser[0] );
238 browser->name = browser_name;
239 browser->next = NULL;
249 if( browser_chance == NULL || browser_max_connections_per_host == NULL || browser_max_connections == NULL )
251 lr_error_message(
"Browser parameter missing. Aborting browser emulation setup. chance: %s:%s, max_connections_per_host %s:%s, max_connections %s:%s",
252 browser_chance_param, browser_chance,
253 browser_max_connections_per_host_param, browser_max_connections_per_host,
254 browser_max_connections_param, browser_max_connections);
259 lr_error_message(
"Browser user agent parameter %s does not exist. Aborting browser emulation setup.", browser_user_agent_string_param);
269 browser->chance =
atoi(browser_chance);
270 browser->max_connections_per_host =
atoi(browser_max_connections_per_host);
271 browser->max_connections =
atoi(browser_max_connections);
288 previous_browser->next = browser;
290 previous_browser = browser;
294 lr_advance_param((
char* )browser_name_param);
297 if( i == (MAX_BROWSER_LIST_LENGTH) )
299 lr_log_message(
"Too many browsers to fit in browser list struct, max list size = %d", MAX_BROWSER_LIST_LENGTH);
373 y_browser* previous_browser = NULL;
374 long fp =
fopen(filename,
"r");
379 lr_error_message(
"Unable to open file %s", filename);
383 lr_log_message(
"Opened file %s", filename);
390 while(
fgets(line,
sizeof line, fp))
393 lr_log_message(
"Read line: %s", line);
396 while( ((
remove =
strchr(line,
'#')) != NULL) ||
398 ((
remove =
strchr(line,
'\r')) != NULL) ||
399 ((
remove =
strchr(line,
'\n')) != NULL) )
409 size_t user_agent_offset;
411 y_browser* browser = (y_browser*)
y_mem_alloc(
sizeof browser[0] );
412 int scanresult =
sscanf(line,
"%4095s %*f%% %d %d %d %n",
413 name, &browser->chance, &browser->max_connections, &browser->max_connections_per_host, &user_agent_offset);
434 lr_log_message(
"Non-matching line.");
440 browser->user_agent_string =
y_strdup(line + user_agent_offset);
453 previous_browser->next = browser;
454 previous_browser = browser;
486 int i, lowerbound, cursor = 0;
487 y_browser* browser = NULL;
490 if( browser_list_chance_total < 1 )
492 lr_error_message(
"y_browseremulation.c: Browser list not initialised before call to y_choose_browser_from_list(). Cannot choose, ignoring.");
513 roll =
y_rand() % browser_list_chance_total;
517 roll = roll * ((browser_list_chance_total /
RAND_MAX));
521 for( browser = browser_list_head; browser != NULL; browser = browser->next)
523 cursor += browser->chance;
534 lr_error_message(
"y_browseremulation.c: Roll result out of bounds: roll: %d, cursor: %d, browser_list_chance_total %d", roll, cursor, browser_list_chance_total);
585 char str_max_connections[12];
586 char str_max_connections_per_host[12];
588 static y_browser* previous_browser;
591 if( new_browser == NULL )
593 if( previous_browser == NULL )
595 lr_error_message(
"y_browser_emulation.c: Attempt to emulate the NULL browser: Ignored.");
600 browser = previous_browser;
605 browser = new_browser;
606 previous_browser = browser;
609 lr_log_message(
"Emulating browser:");
613 max_connections = browser->max_connections;
614 if( max_connections > 50 )
616 lr_log_message(
"y_browser_emulation.c: Loadrunner does not support using more than 50 browser connections. Using 50 connections instead of %d.", max_connections);
617 max_connections = 50;
620 snprintf(str_max_connections,
sizeof str_max_connections,
"%d", max_connections);
621 snprintf(str_max_connections_per_host,
sizeof str_max_connections_per_host,
"%d", browser->max_connections_per_host);
624 web_set_sockets_option(
"MAX_CONNECTIONS_PER_HOST", str_max_connections_per_host);
625 web_set_sockets_option(
"MAX_TOTAL_CONNECTIONS", str_max_connections);
626 web_add_auto_header(
"User-Agent", browser->user_agent_string);
632 #endif // _Y_BROWSER_EMULATION_C_ y_browser * y_choose_browser()
Choose a browser profile from the browser list at random using the defined weights.
Y-lib string function library.
char * user_agent_string
The browser's user agent string.
char * name
The name of the browser, as you wish to refer to it.
void y_save_browser_to_parameters(const y_browser *browser)
Save the contents of a browser struct as a series of "browser_*" loadrunner parameters.
int y_browser_list_chance_total
The total of all the browser weights added together.
int chance
The odds that said browser will be chosen. This is actually a weight, meaning you could use anything ...
char * y_mem_alloc(size_t size)
Ylib wrapper for malloc()
long fopen(const char *filename, const char *access_mode)
Documented at http://www.cplusplus.com/reference/cstdio/fopen/.
int max_connections_per_host
The maximum number of connections per host to emulate for this browser.
Contains core ylib support functions needed for the functioning of the library.
void * next
Pointer to the next element in the single-linked list of browsers.
int atoi(const char *string)
Documented at http://www.cplusplus.com/reference/cstdlib/atoi/.
int strcmp(const char *string1, const char *string2)
Documented at http://www.cplusplus.com/reference/cstring/strcmp/.
#define Y_RAND_MAX
Alternate RAND_MAX constant for use with y_rand.
y_browser * y_choose_browser_from_list(y_browser *browser_list_head, int browser_list_chance_total)
Choose a browser profile from a browser list at random using the defined weights in that list...
char * fgets(char *string, int maxchar, long file_pointer)
Documented at http://www.cplusplus.com/reference/cstdio/fgets/.
int y_emulate_browser(y_browser *new_browser)
Emulate a specific browser.
void y_log_browser(const y_browser *browser)
Log the content of a browser object.
int y_setup_browser_emulation_from_parameters(const char *browser_name_param, const char *browser_chance_param, const char *browser_max_connections_per_host_param, const char *browser_max_connections_param, const char *browser_user_agent_string_param)
Initialize the browser list based on a set of prepared parameters.
int sscanf(const char *buffer, const char *format_string,...)
Documented at http://www.cplusplus.com/reference/cstdio/sscanf/.
y_browser * y_browser_list_head
The first element in the browser list.
char * y_strdup(char *source)
Copy a string into a malloc'd piece of memory using strdup(), and lr_abort() if the allocation fails...
#define MAX_BROWSER_LIST_LENGTH
Limit the browser list to 1000 browsers, as a failsafe in case of wrong parameter settings (which may...
char * strchr(const char *string, int c)
Documented at http://www.cplusplus.com/reference/cstring/strchr/.
char * y_get_parameter_with_malloc_or_null(const char *src_param)
Get the content of a parameter and return it as a char * (malloc version)
int snprintf(char *buffer, size_t n, const char *format_string,...)
Documented at http://www.cplusplus.com/reference/cstdio/snprintf/. This function was introduced by t...
Internal browser structure, describing a browser as an entity.
long y_rand(void)
Generate a random (integer) number between 0 and Y_RAND_MAX (30 bit maxint).
int max_connections
The maximum number of connections to emulate for this browser.
int y_setup_browser_emulation()
Initialize the browser list, using the default values for the parameter names:
int y_setup_browser_emulation_from_file(char *filename)
Initialize the browser list based on a tab-seperated CSV file.
char * y_get_parameter_or_null(const char *param_name)
Get the content of a parameter and return it as a char *, or return NULL if it wasn't set...
#define RAND_MAX
RAND_MAX constant for use with rand() - 15 bits integer.
void free(void *mem_address)
Documented at http://www.cplusplus.com/reference/cstdlib/free/.