This repository has been archived by the owner on Aug 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bytegen.py
92 lines (74 loc) · 2.5 KB
/
bytegen.py
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
# neubot/bytegen_speedtest.py
#
# Copyright (c) 2012 Simone Basso <bassosimone@gmail.com>,
# NEXA Center for Internet & Society at Politecnico di Torino
#
# This file is part of Neubot <http://www.neubot.org/>.
#
# Neubot is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Neubot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Neubot. If not, see <http://www.gnu.org/licenses/>.
#
''' Generates bytes for the speedtest test '''
import getopt
import sys
if __name__ == '__main__':
sys.path.insert(0, '.')
from ..globals import RANDOMBLOCKS
from ..runtime import utils
PIECE_LEN = 262144
class BytegenSpeedtest(object):
''' Bytes generator for speedtest '''
def __init__(self, seconds, piece_len=PIECE_LEN):
''' Initializer '''
self.seconds = seconds
self.ticks = utils.ticks()
self.closed = False
self.piece_len = piece_len
def read(self, count=sys.maxint):
''' Read count bytes '''
if self.closed:
return ''
if count < self.piece_len:
raise RuntimeError('Invalid count')
diff = utils.ticks() - self.ticks
if diff < self.seconds:
data = RANDOMBLOCKS.get_block()[:self.piece_len]
length = '%x\r\n' % self.piece_len
vector = [ length, data, '\r\n' ]
else:
vector = [ '0\r\n', '\r\n' ]
self.closed = True
return ''.join(vector)
def close(self):
''' Close '''
self.closed = True
def main(args):
''' Main() function '''
try:
options, arguments = getopt.getopt(args[1:], 't:')
except getopt.error:
sys.exit('usage: neubot bytegen_speedtest [-t seconds]')
if arguments:
sys.exit('usage: neubot bytegen_speedtest [-t seconds]')
seconds = 5.0
for name, value in options:
if name == '-t':
seconds = float(value)
bytegen = BytegenSpeedtest(seconds)
while True:
data = bytegen.read()
if not data:
break
sys.stdout.write(data)
if __name__ == '__main__':
main(sys.argv)