TEST(size, Queue) { Queue<int> queue3(10); queue3.push(1); queue3.push(2); queue3.push(3); CHECK_EQUAL(3, queue3.size()); }
//============================================================================= //== Function to generate Poisson customers == //== - Random split to queue1() and queue2() == //============================================================================= void generate(double lambda, double mu) { double interarrival_time; // Interarrival time to next send double service_time; // Service time for this customer int select_queue = 1; // if 1 go to queue1 create("generate"); // Loop forever to create customers while(1) { // Pull an interarrival time and hold for it interarrival_time = exponential(1.0 / lambda); hold(interarrival_time); // Pull a service time //service_time = exponential(1.0 / mu); service_time = mu; // Send the customer to a randomly selected queue if (select_queue == 1) { queue1(service_time, clock); select_queue++; } else if (select_queue == 2) { queue2(service_time, clock); select_queue++; } else if (select_queue == 3) { queue3(service_time, clock); select_queue++; } else if (select_queue == 4) { queue4(service_time, clock); select_queue++; } else { queue5(service_time, clock); select_queue = 1; } } }
//============================================================================= //== Function to generate Poisson customers == //== - Random split to queue1() and queue2() == //============================================================================= void generate(double lambda, double mu) { double interarrival_time; // Interarrival time to next send double service_time; // Service time for this customer double rv; // Random Value int queue_len[5]; // Number of customers in system int ties[5]; // Tied queue values int select_q; // Queue Chosen int short_val; // Shortest Queue value int num_ties; // Number of ties int i; // Iteration value create("generate"); // Loop forever to create customers while(1) { // Pull an interarrival time and hold for it interarrival_time = exponential(1.0 / lambda); hold(interarrival_time); // Pull a service time //service_time = exponential(1.0 / mu); service_time = mu; // Get # customers in each system queue_len[0] = qlength(Server1) + num_busy(Server1); queue_len[1] = qlength(Server2) + num_busy(Server2); queue_len[2] = qlength(Server3) + num_busy(Server3); queue_len[3] = qlength(Server4) + num_busy(Server4); queue_len[4] = qlength(Server5) + num_busy(Server5); // Calculate shortest queue short_val = queue_len[0]; for(i=1; i<5; i++) { if(queue_len[i] < short_val) short_val = queue_len[i]; } // Determine ties num_ties = 0; for(i=0; i<5; i++) { if (short_val == queue_len[i]) { select_q = i; ties[num_ties] = i; num_ties++; } } // Randomly select queue if tie occurs if(num_ties > 1) { rv = uniform(0.0,(double)num_ties); for(i=1; i<=num_ties; i++) { if(rv <= i) { select_q = ties[i-1]; break; } } } // Send the customer to shortest queue if(select_q == 0) queue1(service_time, clock); else if(select_q == 1) queue2(service_time, clock); else if(select_q == 2) queue3(service_time, clock); else if(select_q == 3) queue4(service_time, clock); else queue5(service_time, clock); } }
int main(int argc, char* argv) { const int count = 100 * 10000; boost::asio::io_service ioService1; cr::network::AsyncQueue<int> queue1(ioService1); auto startTime = std::chrono::high_resolution_clock::now(); std::thread t1([&] { boost::asio::io_service::work work(ioService1); queue1.asyncPush(1, std::bind(&push, std::ref(queue1), count, std::placeholders::_1)); queue1.asyncPop(std::bind(&pop, std::ref(queue1), std::placeholders::_1, std::placeholders::_2)); ioService1.run(); }); t1.join(); //t2.join(); auto topTime = std::chrono::high_resolution_clock::now(); auto totalTime = std::chrono::duration_cast<std::chrono::microseconds>(topTime - startTime).count(); std::cout << "microseonds: " << totalTime / 1000 << "\n" << "message count: " << count << std::endl << "speed :" << 1.0 * totalTime / count << "\n"; boost::asio::io_service ioService2; boost::asio::io_service ioService3; cr::network::AsyncQueue<int, std::mutex> queue2(ioService2, ioService3); int count2 = count; for (int i = 0; i < 10; ++i) { boost::asio::spawn(ioService2, [&](boost::asio::yield_context yield) { boost::system::error_code ec; while (count2-- > 0) { queue2.asyncPush(1, yield[ec]); } queue2.getPushIoService().stop(); queue2.getPopIoService().stop(); }); } boost::asio::spawn(ioService3, [&](boost::asio::yield_context yield) { boost::system::error_code ec; while (!ec) { int element = queue2.asyncPop(yield[ec]); } }); auto startTime2 = std::chrono::high_resolution_clock::now(); std::thread t2([&] { boost::asio::io_service::work work(ioService2); ioService2.run(); }); std::thread t3([&] { boost::asio::io_service::work work(ioService3); ioService3.run(); }); t2.join(); t3.join(); auto stopTime2 = std::chrono::high_resolution_clock::now(); auto totalTime2 = std::chrono::duration_cast<std::chrono::microseconds>(stopTime2 - startTime2).count(); std::cout << "microseonds: " << totalTime2 / 1000 << "\n" << "message count: " << count << std::endl << "speed :" << 1.0 * totalTime2 / count << "\n"; boost::asio::io_service ioService4; cr::network::AsyncQueue<int, std::mutex> queue3(ioService4); boost::asio::spawn(ioService4, [&](boost::asio::yield_context yield) { boost::system::error_code ec; int element = 1; while (element != 0) { element = queue3.asyncPop(yield[ec]); } ioService4.stop(); }); auto startTime3 = std::chrono::high_resolution_clock::now(); std::thread t4([&] { boost::asio::io_service::work work(ioService4); ioService4.run(); }); for (int i = 1; i < count; ++i) { queue3.push(i); } queue3.push(0); t4.join(); auto stopTime3 = std::chrono::high_resolution_clock::now(); auto totalTime3 = std::chrono::duration_cast<std::chrono::microseconds>(stopTime3 - startTime3).count(); std::cout << "microseonds: " << totalTime3 / 1000 << "\n" << "message count: " << count << std::endl << "speed :" << 1.0 * totalTime3 / count << "\n"; }