add profiling code to test lru cache, remove js popup

This commit is contained in:
Maxime Bloch 2019-09-27 19:25:47 +02:00
parent c6e93f8c7f
commit 35bb6dd5fc
No known key found for this signature in database
GPG key ID: CE32A7D95B7D6418
3 changed files with 42 additions and 6 deletions

View file

@ -1,7 +1,13 @@
import asyncio
import atexit
import datetime
import socket import socket
from time import time from functools import lru_cache
from time import sleep, time
import matplotlib.pyplot as plt
import socketio import socketio
from apscheduler.schedulers.background import BackgroundScheduler
from flask import Flask, render_template, request from flask import Flask, render_template, request
sio = socketio.Server() sio = socketio.Server()
@ -19,6 +25,9 @@ app.wsgi_app = socketio.WSGIApp(sio, app.wsgi_app)
db = {"current_run": None, "run_start_timer": None, "run_data": {}} db = {"current_run": None, "run_start_timer": None, "run_data": {}}
function_times = []
function_data = []
@app.route("/") @app.route("/")
def index(): def index():
@ -39,19 +48,24 @@ def start_run(run_index):
@app.route("/link/start/<run>/<link_index>") @app.route("/link/start/<run>/<link_index>")
def link_start(run, link_index): def link_start(run, link_index):
start = time()
request_data = live_request(run, link_index) request_data = live_request(run, link_index)
sio.emit('live_request', request_data) sio.emit('live_request', request_data)
if db["current_run"] != run: if db["current_run"] != run:
return "Wrong run number, check that you update your run", 404 response = "Wrong run number, check that you update your run", 404
else: else:
run_data = db["run_data"][run]["data"] run_data = db["run_data"][run]["data"]
if link_index in run_data: if link_index in run_data:
return "you already started in this run. Ignoring this request." response = "you already started in this run. Ignoring this request."
else: else:
run_data[link_index] = {"id": link_index, "start": time()} run_data[link_index] = {"id": link_index, "start": time()}
sio.emit('link_start', run_data[link_index]) sio.emit('link_start', run_data[link_index])
return "Success." response = "Success."
stop = time()
function_data.append(stop - start)
function_times.append(len(function_times))
return response
@app.route("/link/handoff/<run>/<index>") @app.route("/link/handoff/<run>/<index>")
@ -78,10 +92,31 @@ def connect(sid, data):
def live_request(run, index): def live_request(run, index):
ip = request.remote_addr ip = request.remote_addr
request_data = {} request_data = {}
request_data["hostname"] = socket.gethostbyaddr(ip)[0] request_data["hostname"] = lookup_hostname(ip)
request_data["time"] = time() request_data["time"] = time()
return request_data return request_data
@lru_cache()
def lookup_hostname(ip):
return socket.gethostbyaddr(ip)[0].split(".")[0]
# def plot_graph():
# try:
# x = function_times
# y = function_data
# plt.plot(x, y)
# plt.show()
# except:
# pass
if __name__ == '__main__': if __name__ == '__main__':
# scheduler = BackgroundScheduler()
# scheduler.add_job(func=plot_graph, trigger="interval", seconds=5)
# scheduler.start()
app.run(host="0.0.0.0", debug=True) app.run(host="0.0.0.0", debug=True)
# shut down the scheduler when exiting the app
# atexit.register(lambda: scheduler.shutdown())

View file

@ -1,2 +1,4 @@
flask flask
python-socketio python-socketio
apscheduler
matplotlib

View file

@ -51,7 +51,6 @@ require(['jquery', 'socket.io'], function(jq, io) {
}); });
function addLiveRequest(request_host, request_time) { function addLiveRequest(request_host, request_time) {
alert("yes");
let hostname = request_host; let hostname = request_host;
let timesting = request_time.getHours() + ":" + request_time.getMinutes(); let timesting = request_time.getHours() + ":" + request_time.getMinutes();