-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
108 lines (95 loc) · 2.97 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# coding=utf-8
import random
import momoko
import datetime
from random import randint
from tornado import httpserver, ioloop, web, gen
DATA_CHOICES = {
'professions': [
'web developer',
'doctor',
'teacher',
'engineer',
'entrepreneur'
],
'names': [
'Ivan',
'Sergey',
'Nina',
'Zina',
'Anna',
'Oxana',
'Vladimir',
'Alexander'
],
'hobbies': [
'guitar',
'games',
'snowboard',
'ski',
'reading books'
],
}
class SetupHandler(web.RequestHandler):
@gen.coroutine
def get(self):
sql = """
CREATE TABLE IF NOT EXISTS test_data (
id SERIAL PRIMARY KEY,
name varchar(80),
date_born date,
profession varchar(80),
hobby varchar(80)
);
"""
self.application.db.execute(sql)
yield [self.create_entry() for i in xrange(5000)]
self.write(u'Ладушки')
self.finish()
@gen.coroutine
def create_entry(self):
sql = """
INSERT INTO test_data (name, date_born, profession, hobby)
VALUES (%s, %s, %s, %s)
"""
name = random.choice(DATA_CHOICES['names'])
profession = random.choice(DATA_CHOICES['professions'])
hobby = random.choice(DATA_CHOICES['hobbies'])
date_born = datetime.datetime.now() - datetime.timedelta(days=randint(1, 365) * randint(18, 35))
yield momoko.Op(self.application.db.execute, sql, (name, date_born, profession, hobby))
class GetHandler(web.RequestHandler):
@gen.coroutine
def get(self, id):
simple = """
SELECT %s;
SELECT id, name, date_born, profession, hobby
FROM test_data LIMIT 10
"""
heavy = """
SELECT %s;
SELECT id, name, date_born, profession, hobby
FROM test_data ORDER BY random() LIMIT 1000
"""
print 'Start %s' % id
params = [(random.choice([heavy, simple]), i) for i in xrange(100)]
yield list(map(lambda p: self.make_request(*p), params))
print 'Finish %s' % id
self.finish()
@gen.coroutine
def make_request(self, sql, number):
cursor = yield momoko.Op(self.application.db.execute, sql, (number,))
self.write(u"Получен результат выполнения запроса: %s<br/>" % cursor.query)
self.flush()
class Application(web.Application):
def __init__(self):
handlers = [
(r"/setup/", SetupHandler),
(r"/(.*)/", GetHandler),
]
super(Application, self).__init__(handlers, debug=True)
dsn = "dbname=test user=test password=test host=localhost port=5432"
self.db = momoko.Pool(dsn=dsn, size=5)
if __name__ == "__main__":
server = httpserver.HTTPServer(Application())
server.listen(8080)
ioloop.IOLoop.instance().start()