Y-lib
Loadrunner libraries
y_flow_list.c
Go to the documentation of this file.
1 /*
2  * Ylib Loadrunner function library.
3  * Copyright (C) 2005-2014 Floris Kraak <randakar@gmail.com> | <fkraak@ymor.nl>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
72 #ifndef _Y_FLOW_C_
73 #define _Y_FLOW_C_
75 
77 #include "y_core.c"
78 
83 typedef int (y_flow_func)();
84 
85 
94 {
96  int number;
98  char *name;
100  y_flow_func *function;
102  int weight;
103 };
104 
105 
112 typedef struct y_struct_flow y_flow;
113 
114 
125 int y_calc_flow_weight_total(y_flow flow_list[], int flow_count)
126 {
127  int i, total = 0;
128  for(i=0; i < flow_count; i++)
129  {
130  y_flow* flow = &flow_list[i];
131  total += flow->weight;
132  }
133  lr_log_message("y_flow: Combined total of weights is: %d", total);
134  return total;
135 }
136 
137 
147 y_flow* y_choose_flow(y_flow flow_list[], int flow_count)
148 {
149  int i, lowerbound = 0;
150  int cursor = 0;
151  int roll = y_rand() % y_calc_flow_weight_total(flow_list, flow_count);
152 
153  lr_log_message("Roll: %d", roll);
154 
155  for(i=0; i < flow_count; i++)
156  {
157  y_flow *flow = &flow_list[i];
158  int weight = flow->weight;
159  cursor += weight;
160 
161  lr_log_message("weight cursor: %d", cursor);
162  if(roll < cursor)
163  {
164  return flow;
165  }
166  }
167  return NULL;
168 }
169 
178 int y_exec_flow(y_flow *chosen_flow)
179 {
180  if(chosen_flow == NULL)
181  {
182  lr_log_message("Warning: Cannot execute NULL flow.");
183  }
184  else if(chosen_flow->name == NULL)
185  {
186  lr_log_message("Warning: Cannot execute a flow without a name!");
187  }
188  else if(chosen_flow->function == NULL)
189  {
190  lr_log_message("Warning: Cannot execute NULL flow function for flow %s", chosen_flow->name);
191  }
192  else
193  {
194  y_flow_func *flow_function = chosen_flow->function;
195  return flow_function(); // Run the flow.
196  }
197  return 0;
198 }
199 
207 y_flow* y_get_flow_by_name(char *flow_name, y_flow flow_list[], int flow_count)
208 {
209  int i;
210  lr_log_message("y_get_flow_by_name(%s)", flow_name);
211 
212  for(i=0; i < flow_count; i++)
213  {
214  y_flow *flow = &flow_list[i];
215 
216  //lr_log_message("Found name: %s", flow->name);
217  if( strcmp(flow_name, flow->name) == 0 )
218  {
219  //lr_log_message("Match: %s", flow->name);
220  return flow;
221  }
222  }
223  lr_log_message("Name not found: %s", flow_name);
224  return NULL;
225 }
226 
227 #endif // _Y_FLOW_C_
int y_exec_flow(y_flow *chosen_flow)
Execute a flow from a flow list.
Definition: y_flow_list.c:178
Descriptor for one single flow in a flow list.
Definition: y_flow_list.c:93
y_flow * y_get_flow_by_name(char *flow_name, y_flow flow_list[], int flow_count)
Fetch a specific item from a flow list, by name.
Definition: y_flow_list.c:207
Contains core ylib support functions needed for the functioning of the library.
int strcmp(const char *string1, const char *string2)
Documented at http://www.cplusplus.com/reference/cstring/strcmp/.
int( y_flow_func)()
Type definition of a function pointer to the flow to execute.
Definition: y_flow_list.c:83
int number
A number. Not used by ylib itself, use it however you want.
Definition: y_flow_list.c:96
int y_calc_flow_weight_total(y_flow flow_list[], int flow_count)
Calculate the total of the weights in a given flow list.
Definition: y_flow_list.c:125
long y_rand(void)
Generate a random (integer) number between 0 and Y_RAND_MAX (30 bit maxint).
Definition: y_core.c:156
char * name
The name of the flow.
Definition: y_flow_list.c:98
y_flow * y_choose_flow(y_flow flow_list[], int flow_count)
Choose a flow from a list of flows.
Definition: y_flow_list.c:147
int weight
The weight assigned to this piece of code, determining how likely it is to be executed.
Definition: y_flow_list.c:102