RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
core/router/testing/augmentation_test.cc
00001 /* augmentation_test.cc
00002    Jeremy Barnes, 4 March 2012
00003    Copyright (c) 2012 Datacratic.  All rights reserved.
00004 
00005    Test for the augmentation functionality.
00006 */
00007 
00008 #define BOOST_TEST_MAIN
00009 #define BOOST_TEST_DYN_LINK
00010 
00011 #include <boost/test/unit_test.hpp>
00012 #include "jml/arch/format.h"
00013 #include "jml/utils/vector_utils.h"
00014 #include "jml/utils/testing/watchdog.h"
00015 #include "jml/arch/futex.h"
00016 #include "rtbkit/core/router/router.h"
00017 #include "rtbkit/plugins/bidding_agent/bidding_agent.h"
00018 #include "rtbkit/plugins/augmentor/augmentor_base.h"
00019 #include <boost/thread/thread.hpp>
00020 #include <ace/Signal.h>
00021 
00022 
00023 using namespace std;
00024 using namespace ML;
00025 using namespace Datacratic;
00026 using namespace RTBKIT;
00027 
00028 BOOST_AUTO_TEST_CASE( test_augmentation_no_augmentors )
00029 {
00030     Watchdog watchdog(10.0);
00031 
00032     auto proxies = std::make_shared<ServiceProxies>();
00033 
00034     AugmentationLoop loop(proxies, "augmentation");
00035 
00036     loop.init();
00037     loop.start();
00038 
00039     proxies->config->dump(cerr);
00040 
00041     auto agentConfig = std::make_shared<AgentConfig>();
00042     AgentConfig::AugmentationInfo info;
00043     info.name = "random";
00044     agentConfig->augmentations.push_back(info);
00045     
00046     PotentialBidder bidder;
00047     bidder.agent = "test";
00048     bidder.config = agentConfig;
00049 
00050     // Add the augmentor
00051     AugmentorBase augmentor("random", "random", proxies);
00052     augmentor.init();
00053 
00054     uint64_t numAugmented = 0;
00055 
00056     augmentor.onRequest = [&] (const AugmentationRequest & request)
00057         {
00058             ML::atomic_inc(numAugmented);
00059 
00060             AugmentationList result;
00061             result[AccountKey()].data[request.augmentor] = "hello";
00062             augmentor.respond(request, result);
00063         };
00064 
00065     augmentor.start();
00066 
00067     proxies->config->dump(cerr);
00068 
00069     ML::sleep(1.0);
00070 
00071     cerr << "starting auctions" << endl;
00072 
00073     //augmentor.configureAndWait();
00074 
00075 
00076     // Do some auctions as fast as we can manage them
00077     filter_istream auctions("rtbkit/core/router/testing/20000-datacratic-auctions.xz");
00078     
00079     uint64_t numStarted = 0, numFinished = 0;
00080 
00081     for (unsigned i = 0;  i < 100;  ++i) {
00082         string current;
00083         getline(auctions, current);
00084         
00085         Date start = Date::now();
00086         Date expiry = start.plusSeconds(0.05);
00087         
00088         std::shared_ptr<BidRequest> bidRequest
00089             (BidRequest::parse("datacratic", current));
00090         
00091         auto handleAuction = [&] (std::shared_ptr<Auction> auction)
00092             {
00093             };
00094         
00095         auto auction = std::make_shared<Auction>(handleAuction,
00096                                                    bidRequest,
00097                                                    current,
00098                                                    "datacratic", 
00099                                                    start, expiry);
00100 
00101         auto info = std::make_shared<AugmentationInfo>();
00102         info->auction = auction;
00103         info->potentialGroups.resize(1);
00104         info->potentialGroups[0].push_back(bidder);
00105     
00106         auto finished = [&] (std::shared_ptr<AugmentationInfo>)
00107             {
00108                 ML::atomic_inc(numFinished);
00109             };
00110 
00111         ML::atomic_inc(numStarted);
00112         loop.augment(info, Date::now().plusSeconds(0.005), finished);
00113     }
00114 
00115     cerr << "finished injecting auctions" << endl;
00116     
00117     loop.sleepUntilIdle();
00118 
00119     ML::sleep(0.1);
00120 
00121     cerr << "loop.numAugmenting() = " << loop.numAugmenting() << endl;
00122 
00123     loop.shutdown();
00124 
00125     cerr << "finished loop shutdown" << endl;
00126 
00127     augmentor.shutdown();
00128 
00129     cerr << "numStarted " << numStarted
00130          << " numFinished " << numFinished << endl;
00131 
00132     BOOST_CHECK_EQUAL(numStarted, numFinished);
00133     BOOST_CHECK_EQUAL(numAugmented, numFinished);
00134 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator