Ejemplo n.º 1
0
//==========================================================================================================
// Get the value for a variable that appear in the script in brackets (like [call_id]). Some are static to
// the test (received from MServer), some generated, some stored from previous messages.
//==========================================================================================================
string ScriptReader::get_value(string var, int call_number, bool try_as_last)
{
    smatch match;
    
    if(!regex_match(var, match, var_regex))
    {
        throw string("Wrong format for var in ScriptReader::get_value(): " + var);
    }
    
    // var_regex:   "(last_)?(((cseq)\\+(\\d+))|([-\\w]+))(:value)?"
    // submatches:   1       234        5       6         7
    bool is_last = (match[1].length() > 0);
    bool just_value = (match[7].length() >  0);
    int add_to_cseq = 0;
    string name = match[2];

    if(match[3].length() > 0)
    {
        name = match[4];
        add_to_cseq = stoi(match[5]);
    }
    
    if(is_last || (try_as_last && (SipMessage::is_message_var(name) || SipParser::inst().match(HEADER_NAME, name))))
    {
        string result = get_last_value(name, call_number, just_value);
        
        if(name == CSEQ)
        {
            return to_string(stoi(result) + add_to_cseq);
        }
        
        return result;
    }
    
    if(name == BRANCH)
    {
        return gen_branch();
    }
    
    CallIDKind cid_kind = string_to_call_id_kind(name);
    
    if(cid_kind != NONE)
    {
        return gen_call_id(cid_kind);
    }
    
    if(name == TAG)
    {
        return gen_tag();
    }
    
    if(vars.count(name) != 0)
    {
        return vars[name];
    }
    
    if(static_vars.count(name) != 0)
    {
        return static_vars.at(name);
    }
    
    return MServer::inst.get_value(name);
}
long test3(const char* timeLog_filepath, int num_of_clients, int ksm )
{
    char shell_command[BUFSIZ];
    char cache_filepath[1024];
    char client_name[1024];
    char group_name[1024];
    int i,j,k;
    long total_time = 0.0L;
    FILE* timeLog_file = NULL;
    char* base_client_name = 
              (ksm==2)? KSM2_BASE_CLIENT_NAME:KSM1_BASE_CLIENT_NAME;

    // Create client principals and its cache for each client
    for( i = 0; i < num_of_clients; i++ ) {
        //printf("\rCreating client principal and ticket cache %d of %d",
        //     i+1, num_of_clients);
        snprintf(client_name, 1024, "%s%d", base_client_name, i);
        create_client_princ( client_name );
        create_client_cache( client_name );
    }
    //printf("\n");

    // Execute all client request sequentially
    for( i = 1; i <= group_count; i++ ) {        
        memset(group_name, 0, 1024);
        snprintf(group_name, 1024, "group%d", i);

        int a[num_of_clients], nvalues = num_of_clients;
        
        for( k = 0; k < nvalues; k++ ) a[k] = k; 
        for( k = 0; k < nvalues -1; k++) {
            int c = rand() / (RAND_MAX/(nvalues-k) + 1 );
            int t = a[k]; a[k] = a[k+c]; a[k+c] = t;
        }

        for( j = 0; j < group_size; j++ ) {
            int cid = a[j];  // client id
            snprintf(client_name, 1024, "%s%d", base_client_name, cid);
            snprintf(cache_filepath, 1024, "%s/%s%d.cache", 
                        RUNTIME_DIR, base_client_name, cid);
        
             
             if( client_exec( group_name, cache_filepath,
                         timeLog_filepath, ksm) < 0 )
             { 
                 for( k = 0; k < num_of_clients; k++ ) {
                     snprintf(client_name, 1024, "%s%d", 
                                        base_client_name, k);
                     delete_client_cache( client_name );
                     delete_client_princ( client_name );
                 }
                 delete_service("group",group_count); 
                 printf("Client failed\n");
                 exit(1);
             }
    
             long last_val = get_last_value( timeLog_filepath );
    
             if( last_val > 100 || last_val < 0) {
                 snprintf(shell_command, BUFSIZ,"%s %s",
                             DEL_LAST_LINE, 
                             timeLog_filepath,
                             NULL);
                 // Reject data that's out of range
                 system(shell_command);
                 if (ksm == 2) {
                     delete_client_cache( client_name );
                     create_client_cache( client_name );                    
                 }
    //               printf("last_val too high, reject%ld\n", last_val);
                 j--;
             }   
        }
    }
   
    timeLog_file = fopen(timeLog_filepath, "r");
    
    if( timeLog_file != NULL ) {
        char line[128];
        for(i = 0; i < group_size*group_count; i++ ) {

            if( fgets(line, sizeof(line), 
                   timeLog_file) != NULL ) 
            {
                total_time += atol( line );
            }
        }
        fclose(timeLog_file);
    }
    else {
        perror( timeLog_filepath );
    }

    for( i = 0; i < num_of_clients; i++ ) {
        snprintf(client_name, 1024, "%s%d", base_client_name, i);
        delete_client_cache( client_name );
        delete_client_princ( client_name );
    }
    //printf("\n"); 
    return total_time;
}