Skip to content

Commit

Permalink
Merge pull request #55 from abushev/mock_exchange
Browse files Browse the repository at this point in the history
Mock exchange
  • Loading branch information
abushev authored Jul 13, 2017
2 parents f561f56 + 558b4eb commit 3a8fee0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/bidder/mock_exchange/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id": "ssp1-27009141-1485848975137","at": 2,"imp": [{"id": "1","bidfloor": 13.521766169371157,"bidfloorcur": "RUB","banner": {"pos": 0,"h": 100,"w": 300},"secure": 0}],"site": {"id": "10930","domain": "warfiles.ru","ref": "http://warfiles.ru/show-142725-rossiya-vpervye-ispytala-noveyshuyu-aviabombu-drel-v-sirii.html","publisher": {"id": "9886"},"ext": {"place_id": 79170},"cat": ["IAB12"]},"device": {"ua": "Mozilla/5.0 (iPad; CPU OS 9_3_5 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) YaBrowser/16.11.0.2708.11 Mobile/13G36 Safari/601.1","ip": "84.234.53.206","make": "Apple","model": "iPad","os": "iOS","osv": "9.0","devicetype": 5},"user": {"id": "35e4f8a5-e897-4589-a075-bc2c8acd7e1e","buyeruid": "6331632281203562848","geo": {"type": 2,"city": "Moscow","region": "MD-BD","country": "Russia"}}}
1 change: 1 addition & 0 deletions examples/bidder/mock_exchange/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
79 changes: 79 additions & 0 deletions examples/bidder/mock_exchange/mock-x.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python3
#encoding: UTF-8

#
# File: main.py
# Author: [email protected]
#
# Created on 13 июня 2017 г., 19:25
#

import requests
import threading
import signal
from time import sleep
import argparse
import sys

class Exchange:
def __init__(self, limit, url, files, timeout, concurrency):
self.exchange = True
self.limit = limit/concurrency
self.threads = []
self.concurrency = concurrency
self.lock = threading.Lock()
self.threads_finished = 0
self.exchange_data = []
self.url = url
self.timeout = timeout
for f in files:
with open(f, 'rb') as data_file:
self.exchange_data.append(data_file.read())

def run(self):
for c in range(0, self.concurrency):
self.threads.append(threading.Thread(target=self.run_thread))
for t in self.threads:
t.start()
while self.exchange:
sleep(0.01)

with self.lock:
if self.threads_finished == len(self.threads):
break
for t in self.threads:
t.join()

def run_thread(self):
session = requests.Session()

idx = 0
while self.exchange:
if self.limit > 0 and idx >= self.limit:
break
try:
r = session.post(self.url, data=self.exchange_data[idx%len(self.exchange_data)], timeout=self.timeout)
except requests.exceptions.Timeout:
print('Connection timeout Timeout occured')
except requests.exceptions.ConnectionError:
print('Connection failed')
break
idx += 1
with self.lock:
self.threads_finished += 1

def stop(self):
self.exchange = False

parser = argparse.ArgumentParser(description="""Mock exchange""")
parser.add_argument('--limit', help='total requests number, 0 for endless requests (default 0)', default=0)
parser.add_argument('--url', help='exchange url (default http://localhost:9081/bid/123)', default="http://localhost:9081/bid/123")
parser.add_argument('--requests', help='Stored bid requests "file1 [file2] ... [fileN] (default data.json)"', default="data.json")
parser.add_argument('--timeout', help='request timeout (default 0.1 sec)', default=0.1)
parser.add_argument('--concurrency', help='threads to execute (default 5)', default=5)
args = parser.parse_args()

exchange = Exchange(limit=int(args.limit), files=args.requests.split(" "), url=args.url, timeout=float(args.timeout), concurrency=int(args.concurrency))
signal.signal(signal.SIGINT, lambda s, f: exchange.stop())
exchange.run()

0 comments on commit 3a8fee0

Please sign in to comment.