static PyObject *
get_communities_pylist(bgpstream_community_set_t *communities) {

  PyObject *list;
  bgpstream_community_t *c;
  Py_ssize_t len = bgpstream_community_set_size(communities);
  int i;
  /* create the dictionary */
  if((list = PyList_New(len)) == NULL)
    return NULL;
  
  PyObject *dict;

  for(i = 0; i< len; i++)
    {
      c = bgpstream_community_set_get(communities, i);

      /* create the dictionary */
      if((dict = PyDict_New()) == NULL)
        return NULL;
      /* add pair to dictionary */
      if (add_to_dict(dict, "asn", Py_BuildValue("k", c->asn)) ||
          add_to_dict(dict, "value", Py_BuildValue("k", c->value))) {
        return NULL;
      }

      /* add dictionary to list*/
      PyList_SetItem(list, (Py_ssize_t)i, dict);
    }
  return list;
}
Exemple #2
0
void set_corpus(void)
{
	char start[1024];
	struct dentry *d;

	while (scanf("%s", start)!= EOF) {
		d = get_dentry(start);
		if (!d)
			return;

		add_to_dict(d);
	}
}
Exemple #3
0
static
void add_node_parser(list *L, ASMinstr value, dictionary *D) // a hack due to bad design!
{
    node_mod = add_node_and_get_address(L, value);
    fprintf(stderr, "%d ", node_mod);

    /*using a global variable; the following line will do nothing if previouslabel == NULL */
    add_to_dict(D, hash(previouslabel), node_mod);

    /* free(previouslabel); */          // DO NOT free previouslabel's space; dict only has a
                                        // pointer to that space, not a copy!

    previouslabel = NULL;               // add it only once!
}
/** Type-dependent field dict */
static PyObject *
BGPElem_get_fields(BGPElemObject *self, void *closure)
{
  PyObject *dict;

  /* create the dictionary */
  if((dict = PyDict_New()) == NULL)
    return NULL;

  switch(self->elem->type)
    {
    case BGPSTREAM_ELEM_TYPE_RIB:
    case BGPSTREAM_ELEM_TYPE_ANNOUNCEMENT:
      if (add_to_dict(dict, "next-hop", get_ip_pystr((bgpstream_ip_addr_t *)&self->elem->nexthop)) ||
          add_to_dict(dict, "as-path", get_aspath_pystr(self->elem->aspath)) ||
          add_to_dict(dict, "communities", get_communities_pylist(self->elem->communities))) {
        return NULL;
      }

      /* FALLTHROUGH */

    case BGPSTREAM_ELEM_TYPE_WITHDRAWAL:
      if (add_to_dict(dict, "prefix", get_pfx_pystr((bgpstream_pfx_t *)&self->elem->prefix))) {
        return NULL;
      }
      break;

    case BGPSTREAM_ELEM_TYPE_PEERSTATE:
      if (add_to_dict(dict, "old-state", get_peerstate_pystr(self->elem->old_state)) ||
          add_to_dict(dict, "new-state", get_peerstate_pystr(self->elem->new_state))) {
        return NULL;
      }
      break;

    case BGPSTREAM_ELEM_TYPE_UNKNOWN:
    default:
      break;
    }

  return Py_BuildValue("N", dict);
}
/** This function created multiple key value pairs and reads them and tests that they are okay **/
void testMultipleReadWrites()
{
  setup();

  unsigned int i=0;
  unsigned int count_iter=0;
  size_t len = 100;
  char keyBuffer[100];
  char * valueBuffer = (char *)malloc(len+1);
  char readBuffer[100];
  ssize_t read;
  clock_t begin, end;
  FILE * fp;
  
  printf("Initiating DB Writes. \n");
  fp = fopen("../sample_emails.txt", "r");
  begin = clock();
  while ((read = getline(&valueBuffer, &len, fp)) != -1) 
  {
    printPoll(count_iter);
    sprintf(keyBuffer,"%d",count_iter++);
    add_to_dict(TEST_DB_NAME,TEST_DIR_NAME,keyBuffer,valueBuffer);
  }
  end = clock();
  double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  fclose(fp);

  printf("\nInitiating Control Sequence Writes.\n");
  count_iter = 0;
  fp = fopen("../sample_emails.txt", "r");
  begin = clock();
  while ((read = getline(&valueBuffer, &len, fp)) != -1) 
  {
    printPoll(count_iter);
    sprintf(keyBuffer,"%d",count_iter++);
  }
  end = clock();
  double time_spent_outside = (double)(end - begin) / CLOCKS_PER_SEC;
  fclose(fp);

  printf("\n For %d writes Total Time Taken:" ANSI_COLOR_RED "[%.3fs]" ANSI_COLOR_RESET ". removing additional [%fs] overhead.\n",count_iter,time_spent-time_spent_outside,time_spent_outside);
  printf("Time Taken per Write:" ANSI_COLOR_RED "[%fs]" ANSI_COLOR_RESET "\n",(time_spent-time_spent_outside)/count_iter);

  printf("Initiating DB Reads\n");
  begin = clock();
  for(i=0;i<count_iter;i++)
  {
    printPoll(i);
    sprintf(keyBuffer,"%d",i);
    get_from_dict_2(TEST_DB_NAME,TEST_DIR_NAME,keyBuffer,readBuffer,50);
  } 
  end = clock();
  time_spent = (double)(end - begin) / CLOCKS_PER_SEC;

  printf("Initiating DB Reads (Control)\n");
  begin = clock();
  for(i=0;i<count_iter;i++)
  {
    printPoll(i);
    sprintf(keyBuffer,"%d",i);
  }
  end = clock();
  time_spent_outside = (double)(end - begin) / CLOCKS_PER_SEC;

  printf("\nFor %d reads Total Time Taken:" ANSI_COLOR_RED "[%.3fs]" ANSI_COLOR_RESET ". removing additional [%fs] overhead..\n",count_iter,time_spent-time_spent_outside,time_spent_outside);
  printf("Time Taken per Read:" ANSI_COLOR_RED "[%fs]" ANSI_COLOR_RESET "\n",(time_spent-time_spent_outside)/count_iter);
}
Exemple #6
0
/** This function created multiple key value pairs and reads them and tests that they are okay **/
void testMultipleReadWrites()
{
  setup();

  /* initialize random seed: */
  srand (time(NULL));
  
  int i=0;
  const int count_iter =100000;
  const char ** keys = (const char **)(malloc(count_iter*(sizeof(char *))));
  const char ** keysX = (const char **)(malloc(count_iter*(sizeof(char *)))); // Only for testing
  char keyBuffer[100];
  char valueBuffer[100];
  char readBuffer[100];
  clock_t begin, end;

  begin = clock();
  for(i=0;i<count_iter;i++)
  {
    sprintf(keyBuffer,"Key%d",randomInt());
    sprintf(valueBuffer,"Value%d",randomInt());
    char * key = (char *)malloc(strlen(keyBuffer)+1);
    strcpy(key,keyBuffer);
    keys[i] = key;
  
    add_to_dict(TEST_DB_NAME,TEST_DIR_NAME,keyBuffer,valueBuffer);
  }
  end = clock();
  double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;

  begin = clock();
  for(i=0;i<count_iter;i++)
  {
    sprintf(keyBuffer,"Key%d",randomInt());
    sprintf(valueBuffer,"Value%d",randomInt());
    char * key = (char *)malloc(strlen(keyBuffer)+1);
    strcpy(key,keyBuffer);
    keysX[i] = key;
  }
  end = clock();
  double time_spent_outside = (double)(end - begin) / CLOCKS_PER_SEC;

  printf("For %d writes Total Time Taken:" ANSI_COLOR_RED "[%.3fs]" ANSI_COLOR_RESET ". removing additional [%fs] overhead.\n",count_iter,time_spent-time_spent_outside,time_spent_outside);
  printf("Time Taken per Write:" ANSI_COLOR_RED "[%fs]" ANSI_COLOR_RESET "\n",(time_spent-time_spent_outside)/count_iter);

  begin = clock();
  for(i=0;i<count_iter;i++)
  {
    get_from_dict_2(TEST_DB_NAME,TEST_DIR_NAME,keys[i],readBuffer,50);
  } 
  end = clock();
  time_spent = (double)(end - begin) / CLOCKS_PER_SEC;

  begin = clock();
  for(i=0;i<count_iter;i++)
  {
    keys[i] = "ds";
  }
  end = clock();
  time_spent_outside = (double)(end - begin) / CLOCKS_PER_SEC;

  printf("For %d reads Total Time Taken:" ANSI_COLOR_RED "[%.3fs]" ANSI_COLOR_RESET ". removing additional [%fs] overhead..\n",count_iter,time_spent-time_spent_outside,time_spent_outside);
  printf("Time Taken per Read:" ANSI_COLOR_RED "[%fs]" ANSI_COLOR_RESET "\n",(time_spent-time_spent_outside)/count_iter);
}