1# Copyright (c) 2012 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4import time
5
6def web_socket_do_extra_handshake(request):
7    # This example handler accepts any request. See origin_check_wsh.py for how
8    # to reject access from untrusted scripts based on origin value.
9
10    pass  # Always accept.
11
12def make_slice(start_time, elapsed_time, label, last):
13    out = []
14    out.append("{")
15    out.append('\"l\": \"{0}\",'.format(label))
16    out.append('\"s\": {0},'.format(start_time))
17    out.append('\"e\": {0}'.format(start_time+elapsed_time))
18    out.append('}')
19    if last == False:
20        out.append(',')
21    return ''.join(out)
22
23def make_thread(thread_name, base_time):
24    out = []
25    out.append('\"n\": \"{0}\",'.format(thread_name))
26    out.append('\"s\": [')
27    out.append(make_slice(base_time, 4, "alligator", False))
28    out.append(make_slice(base_time+2, 1, "bandicoot", False))
29    out.append(make_slice(base_time+5, 1, "cheetah", True))
30    out.append(']')
31    return ''.join(out)
32
33def make_thread_payload(pid, thread_name, base_time):
34    out = []
35    out.append('\"pid\": \"{0}\",'.format(pid))
36    out.append('\"td\": {')
37    out.append(make_thread(thread_name, base_time))
38    out.append('}')
39    return ''.join(out)
40
41def make_thread_command(base_time, thread_name):
42    out = []
43    out.append('{ \"cmd\": \"ptd\",')
44    out.append(make_thread_payload('314159', thread_name,  base_time))
45    out.append('}')
46    return ''.join(out)
47
48def make_count(start_time, value, last):
49    out = []
50    out.append('{')
51    out.append('\"t\": {0},'.format(start_time))
52    out.append('\"v\": [{0}]'.format(value))
53    out.append('}')
54    if last == False:
55        out.append(',')
56    return ''.join(out)
57
58def make_counter(counter_name, series_name, base_time):
59    out = []
60    out.append('\"n\": \"{0}\",'.format(counter_name))
61    out.append('\"sn\": [\"{0}\"],'.format(series_name))
62    out.append('\"sc\": [2],')
63    out.append('\"c\": [')
64    out.append(make_count(base_time, 16, False))
65    out.append(make_count(base_time+1, 32, False))
66    out.append(make_count(base_time+2, 48, False))
67    out.append(make_count(base_time+3, 64, False))
68    out.append(make_count(base_time+8, 16, True))
69    out.append(']')
70    return ''.join(out)
71
72def make_counter_payload(pid, counter_name, series_name, base_time):
73    out = []
74    out.append('\"pid\": \"{0}\",'.format(pid))
75    out.append('\"cd\": {')
76    out.append(make_counter(counter_name, series_name, base_time))
77    out.append('}')
78    return ''.join(out)
79
80def make_counter_command(base_time, counter_name, series_name):
81    out = []
82    out.append('{ \"cmd\": \"pcd\",')
83    out.append(make_counter_payload('314159', counter_name,
84                                    series_name, base_time))
85    out.append('}')
86    return ''.join(out)
87
88def web_socket_transfer_data(request):
89    start_time = 0;
90    while True:
91        msg = make_thread_command(start_time, 'apple')
92        request.ws_stream.send_message(msg, binary=False)
93        msg = make_thread_command(start_time+1, 'banana')
94        request.ws_stream.send_message(msg, binary=False)
95        msg = make_thread_command(start_time+2, 'cherry')
96        request.ws_stream.send_message(msg, binary=False)
97        msg = make_counter_command(start_time+2, 'Base', 'Bytes')
98        request.ws_stream.send_message(msg, binary=False)
99        msg = make_counter_command(start_time+3, 'Font', 'Bytes')
100        request.ws_stream.send_message(msg, binary=False)
101        msg = make_counter_command(start_time+5, 'Textures', 'Bytes')
102        request.ws_stream.send_message(msg, binary=False)
103        start_time += 16
104        time.sleep(0.16)
105
106# vi:sts=4 sw=4 et
107