comparison simulator/simulator.py @ 48:12d6fd69d166

added simulator
author gyorgyf
date Thu, 14 May 2015 12:31:39 +0100
parents
children
comparison
equal deleted inserted replaced
37:2db17c224664 48:12d6fd69d166
1 #!/usr/bin/env python
2 # encoding: utf-8
3 """
4 simulator.py
5
6 Created by George Fazekas on 2013-08-08.
7 Copyright (c) 2013 . All rights reserved.
8 """
9
10 import sys,os,time
11 import httplib as ht
12 import urllib
13 import random as r
14 import datetime as dt
15
16 # IP = "127.0.0.1:8030"
17 IP = "138.37.95.215" # this is the IP of kakapo<=>golden
18
19 HTTP_TIMEOUT = 3
20
21 class DataPoint(object):
22
23 def __init__(self,x,y,ts):
24 self.x = x
25 self.y = y
26 self.ts = ts
27
28 def __repr__(self):
29 return "DataPoint(%f,%f,%s)" %(self.x,self.y,str(self.ts))
30
31
32 class Simulator(object):
33
34 def __init__(self):
35 self.connect()
36 self.data = []
37 self.first_date = None
38 pass
39
40 def connect(self):
41 self.conn = ht.HTTPConnection(IP,timeout=HTTP_TIMEOUT)
42
43 def read_logdata(self,file):
44 '''Read the log file'''
45 with open(file,'r') as fh:
46 lines = fh.readlines()[1000:]
47 for line in lines :
48 # x,y,ts = self.parse_logline(line)
49 x,y,ts = self.parse_logline_newformat(line)
50 self.data.append(DataPoint(x,y,ts))
51 pass
52
53 def parse_logline_newformat(self,line):
54 '''parse a single line of the logfile and convert it to the appropriate datatypes'''
55 x,y,ts = 0,0,0
56 date,rest = line.split("]")
57 h,m,s = map(lambda x: int(x.strip()), date.split(":")[-3:])
58 if self.first_date == None:
59 self.first_date = dt.datetime(1,1,1,h,m,s)
60 ts = dt.datetime(1,1,1,h,m,s) - self.first_date
61 ip,x,y,t = map(lambda x: x.strip(), rest.strip().split(","))
62 # print ts,ip,x,y
63 return float(x),float(y),ts
64
65
66 def parse_logline(self,line):
67 '''parse a single line of the logfile and convert it to the appropriate datatypes'''
68 x,y,ts = 0,0,0
69 date,rest = line.split("]")
70 h,m,s = map(lambda x: int(x.strip()), date.split(":")[-3:])
71 if self.first_date == None:
72 self.first_date = dt.datetime(1,1,1,h,m,s)
73 ts = dt.datetime(1,1,1,h,m,s) - self.first_date
74 ip,x,y, = map(lambda x: x.strip(), rest.split("-"))
75 # print ts,ip,x,y
76 return float(x),float(y),ts
77
78 def yield_data(self):
79 for d in self.data :
80 yield d
81
82 def simulate(self):
83 deltas = map(lambda x:(x[1].ts-x[0].ts), zip(self.data,self.data[1:]))
84 # deltas = map(lambda x: x.seconds+(x.microseconds/1000000.0), deltas)
85 deltas = map(lambda x: x.seconds+r.random(), deltas)
86 # deltas = map(lambda x: x.seconds, deltas)
87 datagen = self.yield_data()
88 for d in deltas :
89 datapoint = datagen.next()
90 print datapoint
91 self.send_coordinate(datapoint.x, datapoint.y)
92 time.sleep(d)
93 pass
94
95 def random(self):
96 '''Send random numbers'''
97 while True :
98 self.send_coordinate(r.random(),r.random())
99 time.sleep(0.3)
100
101 def send_coordinate(self,x,y):
102 self.conn.putrequest("GET","/moodconductor/mood?x=%(x)s&y=%(y)s" %locals(), skip_host=True)
103 self.conn.putheader("Host", "www.isophonics.net")
104 self.conn.endheaders()
105 res = self.conn.getresponse()
106 res.read()
107 print res.status, res.reason
108 print res.read()
109
110
111 def main():
112
113 s = Simulator()
114 # s.random()
115 # s.read_logdata("../performance2.log")
116 s.read_logdata("../performance-09Aug2013Barbican.log")
117 s.simulate()
118
119
120
121 if __name__ == '__main__':
122 main()
123