-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulated_users.py
More file actions
96 lines (75 loc) · 2.42 KB
/
Copy pathsimulated_users.py
File metadata and controls
96 lines (75 loc) · 2.42 KB
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
import os
import sys
import time
import math
import random
import redis
import signal
import pycurl
import cStringIO
import threading
import subprocess
from random import randrange
class viewing_thread(threading.Thread):
def __init__(self, play_list):
threading.Thread.__init__(self)
self.play_list = play_list
def view_the_video(self, content, seg_num):
host_ip = '192.168.0.9'
resolutions = ['1280_720', '854_480', '640_360', '426_240']
seg_num = int(seg_num)
for i in range(seg_num):
num = randrange(4)
res = resolutions[num]
url = "http://" + host_ip + "/" + \
str(content) + '_' + res + '_df_' + str(i) + '.ts'
buf = cStringIO.StringIO()
c = pycurl.Curl()
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.setopt(c.URL, url)
c.setopt(c.WRITEFUNCTION, buf.write)
c.perform()
buf.close()
p = math.exp((-4.6) * (i + 1) / seg_num)
r = random.random()
if r > p:
break
def viewing_proc(self, content, seg_num, req_rat):
print "content id:", content
print "segment number:", seg_num
print "request rate:", req_rat
p = int(req_rat) / 30.0 / 24.0 / 6.0
if p > 1:
times = int(round(p))
for i in range(times):
self.view_the_video(content, seg_num)
else:
r = random.random()
if r < p:
self.view_the_video(content, seg_num)
def run(self):
r = redis.Redis(host='192.168.0.9', port=6379, db=0)
while True:
for content in self.play_list:
cmd = 'seg_num_' + str(content)
seg_num = r.get(cmd)
cmd = 'req_rate_' + str(content)
req_rat = r.get(cmd)
self.viewing_proc(content, seg_num, req_rat)
time.sleep(3)
r = redis.Redis(host='192.168.0.9', port=6379, db=0)
content_num = r.get("content_num")
content_num = int(content_num)
thread_num = 1
threads = []
try:
for i in range(0, thread_num):
num_per_thread = content_num / thread_num
play_list = range(num_per_thread * i, num_per_thread * (i + 1) )
t = viewing_thread(play_list)
t.start()
threads.append(t)
except:
print "Error: unable to start thread"
for t in threads:
t.join()