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