Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

performance issues #37

Closed
outcast opened this issue May 29, 2012 · 2 comments
Closed

performance issues #37

outcast opened this issue May 29, 2012 · 2 comments

Comments

@outcast
Copy link

outcast commented May 29, 2012

Pleas take a look at the code below. This was my best guess. I am sure I am doing something wrong here but switching from pymongo to txmongo has caused a 95% decrease in performance. I am wondering if there is way to use the txmongo.MonoConnection object global so I only need to connect to mongo once. Sorry I am still a bit of a noob to python, twisted and mongo. but basic idea is store udp packet for later processing.


from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor,defer
import txmongo
import struct
import string

x = 0

class Collector(DatagramProtocol):
def init(self):
print "Initializing Collector";

@defer.inlineCallbacks
def datagramReceived(self, data, (host, port)):
    #print "Recieved Packet"
    ch = yield txmongo.MongoConnection()
    dbh = ch.flowpackets.packets
    #print "received %r from %s:%d  type:%s" % (data, host, port,type(data))
    #self.transport.write(data, (host, port))
    global x
    packet = dict()
    packet = { 'packet': data.encode('hex')}
    dbh.insert(packet)
    x += 1

reactor.listenUDP(2055, Collector())
print "Starting Reactor"
reactor.run()
print "Packet Count: ", x

@fiorix
Copy link
Collaborator

fiorix commented May 29, 2012

Perhaps that's because it's making a new connection to mongo on each udp received.
Consider something like the below, which will set up a mongo connection on demand on the first udp:

class Collector(DatagramProtocol):
def init(self):
self.db = None

@defer.inlineCallbacks                                                       
def datagramReceived(self, data, (host, port)):                              
    if self.db is None:                                                      
        self.db = yield txmongo.MongoConnection()                            

    # then use self.db for everything                                        
    # ...                                                                    

On 2012-05-29, at 1:23 PM, James Jones wrote:

from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor,defer
import txmongo
import struct
import string

x = 0

class Collector(DatagramProtocol):
def init(self):
print "Initializing Collector";

@defer.inlineCallbacks
def datagramReceived(self, data, (host, port)):
#print "Recieved Packet"
ch = yield txmongo.MongoConnection()
dbh = ch.flowpackets.packets
#print "received %r from %s:%d type:%s" % (data, host, port,type(data))
#self.transport.write(data, (host, port))
global x
packet = dict()
packet = { 'packet': data.encode('hex')}
dbh.insert(packet)
x += 1

reactor.listenUDP(2055, Collector())
print "Starting Reactor"
reactor.run()
print "Packet Count: ", x

Ship, ahoy! Hast seen the White Whale?

  • Cap'n Ahab

@outcast
Copy link
Author

outcast commented Jul 18, 2012

Sounds like a plan will try that.

@fiorix fiorix closed this as completed Aug 8, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants