8000 test file initial commit by rohith2506 · Pull Request #6 · cookingkode/woof · GitHub
[go: up one dir, main page]

Skip to content
59 changes: 59 additions & 0 deletions tests/test_transactions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import os
import logging
import time
import thread
from woof.transactions import TransactionLogger

logging.basicConfig(
format='%(asctime)s.%(msecs)s:%(name)s:%(thread)d:%(levelname)s:%(process)d:%(message)s',
filename='/tmp/kafkalog',
level=logging.INFO
)

logger = logging.getLogger('kafka')
logger.setLevel(logging.INFO)

srv = os.getenv("GOMSG_SRV", "localhost:9092")

stime = time.time()

# Instantiate
# Should be a long lived object
# async would be True for performance, if needed
# but in fringe cases if there is a restart, msg might not be deliverd
tr = TransactionLogger(srv, "dummy_vertical1", async=True)
print "Time taken for connection: ", time.time() - stime



def thread_test():
stime = time.time()
tr.New(txn_id="gofld3434",
amount=3500,
skus=["vcid_1", "vhid_1"],
detail="{'foo':'bar'}",
userid="rohith2506",
email="r1@gmail.com",
phone="8984758345345")
print "Time taken to send one message: ", time.time() - stime

# Modify
tr.Modify(txn_id="gofld3434",
amount=4000,
detail="{'foo':'bar', 'foo1':'bar1'}",
phone="8984758345345")

# Cancel
tr.Cancel(txn_id="gofld3434",
phone="8984758345345")

# Fulfil
tr.Fulfil(txn_id="gofld3434",
phone="8984758345345")

for i in range(10):
thread.start_new_thread(thread_test,())


# sleep to allow msg to go
time.sleep(60)
51 changes: 51 additions & 0 deletions woof/transactions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from woof.partitioned_producer import PartitionedProducer
import socket
import time
import logging
import threading

log = logging.getLogger("kafka")
woof_tls = threading.local()


class TransactionLogger(object):
def __init__(self, broker, vertical, host=socket.gethostname(), async=False):
self.broker = broker
self.this_host = host
self.vertical = vertical
self.async = async
self.topic = _get_topic_from_vertical(vertical)

def New(self, txn_id, amount, skus, detail="#", userid="#", email="#", phone="#"):
self._send_log("NEW", txn_id, amount, skus, detail, userid, email, phone)

def Modify(self, txn_id, amount="#", skus=[], detail="#", userid="#", email="#", phone="#"):
self._send_log("MODIFY", txn_id, amount, skus, detail, userid, email, phone)

def Cancel(self, txn_id, amount="#", skus=[], detail="#", userid="#", email="#", phone="#"):
self._send_log("CANCEL", txn_id, amount, skus, detail, userid, email, phone)

def Fulfil(self, txn_id, amount="#", skus=[], detail="#", userid="#", email="#", phone="#"):
self._send_log("FULFIL", txn_id, amount, skus, detail, userid, email, phone)

def _send_log(self, verb, txn_id, amount, skus, detail="#", userid="#", email="#", phone="#"):
msg = self._format_message(verb, txn_id, amount, skus, detail, userid, email, phone)
log.info("[transactions log] topic %s txnid %s msg %s \n", self.topic, txn_id, msg)
try:
woof_tls.producer.send(self.topic, txn_id, msg)
except AttributeError:
woof_tls.producer = PartitionedProducer(self.broker, async=self.async)
self._send_log(verb, txn_id, amount, skus, detail, userid, email, phone)

def _format_message(self, verb, txn_id, amount, skus, detail, userid, email, phone):
"""
Generates log message.
"""
separator = '\t'
skus_as_string = ",".join(skus)
return separator.join([self.this_host, str(time.time()), verb, txn_id,
str(amount), skus_as_string, detail, userid, email, phone])


def _get_topic_from_vertical(vertical):
return "_".join(["TRANSACTIONS", vertical])
0