use of com.xrtb.common.HttpPostGet in project XRTB by benmfaul.
the class TestValidBids method testNexageEncoding.
@Test
public void testNexageEncoding() throws Exception {
HttpPostGet http = new HttpPostGet();
String bid = Charset.defaultCharset().decode(ByteBuffer.wrap(Files.readAllBytes(Paths.get("./SampleBids/nexage.txt")))).toString();
String s = null;
long time = 0;
String xtime = null;
try {
try {
time = System.currentTimeMillis();
s = http.sendPost("http://" + Config.testHost + "/rtb/bids/nexage", bid, 30000, 30000);
time = System.currentTimeMillis() - time;
xtime = http.getHeader("X-TIME");
} catch (Exception error) {
fail("Can't connect to test host: " + Config.testHost);
}
assertNotNull(s);
int index = s.indexOf("%3C");
assertTrue(index != -1);
} catch (Exception e) {
e.printStackTrace();
fail(e.toString());
}
}
use of com.xrtb.common.HttpPostGet in project XRTB by benmfaul.
the class GeoStuff method main.
public static void main(String[] args) throws Exception {
BufferedReader br = null;
int percentWin = 80;
int pixelPercent = 90;
int clickPercent = 3;
boolean forever = false;
String fileName = "SampleBids/nexage.txt";
String HOST = "localhost:8080";
ObjectMapper mapper = new ObjectMapper();
int numberOfBids = 1000000;
boolean silent = false;
int sleepTime = 0;
String geoFile = "data/unique_geo_zipcodes.txt";
COUNT_MODE = true;
int i = 0;
while (i < args.length) {
switch(args[i]) {
case "-h":
System.out.println("-file <filename> [Set the file of the bid request(s) for the bidder, default SampleBids/nexage.txt]");
System.out.println("-host <host:port> [Set the host:port for the bidder, default localhost:8080 ]");
System.out.println("-geo filename [Sets the geo zip code file, default data/unique_geo_zipcodes.txt ]");
System.out.println("-ssp \"ssp1, ...\"[Sets the ssp names to use. ]");
System.out.println("-win <n> [Set the percentage to win, default 80 ]");
System.out.println("-pixel <n> [Set the percentage to pixel fires, default 90 ]");
System.out.println("-click <n> [Set the percentage to clicks, default 3 ]");
System.out.println("-silent [Don't show the transactions, default is to show them. ]");
System.out.println("-count <n> [Number of times to execute, default 1000000 ]");
System.out.println("-forever [loop on end of file on the file ]");
case "-file":
fileName = args[i + 1];
i += 2;
break;
case "-geo":
geoFile = args[i + 1];
i += 2;
break;
case "-ssp":
String temp = args[i + 1];
exchanges.clear();
String[] arr = temp.split(",");
for (String s : arr) {
exchanges.add(s.trim());
}
i += 2;
break;
case "-host":
HOST = args[i + 1];
i += 2;
break;
case "-win":
percentWin = Integer.parseInt(args[i + 1]);
i += 2;
break;
case "-sleep":
sleepTime = Integer.parseInt(args[i + 1]);
i += 2;
break;
case "-count":
numberOfBids = Integer.parseInt(args[i + 1]) + 1;
COUNT_MODE = true;
i += 2;
break;
case "-replay":
COUNT_MODE = false;
i++;
break;
case "-silent":
silent = true;
i++;
break;
case "-pixel":
pixelPercent = Integer.parseInt(args[i + 1]);
i += 2;
break;
case "-click":
clickPercent = Integer.parseInt(args[i + 1]);
i += 2;
break;
case "-forever":
forever = true;
i++;
break;
default:
System.err.println("HUH: " + args[i]);
System.exit(1);
}
}
loadGeo(geoFile);
HttpPostGet post = new HttpPostGet();
String data = null;
if (COUNT_MODE)
data = new String(Files.readAllBytes(Paths.get(fileName)), StandardCharsets.UTF_8);
else
br = new BufferedReader(new FileReader(fileName));
winnah = winnah.replaceAll("__HOST__", HOST);
String bidURL = "http://" + HOST + "/rtb/bids/__EXCHANGE__";
String winURL = "http://" + HOST + "/rtb/win/__EXCHANGE__/";
String pixelURL = "http://" + HOST + "/" + pixel;
String redirectURL = "http://" + HOST + ":8080" + redirect;
redirectURL = redirectURL.replaceAll("__HOST__", HOST);
int requests = 0, error = 0, bids = 0, wins = 0, pixels = 0, clicks = 0, nobid = 0;
BigDecimal bidCost = new BigDecimal(0), winCost = new BigDecimal(0);
Random r = new Random();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
i = 0;
boolean running = true;
while (running == true) {
i++;
if (COUNT_MODE) {
if (i >= numberOfBids) {
running = false;
break;
}
} else {
data = br.readLine();
if (data == null) {
if (forever) {
// start over
br = new BufferedReader(new FileReader(fileName));
data = br.readLine();
} else {
// else all done
running = false;
break;
}
}
}
// System.out.println("----->" + data);
Map map = mapper.readValue(data, Map.class);
Map rets = randomize(map);
String id = null;
boolean win = cointoss(percentWin);
String thisBidUrl = bidURL;
String thisWinUrl = winURL;
String thisPixelURL = pixelURL;
String exchange = getExchange();
thisBidUrl = thisBidUrl.replaceAll("__EXCHANGE__", exchange);
thisWinUrl = thisWinUrl.replaceAll("__EXCHANGE__", exchange);
if (COUNT_MODE) {
//map.put("id", UUID.randomUUID().toString());
id = Integer.toString(requests);
map.put("id", id);
}
String bid = mapper.writeValueAsString(map);
if (!silent)
System.out.print(i + " --->");
String hisBid = null;
try {
hisBid = post.sendPost(thisBidUrl, bid, 250, 250);
if (sleepTime > 0)
Thread.sleep(sleepTime);
requests++;
double x = 0;
Thread.sleep(1);
if (!silent)
System.out.print("<---");
Map theBid = null;
if (hisBid != null) {
Map bidmap = mapper.readValue(hisBid, Map.class);
List list = (List) bidmap.get("seatbid");
bidmap = (Map) list.get(0);
list = (List) bidmap.get("bid");
theBid = (Map) list.get(0);
x = (Double) theBid.get("price");
bidCost = bidCost.add(new BigDecimal(x));
bids++;
} else
nobid++;
if (win && hisBid != null) {
wins++;
//(String)map.get("cost");
String str = "" + (x * .85);
BigDecimal wc = new BigDecimal(str);
String theWin = makeWin(map, theBid, rets, wc.doubleValue());
if (!silent)
System.out.print("W-->");
String rc = post.sendGet(thisWinUrl + theWin, 5000, 5000);
if (!silent)
System.out.println(rc);
winCost = winCost.add(wc);
Thread.sleep(1);
String crid = (String) map.get("crid");
String adid = (String) map.get("adid");
//String cost = (String) map.get("cost");
String bidid = (String) rets.get("uuid");
String lat = "" + rets.get("lat");
String lon = "" + rets.get("lon");
if (cointoss(pixelPercent)) {
thisPixelURL = thisPixelURL.replaceAll("__EXCHANGE__", exchange);
thisPixelURL = thisPixelURL.replaceAll("__ADID__", adid);
thisPixelURL = thisPixelURL.replaceAll("__CRID__", crid);
thisPixelURL = thisPixelURL.replaceAll("__COST__", str);
thisPixelURL = thisPixelURL.replaceAll("__BIDID__", bidid);
thisPixelURL = thisPixelURL.replaceAll("__LAT__", lat);
thisPixelURL = thisPixelURL.replaceAll("__LON__", lon);
rc = post.sendGet(thisPixelURL, 5000, 5000);
pixels++;
}
if (cointoss(clickPercent)) {
String thisRedirect = redirectURL.replaceAll("__ADID__", adid);
thisRedirect = thisRedirect.replaceAll("__BIDID__", bidid);
thisRedirect = thisRedirect.replaceAll("__CRID__", crid);
thisRedirect = thisRedirect.replaceAll("__EXCHANGE__", exchange);
thisRedirect = thisRedirect.replaceAll("__COST__", "price=" + str);
thisRedirect = thisRedirect.replaceAll("__LAT__", "lat=" + lat);
thisRedirect = thisRedirect.replaceAll("__LON__", "lon=" + lon);
thisRedirect = thisRedirect.replace("8080:8080", "8080");
rc = post.sendGet(thisRedirect, 5000, 5000);
clicks++;
}
} else {
if (!silent)
System.out.println(".");
}
} catch (Exception err) {
if (!silent)
err.printStackTrace();
error++;
}
// pixel load simulation here
// click test here
}
System.out.println("Requests = " + requests);
System.out.println("Bids = " + bids);
System.out.println("Nobids = " + nobid);
System.out.println("Wins = " + wins);
System.out.println("Pixels = " + pixels);
System.out.println("Clicks = " + clicks);
System.out.println("BID COST = " + bidCost);
System.out.println("WIN COST = " + winCost);
System.out.println("Errors: " + error);
}
use of com.xrtb.common.HttpPostGet in project XRTB by benmfaul.
the class TestAdx method testAdx.
/**
* Test a valid bid response.
*
* @throws Exception
* on networking errors.
*/
@Test
public void testAdx() throws Exception {
BufferedReader br = new BufferedReader(new FileReader("SampleBids/adxrequests"));
String data;
ObjectMapper mapper = new ObjectMapper();
HttpPostGet http = new HttpPostGet();
while ((data = br.readLine()) != null) {
Map map = mapper.readValue(data, Map.class);
String protobuf = (String) map.get("protobuf");
if (protobuf != null) {
byte[] protobytes = DatatypeConverter.parseBase64Binary(protobuf);
InputStream is = new ByteArrayInputStream(protobytes);
byte[] returns = http.sendPost("http://" + Config.testHost + "/rtb/bids/adx", protobytes);
// AdxBidResponse resp = new AdxBidResponse(returns);
// System.out.println(resp.toString());
/* try {
AdxBidRequest bidRequest = new AdxBidRequest(is);
System.out.println(bidRequest.internal);
System.out.println("============================================");
System.out.println(bidRequest.root);
System.out.println("--------------------------------------------");
} catch (Exception error) {
error.printStackTrace();
} */
}
}
}
use of com.xrtb.common.HttpPostGet in project XRTB by benmfaul.
the class TestValidBids method testBannerRespondWithBid.
/**
* Test a valid bid response.
*
* @throws Exception
* on networking errors.
*/
@Test
public void testBannerRespondWithBid() throws Exception {
HttpPostGet http = new HttpPostGet();
String s = Charset.defaultCharset().decode(ByteBuffer.wrap(Files.readAllBytes(Paths.get("./SampleBids/nexage.txt")))).toString();
long time = 0;
try {
http.sendPost("http://" + Config.testHost + "/rtb/bids/nexage", s);
} catch (Exception error) {
fail("Network error");
}
String xtime = null;
try {
s = Charset.defaultCharset().decode(ByteBuffer.wrap(Files.readAllBytes(Paths.get("./SampleBids/nexage.txt")))).toString();
try {
time = System.currentTimeMillis();
s = http.sendPost("http://" + Config.testHost + "/rtb/bids/nexage", s);
time = System.currentTimeMillis() - time;
xtime = http.getHeader("X-TIME");
} catch (Exception error) {
fail("Can't connect to test host: " + Config.testHost);
}
Map m = null;
try {
m = DbTools.mapper.readValue(s, Map.class);
} catch (Exception error) {
fail("Bad JSON for bid");
}
List list = (List) m.get("seatbid");
m = (Map) list.get(0);
assertNotNull(m);
String test = (String) m.get("seat");
assertTrue(test.equals("99999999"));
list = (List) m.get("bid");
assertEquals(list.size(), 1);
m = (Map) list.get(0);
assertNotNull(m);
test = (String) m.get("impid");
assertTrue(test.equals("1"));
test = (String) m.get("id");
assertTrue(test.equals("35c22289-06e2-48e9-a0cd-94aeb79fab43") || test.equals("123"));
Double d = (Double) m.get("price");
assertNotNull(d);
test = (String) m.get("adid");
assertTrue(test.equals("ben:payday"));
test = (String) m.get("cid");
assertTrue(test.equals("ben:payday"));
list = (List) m.get("adomain");
test = (String) list.get(0);
assertTrue(test.equals("originator.com"));
System.out.println("XTIME: " + xtime);
System.out.println("RTTIME: " + time);
System.out.println(s);
assertTrue(s.contains("nurl"));
assertTrue(s.contains("cid"));
assertTrue(s.contains("iurl"));
} catch (Exception e) {
e.printStackTrace();
fail(e.toString());
}
}
use of com.xrtb.common.HttpPostGet in project XRTB by benmfaul.
the class TestValidBids method testNativeContentStreaming.
/**
* Test Native App Wall content advertising
*
* @throws Exception
* on network and configuration errors/
*/
@Test
public void testNativeContentStreaming() throws Exception {
HttpPostGet http = new HttpPostGet();
String bid = Charset.defaultCharset().decode(ByteBuffer.wrap(Files.readAllBytes(Paths.get("./SampleBids/nexageNativeContentStreamWithVideo.txt")))).toString();
String s = null;
long time = 0;
/******** Make one bid to prime the pump */
try {
http.sendPost("http://" + Config.testHost + "/rtb/bids/nexage", bid, 300000, 300000);
} catch (Exception error) {
fail("Network error");
}
/*********************************/
String xtime = null;
try {
try {
time = System.currentTimeMillis();
s = http.sendPost("http://" + Config.testHost + "/rtb/bids/nexage", bid, 300000, 300000);
time = System.currentTimeMillis() - time;
xtime = http.getHeader("X-TIME");
} catch (Exception error) {
fail("Can't connect to test host: " + Config.testHost);
}
assertNotNull(s);
System.out.println(s + "\n----------");
Map m = null;
try {
m = DbTools.mapper.readValue(s, Map.class);
} catch (Exception error) {
fail("Bad JSON for bid");
}
List list = (List) m.get("seatbid");
m = (Map) list.get(0);
assertNotNull(m);
String test = (String) m.get("seat");
assertTrue(test.equals("99999999"));
list = (List) m.get("bid");
assertEquals(list.size(), 1);
m = (Map) list.get(0);
assertNotNull(m);
test = (String) m.get("impid");
assertTrue(test.equals("39c22289-06e2-48e9-a0cd-94aeb79fab43-1=3"));
Double d = (Double) m.get("price");
assertTrue(d == 10.5);
test = (String) m.get("adid");
assertTrue(test.equals("ben:payday"));
test = (String) m.get("cid");
assertTrue(test.equals("ben:payday"));
test = (String) m.get("crid");
assertTrue(test.equals("iAmStreamingContentVideo"));
list = (List) m.get("adomain");
test = (String) list.get(0);
assertTrue(test.equals("originator.com"));
System.out.println("XTIME: " + xtime);
System.out.println("RTTIME: " + time);
System.out.println(s);
assertFalse(s.contains("pub"));
assertFalse(s.contains("ad_id"));
assertFalse(s.contains("bid_id"));
assertFalse(s.contains("site_id"));
/**
* Remove the layout in the request, it should still bid
*/
Map map = DbTools.mapper.readValue(bid, Map.class);
list = (List) map.get("imp");
Map sub = (Map) list.get(0);
sub = (Map) sub.get("native");
sub.remove("layout");
s = DbTools.mapper.writer().withDefaultPrettyPrinter().writeValueAsString(map);
try {
time = System.currentTimeMillis();
s = http.sendPost("http://" + Config.testHost + "/rtb/bids/nexage", s, 3000000, 3000000);
time = System.currentTimeMillis() - time;
xtime = http.getHeader("X-TIME");
} catch (Exception error) {
fail("Can't connect to test host: " + Config.testHost);
}
assertNotNull(s);
} catch (Exception e) {
e.printStackTrace();
fail(e.toString());
}
}
Aggregations