示例#1
0
文件: pricer.cpp 项目: runnig/pricer
	void add_order(timestamp_t timestamp, order_id_t id, side_t side,
			price_t price, int size)
	{
		assert(timestamp >= 0);
		assert(id >= 0);
		assert(price > (price_t) 0);
		assert(size >= 0);
		assert(side == m_side);

		// find() takes O(1) for hash tables
		order_table_iter_t pos = m_orders.find(id);

		if (pos != m_orders.end()) // order id already exists in the book
		{
			throw cant_add_order(timestamp, id,
					"order with the id already exists in the book");
		}
		order_ptr neword(new order(timestamp, side, price, size));

		m_orders[id] = neword; // operator [] takes O(1) amortized time for hash_table

		// O(N); N/2 on average
		insert_sorted(m_sorted_orders, neword);

		m_shares += size;
		m_last_timestamp = timestamp;

		if (m_orders.size() > m_max_orders)
			m_max_orders = m_orders.size();
		if (m_sorted_orders.size() > m_max_sorted)
			m_max_sorted = m_sorted_orders.size();
	}
示例#2
0
/*
 * prepare data and execute the new order transaction for one order
 * officially, this is supposed to be simulated terminal I/O
 */
static int do_neword (int t_num)
{
    int c_num;
    int i,ret;
    clock_t clk1,clk2;
    double rt;
    struct timespec tbuf1;
    struct timespec tbuf2;
    int  w_id, d_id, c_id, ol_cnt;
    int  all_local = 1;
    int  notfound = MAXITEMS+1;  /* valid item ids are numbered consecutively
				    [1..MAXITEMS] */
    int rbk;
    int  itemid[MAX_NUM_ITEMS];
    int  supware[MAX_NUM_ITEMS];
    int  qty[MAX_NUM_ITEMS];

    if(num_node==0){
	w_id = RandomNumber(1, num_ware);
    }else{
	c_num = ((num_node * t_num)/num_conn); /* drop moduls */
	w_id = RandomNumber(1 + (num_ware * c_num)/num_node,
			    (num_ware * (c_num + 1))/num_node);
    }
    d_id = RandomNumber(1, DIST_PER_WARE);
    c_id = NURand(1023, 1, CUST_PER_DIST);

    ol_cnt = RandomNumber(5, 15);
    rbk = RandomNumber(1, 100);

    for (i = 0; i < ol_cnt; i++) {
	itemid[i] = NURand(8191, 1, MAXITEMS);
	if ((i == ol_cnt - 1) && (rbk == 1)) {
	    itemid[i] = notfound;
	}
	if (RandomNumber(1, 100) != 1) {
	    supware[i] = w_id;
	}
	else {
	    supware[i] = other_ware(w_id);
	    all_local = 0;
	}
	qty[i] = RandomNumber(1, 10);
    }

    clk1 = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tbuf1 );
    for (i = 0; i < MAX_RETRY; i++) {
      ret = neword(t_num, w_id, d_id, c_id, ol_cnt, all_local, itemid, supware, qty);
      clk2 = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tbuf2 );

      if(ret){

	rt = (double)(tbuf2.tv_sec * 1000.0 + tbuf2.tv_nsec/1000000.0-tbuf1.tv_sec * 1000.0 - tbuf1.tv_nsec/1000000.0);
        //printf("NOT : %.3f\n", rt);
        if (freport_file != NULL) {
          fprintf(freport_file,"%d %.3f\n", time_count, rt);
        }

	if(rt > max_rt[0])
	  max_rt[0]=rt;
	hist_inc(0, rt);
	if(counting_on){
	  if( rt < RTIME_NEWORD ){
	    success[0]++;
	    success2[0][t_num]++;
	  }else{
	    late[0]++;
	    late2[0][t_num]++;
	  }
	}

	return (1); /* end */
      }else{

	if(counting_on){
	  retry[0]++;
	  retry2[0][t_num]++;
	}

      }
    }

    if(counting_on){
      retry[0]--;
      retry2[0][t_num]--;
      failure[0]++;
      failure2[0][t_num]++;
    }

    return (0);
}