I made two branches of my tornado app, one is sync version with pymongo, and the other is Async version with motor. Then I tested them with AB, and found the 'Async' version had bad performance compared to the 'Sync' version.
* The site is deployed on my local machine and only uses 1 process.
- This is the 'sync' version

-This is the 'Async' version

QPS: 14.5(Sync) > 4.13(Async)
Time per request: 68.984ms(Sync) < 242.28ms(Async)
This is the 'Sync' code:
class IndexHandler(BaseHandler):
# http://ip:port/
def get(self):
collection = 'show'
start_ts = helpers.today_start_timestamp()
end_ts = helpers.today_end_timestamp()
condition = {'push_time': {'$gte': start_ts, '$lt': end_ts}}
statistic = {
'total_show_number': self.db.show.count(),
'total_video_number': self.db.video.count(),
'total_push_show_number': self.db.show.find({'state': '1'}).count(),
'total_push_video_number': self.db.video.find({'state': '1'}).count(),
'today_show_number': self.db.show.find({'push_time': {'$gte': start_ts, '$lt': end_ts}}).count(),
'today_video_number': self.db.video.find({'push_time': {'$gte': start_ts, '$lt': end_ts}}).count(),
'today_push_show_number': self.db.show.find({'push_time': {'$gte': start_ts, '$lt': end_ts},
'state': '1'}).count(),
'today_push_video_number': self.db.video.find({'push_time': {'$gte': start_ts, '$lt': end_ts},
'state': '1'}).count(),
}
return self.paginate_render("index.html",
collection=collection,
condition=condition,
sort_key="push_time",
sort_direction=pymongo.DESCENDING,
statistic=statistic)
This is the 'Async' code:
class IndexHandler(BaseHandler):
PAGE_SIZE = 20
# http://ip:port/
@coroutine
def get(self):
start_ts = helpers.today_start_timestamp()
end_ts = helpers.today_end_timestamp()
shows = self.db.show.find({'push_time': {'$gte': start_ts, '$lt': end_ts}}).sort("push_time", -1)
show_info_list = yield helpers.format_show(shows)
total_show_number = yield self.db.show.count()
total_video_number = yield self.db.video.count()
total_push_show_number = yield self.db.show.find({'state': '1'}).count()
total_push_video_number = yield self.db.video.find({'state': '1'}).count()
today_show_number = yield self.db.show.find({'push_time': {'$gte': start_ts, '$lt': end_ts}}).count()
today_video_number = yield self.db.video.find({'push_time': {'$gte': start_ts, '$lt': end_ts}}).count()
today_push_show_number = yield self.db.show.find({'push_time': {'$gte': start_ts, '$lt': end_ts},
'state': '1'}).count()
today_push_video_number = yield self.db.video.find({'push_time': {'$gte': start_ts, '$lt': end_ts},
'state': '1'}).count()
statistic = {
'total_show_number': total_show_number,
'total_video_number': total_video_number,
'total_push_show_number': total_push_show_number,
'total_push_video_number': total_push_video_number,
'today_show_number': today_show_number,
'today_video_number': today_video_number,
'today_push_show_number': today_push_show_number,
'today_push_video_number': today_push_video_number,
}
page_size = self.PAGE_SIZE
page_current = self.get_arg("pg", 1, int)
item_total = today_show_number
if item_total % self.PAGE_SIZE:
page_total = item_total / self.PAGE_SIZE + 1
else:
page_total = item_total / self.PAGE_SIZE
self.render("index.html", shows=show_info_list, statistic=statistic, page_size=page_size,
page_current=page_current, page_total=page_total)