void Main ()
{
int i;
Char ustr[64];
Srand ((unsigned) time (NULL));//preferably in Vuser_init
for(i = 0;i < 10;i + +) {
Getuniquestring (I,USTR);
Lr_output_message (USTR);
}
}
void getuniquestring (int invalue, char *outstr)
{
int ID, SCID;
Char *vuser_group;
Lr_whoami (&id, &vuser_group, &scid);
Web_save_timestamp_param ("Tstamp", last);
sprintf (outstr, "%s%05d%010d%04d", Lr_eval_string ("{tstamp}"), Id,rand (), invalue);
Free (vuser_group);
}
It is recommended that the random seed (srand (unsigned) time (NULL)) be placed in the script initialization function and only initialized once. If placed in a sub-function, each call is initialized, the resulting random number may be the same. is not a safe code.
This unique number has four levels: Millisecond time + Virtual user id+ random number + incoming parameters; There is basically no repetition in the same controller. A little more processing can get the idea of things, such as: LoadRunner implementation: The calculation string MD5 encrypted into MD5 string, and then modified to become a GUID.
Here's a security question. It is worth explaining that random seeds are best initialized only once, and the algorithm of random numbers has a certain relationship with time. If you put a random seed in a sub-function, you will find that the random number generated is the same.
Don't be surprised, why is it a security problem, say serious, the random number is an algorithm, it is possible to be robbed by others and calculated the next random value, it is not safe.
The second method:
Previously there was a description of the parameter unique number feature for LoadRunner, due to some problems and poor flexibility. There are also method descriptions for using scripts to provide a unique number for LoadRunner. The two methods are different but not perfect, now need to get a batch of unique numbers, the middle do not have breakpoints. The 1th method described above can be implemented, but must be done at once, the biggest problem is the data will be reused after the exit. I want to use PHP to implement a simple, the results found that PHP is not good at doing this. To understand it, it's very easy to implement it in Java. From a servlet, give a global variableLockingCan. This lock feature in Java is key here, and the code looks like this: Package org. Higkoo. Test Import java.io.IOException; Import Java.io.PrintWriter; Import javax.servlet.ServletException; Import Javax.servlet.http.HttpServlet; Import Javax.servlet.http.HttpServletRequest; Import Javax.servlet.http.HttpServletResponse; Public classDemoextendsHttpServlet { Private Static Final LongSerialversionuid = 1L; Public StaticObject onlyyou =NewObject (); Public Static intid = 0; Public voidDoget (HttpServletRequest request, httpservletresponse response) throwsServletexception, IOException { Response. setContentType ("text/html; Charset=utf-8"); Response. SetHeader ("Cache-control", "No-cache"); PrintWriter out = response. Getwriter (); Out. println (get_id ()); Out. Close (); } Public voidDoPost (HttpServletRequest request, httpservletresponse response) throwsServletexception, IOException { id = Integer. parseint (Request. GetParameter ("id")); } Public Static intget_id () { synchronized(onlyyou) { returnID + +; } } } Get an ID directly using GET, and use the Post method to assign a value to the ID. The way to get this ID in LoadRunner doesn't have to be introduced, it's a good thing. |