int main(int argc, char *argv[]){ options opts; opts.n_arrows = 100; opts.kmeans_attempts = 1; opts.use_all_vectors = 0; int c, i; opterr = 0; while((c = getopt(argc, argv, "ak:n:")) != -1){ switch(c){ case 'a': opts.use_all_vectors = 1; break; case 'k': opts.kmeans_attempts = atoi(optarg); if(opts.kmeans_attempts < 1){ opts.kmeans_attempts = 1; } break; case 'n': opts.n_arrows = atoi(optarg); if(opts.n_arrows < 1){ opts.n_arrows = 1; } break; case '?': if('k' == optopt || 'n' == optopt){ fprintf(stderr, "Option -%c requires an argument.\n", optopt); }else if(isprint(optopt)){ fprintf(stderr, "Unknown option `-%c'.\n", optopt); }else{ fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt); } default: usage(); return EXIT_FAILURE; } } if(optind >= argc){ usage(); return EXIT_FAILURE; } for(i = optind; i < argc; i++){ data d; data_init(&d); data_read(&d, argv[i]); if(opts.use_all_vectors || d.n <= opts.n_arrows || data_cull_zeros(&d, opts.n_arrows)){ output_plot(&d); }else{ data plot_data; data_init(&plot_data); gen_plot_kmeans(&d, opts.n_arrows, &plot_data, opts.kmeans_attempts); output_plot(&plot_data); data_destroy(&plot_data); } data_destroy(&d); } return EXIT_SUCCESS; }
void fit_destroy(fitinfo *fit) { pars_destroy(&fit->pars); profile_destroy(&fit->p); model_destroy(&fit->m); data_destroy(&fit->dataA); data_destroy(&fit->dataB); data_destroy(&fit->dataC); data_destroy(&fit->dataD); interface_destroy(&fit->rm); if (fit->worksize>0) free(fit->work); if (fit->capacity>0) free(fit->fitQ); fit->capacity = -1; fit->nQ = 0; fit->worksize = 0; }
void entry_destroy(struct entry_t *entry){ data_destroy(entry->value); free(entry->key); free(entry); }
int testDup() { int result, data_size = strlen("1234567890abc")+1; char *data_s = strdup("1234567890abc"); struct data_t *data = data_create2(data_size,data_s); struct data_t *data2 = data_dup(data); result = (data->data != data2->data) && (data->datasize == data2->datasize) && (memcmp(data->data, data2->data, data->datasize) == 0); data_destroy(data); data_destroy(data2); printf("Modulo data -> teste data_dup: %s\n",result?"passou":"nao passou"); return result; }
int testRemoveCabeca() { int result; struct list_t *list; struct entry_t *e1, *e2, *e3, *entry; struct data_t *data; printf("Módulo list -> teste remover cabeça:"); if ((list = list_create()) == NULL) error(1, errno, " O teste não pode prosseguir"); if ((data = data_create(5)) == NULL) error(1, errno, " O teste não pode prosseguir"); e1 = entry_create("abc", data); e2 = entry_create("def", data); e3 = entry_create("ghi", data); if (e1 == NULL || e2 == NULL || e3 == NULL) error(1, errno, " O teste não pode prosseguir"); memcpy(e1->value->data, "abc1", 5); memcpy(e2->value->data, "def1", 5); memcpy(e3->value->data, "ghi1", 5); list_add(list,e1); list_add(list,e2); list_add(list,e3); assert(list_remove(NULL, "abc") < 0); result = (list_remove(NULL, "abc") < 0); assert(list_remove(list, NULL) < 0); result = result && (list_remove(list, NULL) < 0); result = result && list_remove(list, "abc") == 0 && list_size(list) == 2; entry = list_get(list, "def"); result = result && entry != e2 && strcmp(entry->key, e2->key) == 0; entry = list_get(list, "ghi"); result = result && entry != e3 && strcmp(entry->key, e3->key) == 0; list_destroy(list); entry_destroy(e1); entry_destroy(e2); entry_destroy(e3); data_destroy(data); printf(" %s\n", result ? "passou" : "não passou"); return result; }
int testCreate() { int result; struct data_t *data = data_create(1024); memcpy(data->data,"1234567890a",strlen("1234567890a")+1); result = (strcmp(data->data,"1234567890a") == 0) && (data->datasize == 1024); data_destroy(data); printf("Modulo data -> teste data_create: %s\n",result?"passou":"nao passou"); return result; }
int test_regression_evaluate_population(void) { struct data *d = csv_load_data(TEST_DATA, 1, ","); setup_population(); regression_evaluate_population(p, d); /* clean up */ teardown_population(); data_destroy(d); return 0; }
int testGetKeys() { int result; struct list_t *list; struct entry_t *e1, *e2, *e3; struct data_t *data; char **keys; printf("Módulo list -> teste busca chaves:"); if ((list = list_create()) == NULL) error(1, errno, " O teste não pode prosseguir"); if ((data = data_create(5)) == NULL) error(1, errno, " O teste não pode prosseguir"); assert(list_get_keys(NULL) == NULL); result = (list_get_keys(NULL) == NULL); e1 = entry_create("abc", data); e2 = entry_create("def", data); e3 = entry_create("ghi", data); if (e1 == NULL || e2 == NULL || e3 == NULL) error(1, errno, " O teste não pode prosseguir"); memcpy(e1->value->data, "abc1", 5); memcpy(e2->value->data, "def1", 5); memcpy(e3->value->data, "ghi1", 5); list_add(list,e1); list_add(list,e2); list_add(list,e3); if ((keys = list_get_keys(list)) == NULL) error(1, errno, " O teste não pode prosseguir"); result = strcmp(keys[0], e1->key) == 0 && keys[0] != e1->key && strcmp(keys[1], e2->key) == 0 && keys[1] != e2->key && strcmp(keys[2], e3->key) == 0 && keys[2] != e3->key && keys[3] == NULL; list_free_keys(keys); list_destroy(list); entry_destroy(e1); entry_destroy(e2); entry_destroy(e3); data_destroy(data); printf(" %s\n", result ? "passou" : "não passou"); return result; }
int main(void) { int i; _config = config_create(FILE_CONFIG); _log = log_create(FILE_LOG, "Nodo", false, LOG_LEVEL_INFO); _data = data_get(config_get_string_value(_config, CONFIG_ARCHIVO_BIN)); bloques_set(); //////////////////////// pthread_t p_fs; if (pthread_create(&p_fs, NULL, (void*) fs_conectar, NULL) != 0) { perror("pthread_create"); exit(1); } pthread_join(p_fs, (void**) NULL); /* void* saludo = malloc(BLOQUE_SIZE); strcpy(saludo, "ahora cambio el mensaje!"); setBloque(0, saludo); void* dataget = getBloque(0); char* saludoget =(char*) malloc(strlen(saludo)+1); memcpy(saludoget, dataget, strlen(saludo)+1); printf("%s\n", saludoget); free_null(saludo); free_null(saludoget); free_null(dataget); */ /* char *d = NULL; d = getFileContent("hola"); for(i=0;i<10;i++) printf("%c", d[i]); file_mmap_free(d, "hola"); */ data_destroy(); config_destroy(_config); printf("fin ok"); //while (true); return EXIT_SUCCESS; }
// delete the contents of the bucket. Note, that the bucket becomes empty, but the bucket itself is // not destroyed. void bucket_destroy_contents(bucket_t *bucket) { assert(bucket); // at this point, since the bucket is being destroyed, there should be a connected transfer client. assert(bucket->transfer_client == NULL); if (bucket->data) { data_destroy(bucket->data, _mask, bucket->hashmask); data_free(bucket->data); bucket->data = NULL; } assert(bucket->data == NULL); }
int test_regression_evaluate(void) { int i; int res; float f = 100.0; float **func_input; float solution_score = 5.0; struct tree *t; struct node **chromosome = malloc(sizeof(struct node *) * 5); struct data *d = csv_load_data(TEST_DATA, 1, ","); /* initialize func_input */ func_input = malloc(sizeof(float *) * 2); for (i = 0; i < 2; i++) { func_input[i] = malloc(sizeof(float) * (unsigned long) d->rows); } /* build chromosome */ chromosome[0] = node_new_constant(FLOAT, &f); chromosome[1] = node_new_input((char *) "x"); chromosome[2] = node_new_func(MUL, 2); chromosome[3] = node_new_func(RAD, 1); chromosome[4] = node_new_func(SIN, 1); /* build tree */ t = malloc(sizeof(struct tree)); t->root = NULL; t->size = 5; t->depth = -1; t->score = NULL; t->chromosome = chromosome; /* evaluate tree */ res = regression_evaluate(t, func_input, d, (char *) "y"); mu_check(res == 0); mu_check(fltcmp(t->score, &solution_score) == 0); mu_check(t->hits == 361); /* clean up */ free_chromosome(chromosome, 5); tree_destroy(t); data_destroy(d); free_mem_arr(func_input, 2, free); return 0; }
int testAddVarios() { int result,i,keysize; struct list_t *list; struct entry_t *entry[1024]; struct entry_t *aux; struct data_t *data; char key[16]; printf("Módulo list -> teste adicionar vários:"); if ((list = list_create()) == NULL) error(1, errno, " O teste não pode prosseguir"); for(i=0; i<1024; i++) { sprintf(key, "keyabc-%d",i); keysize = strlen(key) + 1; if ((data = data_create(keysize)) == NULL) error(1, errno, " O teste não pode prosseguir"); memcpy(data->data, key, keysize); if ((entry[i] = entry_create(key, data)) == NULL) error(1, errno, " O teste não pode prosseguir"); data_destroy(data); list_add(list, entry[i]); } assert(list_size(list) == 1024); result = (list_size(list) == 1024); for(i=0; i<1024; i++) { assert(list_get(list, entry[i]->key) != entry[i]); aux = list_get(list, entry[i]->key); result = result && (aux != entry[i]) && strcmp(aux->key, entry[i]->key) == 0; entry_destroy(entry[i]); } list_destroy(list); printf(" %s\n", result ? "passou" : "não passou"); return result; }
int testAddCabeca() { int result; struct list_t *list; struct entry_t *entry; struct entry_t *entry2; struct data_t *data; printf("Módulo list -> teste adicionar cabeça:"); if ((list = list_create()) == NULL) error(1, errno, " O teste não pode prosseguir"); if ((data = data_create(5)) == NULL) error(1, errno, " O teste não pode prosseguir"); if ((entry = entry_create("abc", data)) == NULL) error(1, errno, " O teste não pode prosseguir"); memcpy(entry->value->data, "abc1", 5); assert(list_add(NULL, entry) < 0); result = (list_add(NULL, entry) < 0); assert(list_add(list, NULL) < 0); result = result && (list_add(list, NULL) < 0); result = result && (list_add(list, entry) == 0); entry2 = list_get(list, "abc"); result = result && entry2 != entry && list_size(list) == 1 && strcmp(entry->key, entry2->key) == 0; entry_destroy(entry); data_destroy(data); list_destroy(list); printf(" %s\n",result ? "passou" : "não passou"); return result; }
int main(int argc, char**argv){ //verifica se tem no argumento o adress_port if(argc < 2) { perror("Poucos ou muitos argumentos"); return -1; } char ** outKeys; char *input = malloc(100 * sizeof(char)); struct data_t *inData, *outData; struct rtable_t *rtable; char *instruction; char *inKeyData; char *inKey; int numSpace,i; if((rtable = rtable_bind(argv[1])) == NULL) { perror("Erro ao conectar-se ao servidor"); return -1; } while(1){ printf("Enter an instruction: "); fgets(input, 100, stdin); //Numero de Espaços for(i=0,numSpace=0;input[i]!='\0';i++) if(input[i] == ' ') numSpace++; switch(numSpace) { case 0: instruction = strtok(input,"\n"); break; case 1: instruction = strtok(input," "); inKey = strtok(NULL,"\n"); break; case 2: instruction = strtok(input," "); inKey = strtok(NULL," "); inKeyData = strtok(NULL,"\n"); if(inKey != NULL && inKeyData != NULL) inData = data_create2(strlen(inKeyData),inKeyData); break; default: instruction = ""; break; } if(strcmp(instruction,"put") == 0 && numSpace == 2) { if(rtable_put(rtable,inKey,inData)==0) { printf("Operação executada com sucesso\n"); } else { printf("Operação executa com insucesso\n"); } data_destroy(inData); } else if(strcmp(instruction,"get") == 0 && numSpace == 1) { if(strcmp(inKey,"!") == 0) { outKeys = rtable_get_keys(rtable); if(outKeys != NULL) { printf("Operação executada com sucesso\n"); for(i = 0; outKeys[i] != NULL ; i++) { printf("A tabela remota tem a key: %s\n", outKeys[i]); } rtable_free_keys(outKeys); } else { printf("Operação executa com insucesso\n"); } } else { outData = rtable_get(rtable,inKey); if(outData != NULL) { printf("Operação executada com sucesso\n"); printf("Data recebida com tamanho %d\n",outData->datasize); data_destroy(outData); } else { printf("Operação executa com insucesso\n"); } } } else if(strcmp(instruction,"update" ) == 0 && numSpace == 2) { if(rtable_update(rtable,inKey,inData)==0) { printf("Operação executada com sucesso\n"); } else { printf("Operação executa com insucesso\n"); } data_destroy(inData); } else if(strcmp(instruction,"del") == 0 && numSpace == 1) { if(rtable_del(rtable,inKey)==0) { printf("Operação executada com sucesso\n"); } else { printf("Operação executa com insucesso\n"); } } else if(strcmp(instruction,"size") == 0 && numSpace == 0) { printf("Numero de elementos da tabela remota: %d\n",rtable_size(rtable)); } else if(strcmp(instruction,"quit") == 0 && numSpace == 0) { free(input); if(rtable_unbind(rtable)<0) { perror("rtable_unbind"); return -1; } printf("Adeus humano!\n"); return 0; } else { printf("Non-valid instruction\n"); } } }
/** * Reorganize histogram. * * <p>Remarks</p> * <ul> * <li>If (exist == NULL), the number of histogram blocks is set to nind, * by adding empty blocks or removing blocks from the end.</li> * <li>If (exist != NULL), histogram blocks may selected as follows: * exist(i,0) == 0: block i is deleted; * exist(i,0) > 0: block i is preserved</li> * <li> Only preserved blocks will be available after reorganization! The * index correspondence may be changed by this operation!nind is not used * in this case.</li> * </ul> * * @param exist - index existing flag sequence (may be NULL) * @param nind - index number * @return O_K if ok, NOT_EXEC if not executed */ INT16 CGEN_PUBLIC CHistogram::ReorgEx(CData* exist, INT32 nind) { INT32 n, ifree, i, imax, rl; CData* h; if (CheckHisto() != O_K) return (NOT_EXEC); /* --- change only histogram number */ if (exist == NULL) { if (nind > 0 && nind != data_nblock(m_hist)) { data_realloc (m_hist, nind * m_bins); set_data_nblock(m_hist, nind); set_data_nrec (m_hist, nind * m_bins); return (O_K); } else return (NOT_EXEC); } /* --- reorganize according to exist */ if (exist != NULL) { imax = data_nrec(exist); n = 0; for (i = 0; i < imax; i++) { if (dfetch (exist,i,0) > 0.0) n++; } if (n == imax) return (O_K); h = data_create("h"); data_copy (m_hist,h); rl = BytesPerBlock(); data_realloc (m_hist, n * m_bins); ifree = 0; for (i = 0; i < imax; i++) { if (dfetch (exist,i,0) > 0.0) { dl_memmove ( xaddr(m_hist,ifree,0),xaddr(h,i*m_bins,0), rl); ifree += m_bins; } } if (m_hmode == 3) { data_copy (m_hist,h); rl = 2 * data_reclen(m_minmax); data_realloc (m_minmax, n * 2); ifree = 0; for (i = 0; i < imax; i++) { if (dfetch (exist,i,0) > 0.0) { dl_memmove ( xaddr(m_minmax,ifree,0),xaddr(h,2*i,0), rl); ifree += 2; } } } data_destroy(h); set_data_nblock(m_hist, n); return (O_K); } return (O_K); }
int testEntry() { int result, size, keysize, datasize, datasize_conv; short opcode, c_type, keysize_conv; char *msg_str = NULL; char *datastr = strdup("abc"); struct message_t *msg; struct data_t *d; printf("Módulo mensagem -> teste - Entry"); d = data_create2(strlen(datastr) + 1, datastr); msg = (struct message_t *) malloc(sizeof(struct message_t)); msg->opcode = OC_PUT; msg->c_type = CT_ENTRY; msg->content.entry = entry_create(datastr, d); data_destroy(d); size = message_to_buffer(msg, &msg_str); opcode = htons(msg->opcode); c_type = htons(msg->c_type); keysize = strlen(msg->content.entry->key); keysize_conv = htons(keysize); char comp_key[keysize]; memcpy(comp_key, msg_str + 6, keysize); datasize = msg->content.entry->value->datasize; datasize_conv = htonl(datasize); char comp_data[datasize]; memcpy(comp_data, msg_str + 4 + 2 + keysize + 4, datasize); result = memcmp(msg_str, &opcode, 2) == 0 && memcmp(msg_str + 2, &c_type, 2) == 0 && memcmp(msg_str + 4, &keysize_conv, 2) == 0 && memcmp(msg_str + 6, &comp_key, keysize) == 0 && memcmp(msg_str + 6 + keysize, &datasize_conv, 4) == 0 && memcmp(msg_str + 6 + keysize + 4, &comp_data, datasize) == 0; free_message(msg); msg = buffer_to_message(msg_str, size); result = result && msg->opcode == OC_PUT && msg->c_type == CT_ENTRY && strcmp(msg->content.entry->key, datastr) == 0 && msg->content.entry->value->datasize == strlen(datastr) + 1 && strcmp(msg->content.entry->value->data, datastr) == 0; free(msg_str); free(datastr); //print_message(msg); free_message(msg); printf(" %s\n", result ? "passou" : "não passou"); return result; }
int test_regression_traverse(void) { int i; int res; float f1 = 1.0; float f2 = 2.0; float f3; float *flt_ptr; float zero = 0.0; float **func_input; struct node *result_node; struct node **chromosome = malloc(sizeof(struct node *) * 3); struct stack *stack = stack_new(); struct data *d = csv_load_data(TEST_DATA, 1, ","); /* initialize func_input */ func_input = malloc(sizeof(float *) * 2); for (i = 0; i < 2; i++) { func_input[i] = malloc(sizeof(float) * (unsigned long) d->rows); } /* ADD */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_constant(FLOAT, &f2); chromosome[2] = node_new_func(ADD, 2); res = regression_traverse(0, 2, chromosome, stack, func_input, d); result_node = stack_pop(stack); for (i = 0; i < d->rows; i++) { f3 = f1 + f2; flt_ptr = &((float *) result_node->values)[i]; mu_check(fltcmp(flt_ptr, &f3) == 0); } node_destroy(result_node); free_chromosome(chromosome, 3); /* SUB */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_constant(FLOAT, &f2); chromosome[2] = node_new_func(SUB, 2); res = regression_traverse(0, 2, chromosome, stack, func_input, d); result_node = stack_pop(stack); for (i = 0; i < d->rows; i++) { f3 = f1 - f2; flt_ptr = &((float *) result_node->values)[i]; mu_check(fltcmp(flt_ptr, &f3) == 0); } node_destroy(result_node); free_chromosome(chromosome, 3); /* MUL */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_constant(FLOAT, &f2); chromosome[2] = node_new_func(MUL, 2); res = regression_traverse(0, 2, chromosome, stack, func_input, d); result_node = stack_pop(stack); for (i = 0; i < d->rows; i++) { f3 = f1 * f2; flt_ptr = &((float *) result_node->values)[i]; mu_check(fltcmp(flt_ptr, &f3) == 0); } node_destroy(result_node); free_chromosome(chromosome, 3); /* DIV */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_constant(FLOAT, &f2); chromosome[2] = node_new_func(DIV, 2); res = regression_traverse(0, 2, chromosome, stack, func_input, d); result_node = stack_pop(stack); for (i = 0; i < d->rows; i++) { f3 = f1 / f2; flt_ptr = &((float *) result_node->values)[i]; mu_check(fltcmp(flt_ptr, &f3) == 0); } node_destroy(result_node); free_chromosome(chromosome, 3); /* DIV - FAIL TEST - DIVIDE BY ZERO */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_constant(FLOAT, &zero); chromosome[2] = node_new_func(DIV, 2); res = regression_traverse(0, 2, chromosome, stack, func_input, d); mu_check(res == -1); free_chromosome(chromosome, 3); /* POW */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_constant(FLOAT, &f2); chromosome[2] = node_new_func(POW, 2); res = regression_traverse(0, 2, chromosome, stack, func_input, d); result_node = stack_pop(stack); for (i = 0; i < d->rows; i++) { f3 = (float) pow(f1, f2); flt_ptr = &((float *) result_node->values)[i]; mu_check(fltcmp(flt_ptr, &f3) == 0); } node_destroy(result_node); free_chromosome(chromosome, 3); /* LOG */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_func(LOG, 1); res = regression_traverse(0, 1, chromosome, stack, func_input, d); result_node = stack_pop(stack); for (i = 0; i < d->rows; i++) { f3 = (float) log(f1); flt_ptr = &((float *) result_node->values)[i]; mu_check(fltcmp(flt_ptr, &f3) == 0); } node_destroy(result_node); free_chromosome(chromosome, 2); /* EXP */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_func(EXP, 1); res = regression_traverse(0, 1, chromosome, stack, func_input, d); result_node = stack_pop(stack); for (i = 0; i < d->rows; i++) { f3 = (float) exp(f1); flt_ptr = &((float *) result_node->values)[i]; mu_check(fltcmp(flt_ptr, &f3) == 0); } node_destroy(result_node); free_chromosome(chromosome, 2); /* RAD */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_func(RAD, 1); res = regression_traverse(0, 1, chromosome, stack, func_input, d); result_node = stack_pop(stack); for (i = 0; i < d->rows; i++) { f3 = (float) f1 * (float) (PI / 180.0); flt_ptr = &((float *) result_node->values)[i]; mu_check(fltcmp(flt_ptr, &f3) == 0); } node_destroy(result_node); free_chromosome(chromosome, 2); /* SIN */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_func(SIN, 1); res = regression_traverse(0, 1, chromosome, stack, func_input, d); result_node = stack_pop(stack); for (i = 0; i < d->rows; i++) { f3 = (float) sin(f1); flt_ptr = &((float *) result_node->values)[i]; mu_check(fltcmp(flt_ptr, &f3) == 0); } node_destroy(result_node); free_chromosome(chromosome, 2); /* COS */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_func(COS, 1); res = regression_traverse(0, 1, chromosome, stack, func_input, d); result_node = stack_pop(stack); for (i = 0; i < d->rows; i++) { f3 = (float) cos(f1); flt_ptr = &((float *) result_node->values)[i]; mu_check(fltcmp(flt_ptr, &f3) == 0); } node_destroy(result_node); free_chromosome(chromosome, 2); /* FAIL TEST - UNKNOWN FUNCTION */ chromosome[0] = node_new_constant(FLOAT, &f1); chromosome[1] = node_new_func(99, 1); res = regression_traverse(0, 1, chromosome, stack, func_input, d); mu_check(res == -2); free_chromosome(chromosome, 2); /* clean up */ stack_destroy(stack, free); free(chromosome); data_destroy(d); free_mem_arr(func_input, 2, free); return 0; }
int main(int argc, char *argv[]) { int x; int result; DATA D = new_data(); ARRAY urls = new_array(); CREW crew; LINES *lines; CLIENT *client; pthread_t cease; pthread_t timer; pthread_attr_t scope_attr; void *statusp; sigset_t sigs; sigemptyset(&sigs); sigaddset(&sigs, SIGHUP); sigaddset(&sigs, SIGINT); sigaddset(&sigs, SIGALRM); sigaddset(&sigs, SIGTERM); sigprocmask(SIG_BLOCK, &sigs, NULL); lines = xcalloc(1, sizeof *lines); lines->index = 0; lines->line = NULL; memset(&my, 0, sizeof(struct CONFIG)); parse_rc_cmdline(argc, argv); if (init_config() < 0) { /* defined in init.h */ exit( EXIT_FAILURE ); /* polly was a girl... */ } parse_cmdline(argc, argv); /* defined above */ ds_module_check(); /* check config integ */ /** * XXX: we should consider moving the following * if-checks into the ds_module_check */ if (my.config) { show_config(TRUE); } if (my.url != NULL) { my.length = 1; } else { my.length = read_cfg_file(lines, my.file); } if (my.reps < 0) { my.reps = my.length; } if (my.length == 0) { display_help(); } /* cookie is an EXTERN, defined in setup */ cookie = xcalloc(sizeof(COOKIE), 1); cookie->first = NULL; if ((result = pthread_mutex_init( &(cookie->mutex), NULL)) !=0) { NOTIFY(FATAL, "pthread_mutex_init" ); } /* memory allocation for threads and clients */ client = xcalloc(my.cusers, sizeof(CLIENT)); if ((crew = new_crew(my.cusers, my.cusers, FALSE)) == NULL) { NOTIFY(FATAL, "unable to allocate memory for %d simulated browser", my.cusers); } /** * determine the source of the url(s), * command line or file, and add them * to the urls struct. */ if (my.url != NULL) { URL tmp = new_url(my.url); url_set_ID(tmp, 0); if (my.get && url_get_method(tmp) != POST && url_get_method(tmp) != PUT) { url_set_method(tmp, my.method); } array_npush(urls, tmp, URLSIZE); // from cmd line } else { for (x = 0; x < my.length; x++) { URL tmp = new_url(lines->line[x]); url_set_ID(tmp, x); array_npush(urls, tmp, URLSIZE); } } /** * display information about the siege * to the user and prepare for verbose * output if necessary. */ if (!my.get && !my.quiet) { fprintf(stderr, "** "); display_version(FALSE); fprintf(stderr, "** Preparing %d concurrent users for battle.\n", my.cusers); fprintf(stderr, "The server is now under siege..."); if (my.verbose) { fprintf(stderr, "\n"); } } /** * record start time before spawning threads * as the threads begin hitting the server as * soon as they are created. */ data_set_start(D); /** * for each concurrent user, spawn a thread and * loop until condition or pthread_cancel from the * handler thread. */ pthread_attr_init(&scope_attr); pthread_attr_setscope(&scope_attr, PTHREAD_SCOPE_SYSTEM); #if defined(_AIX) /* AIX, for whatever reason, defies the pthreads standard and * * creates threads detached by default. (see pthread.h on AIX) */ pthread_attr_setdetachstate(&scope_attr, PTHREAD_CREATE_JOINABLE); #endif /** * invoke OpenSSL's thread safety */ #ifdef HAVE_SSL SSL_thread_setup(); #endif /** * create the signal handler and timer; the * signal handler thread (cease) responds to * ctrl-C (sigterm) and the timer thread sends * sigterm to cease on time out. */ if ((result = pthread_create(&cease, NULL, (void*)sig_handler, (void*)crew)) < 0) { NOTIFY(FATAL, "failed to create handler: %d\n", result); } if (my.secs > 0) { if ((result = pthread_create(&timer, NULL, (void*)siege_timer, (void*)cease)) < 0) { NOTIFY(FATAL, "failed to create handler: %d\n", result); } } /** * loop until my.cusers and create a corresponding thread... */ for (x = 0; x < my.cusers && crew_get_shutdown(crew) != TRUE; x++) { client[x].id = x; client[x].bytes = 0; client[x].time = 0.0; client[x].hits = 0; client[x].code = 0; client[x].ok200 = 0; client[x].fail = 0; client[x].urls = urls; client[x].auth.www = 0; client[x].auth.proxy = 0; client[x].auth.type.www = BASIC; client[x].auth.type.proxy = BASIC; client[x].rand_r_SEED = urandom(); result = crew_add(crew, (void*)start_routine, &(client[x])); if (result == FALSE) { my.verbose = FALSE; fprintf(stderr, "Unable to spawn additional threads; you may need to\n"); fprintf(stderr, "upgrade your libraries or tune your system in order\n"); fprintf(stderr, "to exceed %d users.\n", my.cusers); NOTIFY(FATAL, "system resources exhausted"); } } /* end of for pthread_create */ crew_join(crew, TRUE, &statusp); #ifdef HAVE_SSL SSL_thread_cleanup(); #endif /** * collect all the data from all the threads that * were spawned by the run. */ for (x = 0; x < ((crew_get_total(crew) > my.cusers || crew_get_total(crew)==0 ) ? my.cusers : crew_get_total(crew)); x++) { data_increment_count(D, client[x].hits); data_increment_bytes(D, client[x].bytes); data_increment_total(D, client[x].time); data_increment_code (D, client[x].code); data_increment_ok200(D, client[x].ok200); data_increment_fail (D, client[x].fail); data_set_highest (D, client[x].himark); data_set_lowest (D, client[x].lomark); client[x].rand_r_SEED = urandom(); } /* end of stats accumulation */ /** * record stop time */ data_set_stop(D); /** * cleanup crew */ crew_destroy(crew); for (x = 0; x < my.cusers; x++) { // XXX: TODO //digest_challenge_destroy(client[x].auth.wwwchlg); //digest_credential_destroy(client[x].auth.wwwcred); //digest_challenge_destroy(client[x].auth.proxychlg); //digest_credential_destroy(client[x].auth.proxycred); } array_destroy(my.lurl); xfree(client); if (my.get) { if (data_get_ok200(D) > 0) { exit(EXIT_SUCCESS); } else { if (!my.quiet) echo("[done]\n"); exit(EXIT_FAILURE); } } /** * take a short nap for cosmetic effect * this does NOT affect performance stats. */ pthread_usleep_np(10000); if (my.verbose) fprintf(stderr, "done.\n"); else fprintf(stderr, "\b done.\n"); /** * prepare and print statistics. */ if (my.failures > 0 && my.failed >= my.failures) { fprintf(stderr, "%s aborted due to excessive socket failure; you\n", program_name); fprintf(stderr, "can change the failure threshold in $HOME/.%src\n", program_name); } fprintf(stderr, "\nTransactions:\t\t%12u hits\n", data_get_count(D)); fprintf(stderr, "Availability:\t\t%12.2f %%\n", data_get_count(D)==0 ? 0 : (double)data_get_count(D) / (data_get_count(D)+my.failed) *100 ); fprintf(stderr, "Elapsed time:\t\t%12.2f secs\n", data_get_elapsed(D)); fprintf(stderr, "Data transferred:\t%12.2f MB\n", data_get_megabytes(D)); /*%12llu*/ fprintf(stderr, "Response time:\t\t%12.3f secs\n", data_get_response_time(D)); fprintf(stderr, "Transaction rate:\t%12.2f trans/sec\n", data_get_transaction_rate(D)); fprintf(stderr, "Throughput:\t\t%12.2f MB/sec\n", data_get_throughput(D)); fprintf(stderr, "Concurrency:\t\t%12.2f\n", data_get_concurrency(D)); fprintf(stderr, "Successful transactions:%12u\n", data_get_code(D)); if (my.debug) { fprintf(stderr, "HTTP OK received:\t%12u\n", data_get_ok200(D)); } fprintf(stderr, "Failed transactions:\t%12u\n", my.failed); fprintf(stderr, "Longest transaction:\t%12.3f\n", data_get_highest(D)); fprintf(stderr, "Shortest transaction:\t%12.3f\n", data_get_lowest(D)); fprintf(stderr, " \n"); if(my.mark) mark_log_file(my.markstr); if(my.logging) log_transaction(D); data_destroy(D); if (my.url == NULL) { for (x = 0; x < my.length; x++) xfree(lines->line[x]); xfree(lines->line); xfree(lines); } else { xfree(lines->line); xfree(lines); } pthread_mutex_destroy( &(cookie->mutex)); /** * I should probably take a deeper look * at cookie content to free it but at * this point we're two lines from exit */ xfree (cookie); xfree (my.url); exit(EXIT_SUCCESS); } /* end of int main **/
int test_regression_func_input(void) { int i; int res; float f = 100.0; float *f_arr; float **func_input; struct data *d = csv_load_data(TEST_DATA, 1, ","); struct node **stack = malloc(sizeof(struct node *) * 4); /* malloc func_input */ func_input = malloc(sizeof(float *) * 2); for (i = 0; i < 2; i++) { func_input[i] = malloc(sizeof(float) * (unsigned long) d->rows); } /* initialize float array */ f_arr = malloc(sizeof(float) * (unsigned long) d->rows); for (i = 0; i < d->rows; i++) { f_arr[i] = (float) i; } /* create stack */ stack[0] = node_new_input((char *) "x"); stack[1] = node_new_input((char *) "y"); stack[2] = node_new_constant(FLOAT, &f); stack[3] = node_new_eval(FLOAT, (void *) f_arr, d->rows); /* input node - x */ regression_func_input(stack[0], d, func_input, 0); for (i = 0; i < d->rows; i++) { res = fltcmp(&func_input[0][i], d->data[data_field_index(d, "x")][i]); mu_check(res == 0); } /* input node - y */ regression_func_input(stack[1], d, func_input, 1); for (i = 0; i < d->rows; i++) { res = fltcmp(&func_input[1][i], d->data[data_field_index(d, "y")][i]); mu_check(res == 0); } /* constant node */ regression_func_input(stack[2], d, func_input, 0); for (i = 0; i < d->rows; i++) { res = fltcmp(&func_input[0][i], &f); mu_check(res == 0); } /* eval node */ regression_func_input(stack[3], d, func_input, 0); for (i = 0; i < d->rows; i++) { f = (float) i; res = fltcmp(&func_input[0][i], &f); mu_check(res == 0); } /* clean up */ node_destroy(stack[0]); node_destroy(stack[1]); node_destroy(stack[2]); node_destroy(stack[3]); data_destroy(d); free_mem_arr(func_input, 2, free); free(stack); return 0; }