Protocol Implementation > Implement the main function |
The main function includes registration of the protocol, registration of event handlers, and the C language function interfaces.
See The CProtocolExtension Class
When LoadRunner loads a protocol, the first action is to call the protocol's RegisterProtocol
function, implemented in main.cpp
. RegisterProtocol
is called only once. In the RegisterProtocol
function, the CProtocolExtension object is created and CProtocolExtension::Register
is called.
In RegisterProtocol
, register event handlers and perform any one-time setup actions required by your protocol. LoadRunner generally runs several processes for a protocol, each with a certain number of Vusers. Do not perform initializations in RegisterProtocol
that are required independently by each process. Initializations required at a lower scope are performed by the event handlers. For example, implement initializations for each process (such as global variable values) in the PROCESS_INIT_EVENT handler and implement initializations for a new Vuser and for any thread required by your implementation in the THREAD_INIT_EVENT
.
The return value of RegisterProtocol
is the pointer to the protocol configuration returned by CProtocolExtension::Register
.
Registration in main.cpp
Copy Code
|
|
---|---|
#include <ProtocolExtension.h> #include "RequestHeaders.h" #include "TWEB_res.h" /// register the protocol in the LR execution environment at the DLL loading; do not change the function name extern "C" __declspec(dllexport) ext_out_config * RegisterProtocol(void * EXTID, ext_in_config * drv_data) { CProtocolExtension* extension = CProtocolExtension::Instance(); // register thread start/stop handlers extension->RegisterEventHandler(THREAD_INIT_EVENT, ThreadInitEventHandler); extension->RegisterEventHandler(THREAD_TERMINATE_EVENT, ThreadTerminatetEventHandler); // register the protocol return extension->Register("TWEB", EXTID, drv_data); } |
Events that can be trapped
Event | Description |
---|---|
PROCESS_INIT_EVENT | Process starting |
PROCESS_TERMINATE_EVENT | Normal process end |
THREAD_INIT_EVENT | Thread starting |
THREAD_TERMINATE_EVENT | Normal thread end |
ABORT_EVENT | Terminated on unrecoverable error |
ACTION_START_EVENT | Started an action (Any action, not just the default "Action") |
ACTION_END_EVENT | Normal action end |
ITERATION_START_EVENT | Normal iteration start |
ITERATION_END_EVENT | Normal iteration end |
Timers and Vuser data in main.cpp
Copy Code
|
|
---|---|
#include <ProtocolExtension.h> #include "RequestHeaders.h" #include "TWEB_res.h" // // Event handlers. // The event handlers are called by the LoadRunner execution environment. // /// sample callback to demonstrate timer feature int SampleTimerCallback(void* context) { // Output to the log CProtocolExtension::Instance()->LogWarningMessage(TWEB_TIMER); return 0; } /// thread start handler; here we should initialize Virtual User data int ThreadInitEventHandler(void *) { // create an object of the virtual user data class. RequestHeaders* myData = new RequestHeaders(); CProtocolExtension::Instance()->SetVirtualUserData(myData); CProtocolExtension::Instance()->RegisterTimer(SampleTimerCallback, 0, 500); return EVENT_HANDLER_PASSED; } /// thread termination handler /// deallocate per-thread global resources such as Virtual User data int ThreadTerminatetEventHandler(void *) { // delete the object of the virtual user data class. RequestHeaders* myData = reinterpret_cast<RequestHeaders*>(CProtocolExtension::Instance()->GetVirtualUserData()); delete myData; return EVENT_HANDLER_PASSED; } |