~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to files/dcctest.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2009-09-14 09:00:29 UTC
  • mfrom: (1.1.4 upstream) (3.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090914090029-uval0ekt72kmklxw
Tags: 0.9.1.3-3
Changed dependency on python-setuptools to python-pkg-resources
(Closes: #546435) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
#
3
 
# copy this script to the botdir,edit and run it
4
 
# example: dcctest.py 10 0 .. this starts 10 test clients and sleep 0 sec
5
 
# between commands
6
 
 
7
 
__copyright__ = 'this file is in the public domain'
8
 
 
9
 
# vars
10
 
channel = '#dunkbots'
11
 
botnick = 'gozerbot'
12
 
mynick = 'test'
13
 
listenip = '127.0.0.1'
14
 
server = 'localhost'
15
 
port = 50002
16
 
 
17
 
# imports
18
 
from gozerbot.generic import handle_exception, rlog, enable_logging, setdefenc
19
 
from  gozerbot.config import config
20
 
enable_logging()
21
 
rlog(100, 'dcctest', 'starting...')
22
 
config['loglevel'] = 10
23
 
from gozerbot.plugins import plugins
24
 
from gozerbot.examples import examples
25
 
import gozerbot.generic
26
 
import gozerbot.bot
27
 
import gozerbot.irc
28
 
import sys, time, socket, thread, struct, os, random, signal
29
 
setdefenc('utf-8')
30
 
nrloops = 10
31
 
commandsdone = 0
32
 
starttime = time.time()
33
 
 
34
 
if not len(sys.argv) ==  4:
35
 
    print 'i need three  arguments: 1) nrloops 2) nrclients 3) timetosleep'
36
 
    os._exit(0)
37
 
else:
38
 
    nrloops = int(sys.argv[1])
39
 
    nrbots = int(sys.argv[2])
40
 
    timetosleep = float(sys.argv[3])
41
 
 
42
 
def stop(x, y):
43
 
    print str(commandsdone/(time.time()-starttime)) + " commands per second"
44
 
    gozerbot.generic.die()
45
 
 
46
 
# register SIGTERM handler to stop
47
 
signal.signal(signal.SIGTERM, stop)
48
 
signal.signal(signal.SIGINT, stop)
49
 
 
50
 
plugins.regplugins()
51
 
 
52
 
def listen():
53
 
    try:
54
 
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
55
 
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
56
 
        s.bind((listenip, int(port)))
57
 
        s.listen(1)
58
 
    except Exception,e:
59
 
        handle_exception()
60
 
        os._exit(1)
61
 
    s.setblocking(1)
62
 
    teller = 0
63
 
    while(1):
64
 
        time.sleep(0.1)
65
 
        try:
66
 
            insocket, addr = s.accept()
67
 
        except Exception, ex:
68
 
            handle_exception()
69
 
            os._exit(1)
70
 
        thread.start_new_thread(serve, (insocket, ))
71
 
        teller += 1
72
 
        thread.start_new_thread(dodcctest, ('test' + str(teller), \
73
 
insocket, nrloops))
74
 
 
75
 
 
76
 
def serve(insocket):
77
 
    global commandsdone
78
 
    f = insocket.makefile()
79
 
    while(1):
80
 
        try:
81
 
            r = f.readline()
82
 
            if not r:
83
 
                break
84
 
            rlog(100, 'DCC', r.strip())
85
 
            commandsdone += 1
86
 
        except Exception, ex:
87
 
            handle_exception()
88
 
            return
89
 
 
90
 
donot = ['reboot', 'cycle', 'loglevel', 'quit', 'email', 'meet', 'nick', \
91
 
'part', 'cc', 'chat', 'join', ' nick', 'update', 'install', \
92
 
'reconnect', 'jump', 'nonfree', 'relay', 'rss', 'fleet', 'sendraw', \
93
 
'upgrade', 'alarm', 'remind', 'intro', 'host', 'ip', 'alarm', 'tests', \
94
 
'unload', 'delete', 'dfwiki', 'dig', 'silent', 'reconnect', 'switch', 'op',
95
 
'dict', 'slashdot', 'films', 'latest', 'weather', 'coll', 'web', 'mail', \
96
 
'markov', 'probe', 'sc', 'log', 'validate']
97
 
 
98
 
def dodcctest(name, insocket, nrloop):
99
 
    a = examples.getexamples()
100
 
    teller = 0
101
 
    while 1:
102
 
        nrloop -= 1
103
 
        if nrloop == 0:
104
 
            break
105
 
        random.shuffle(a)
106
 
        for z in a:
107
 
            teller += 1
108
 
            no = 0
109
 
            for zz in donot:
110
 
                if z.find(zz) != -1:
111
 
                    no = 1
112
 
                    break
113
 
            if no:
114
 
                continue
115
 
            rlog(100, '%s %s-%s' % (name, nrloop, teller), 'sending ' \
116
 
+ str(z))
117
 
            insocket.send('!' + str(z) + ' chan ' + channel + '\n')
118
 
        teller = 0
119
 
 
120
 
thread.start_new_thread(listen, ())
121
 
 
122
 
def startirc(nr):
123
 
    testnick = mynick + str(nr)
124
 
    #i = gozerbot.bot.Bot('test@test', 'test')
125
 
    i = gozerbot.irc.Irc()
126
 
    try:
127
 
        i.connect(testnick, server, 6667)
128
 
    except:
129
 
        return
130
 
    ipip2 = socket.inet_aton(listenip)
131
 
    ipip = struct.unpack('>L', ipip2)[0]
132
 
    zz = 'DCC CHAT CHAT %s %s' % (ipip, port)
133
 
    i.ctcp(botnick, zz)
134
 
 
135
 
for i in range(nrbots):
136
 
    thread.start_new_thread(startirc, (i, ))
137
 
    time.sleep(timetosleep)
138
 
 
139
 
while(1):
140
 
    try:
141
 
        time.sleep(1)
142
 
    except:
143
 
        stop(0,0)