What I want to do is to record the number of users in the system, so called active users, along the elapsed time of the test run.

The best solution is to declare a two dimension global array say, counter[time][counter], increase the counter when a VU login and decrease the counter when a VU logout. Then flush the data from that array to a file after test run. But VU can't take array as shared variable, so I am thinking about using only a shared int counter, and write the data directly to a file at runtime.

We will run this kind of scenario, Login --> do all transactions --> Logout, a few times in a test run for each VU.

I can do the following:
Declare shared int counter; in Login script
counter++; after login and print it to a file with timestamp
counter--; after logout and print it to the file with timestamp

But when shall I open the file and close the file? Since the Login and Logout script are called so many time, maybe a few thousand times during the test run, if I open the file in Login and close it in Logout, the file will be opened thousands of time and closed throusands of times, right? What I want is just to open once and close once. How can I do that?

Is there any other better way to record the active users?

As I understand, the VU scripts are actually executed in a multiple processes manner, but where is the main()?

Best regards
Jirong Hu