/
index.html
176 lines (158 loc) · 6.97 KB
/
index.html
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Speed Test - comparing JSON and ProtoBuf speed</title>
<!-- Bootstrap -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container" ng-app="speed-test-app">
<div class="row" ng-controller="SpeedTestController as ctrl">
<div class="col-xs-12">
<h1>Speed Test - comparing JSON and ProtoBuf speed</h1>
<p>
The following buttons will request a bunch of data from the server and show how fast they go. Both
return exactly the same data, but with different protocols.
</p>
<div class="row">
<div class="col-xs-4 col-sm-2">
<button class="btn btn-primary btn-block" ng-click="ctrl.requestProtobuf()">15x - ProtoBuf request</button>
</div>
<div class="col-xs-4 col-sm-2">
<button class="btn btn-success btn-block" ng-click="ctrl.requestJson()">15x - JSON request</button>
</div>
<div class="col-xs-4 col-sm-2">
<button class="btn btn-info btn-block" ng-click="ctrl.requestProtobufJava()">Java</button>
</div>
</div>
<div class="row">
<div class="col-xs-4 col-sm-2">
<button class="btn btn-primary btn-block" ng-click="ctrl.postProtobuf()">Post ProtoBuf</button>
</div>
<div class="col-xs-4 col-sm-2">
<button class="btn btn-success btn-block" ng-click="ctrl.postJSON()">Post JSON</button>
</div>
</div>
</div>
</div>
</div>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>
<script src="bundle.js"></script>
<script>
(function() {
const People = protobuf.roots.default.demo.People;
const Person = protobuf.roots.default.demo.Person;
const Address = protobuf.roots.default.demo.Address;
const peopleEndpoint = '/people';
const protobufResp = 'arraybuffer';
const octetStream = 'binary/octet-stream';
const NUMBER_OF_CALLS = 50;
angular.module('speed-test-app', [])
.controller('SpeedTestController', function($http) {
let ctrl = this;
ctrl.people = [];
ctrl.time = [];
ctrl.requestProtobufJava = requestProtobufJava;
ctrl.requestProtobuf = protobuRequest;
ctrl.requestJson = jsonRequest;
ctrl.postProtobuf = postProtobuf;
ctrl.postJSON = postJSON;
function requestProtobufJava() {
$http.get(`/protobuf/java`).then(function() {
console.log('done');
});
}
function postJSON() {
let startTime = Date.now();
let address = new Address({
street: 'Street',
number: 100
});
let jsonPerson = {
name: 'Some person',
address: [],
mobile: [],
email: []
};
for (let i = 0; i < 3; i++) jsonPerson.address.push(address);
for (let i = 0; i < 3; i++) jsonPerson.mobile.push('(1) 732-757-2923');
for (let i = 0; i < 3; i++) jsonPerson.email.push('someone@somewhere.com');
let person = new Person(jsonPerson);
let people = new People({
person: [person]
});
return $http.post('/json/people', people).then(function() {
recordTime(Date.now() - startTime, postJSON);
});
}
function postProtobuf() {
let startTime = Date.now();
let address = new Address({
street: 'Street',
number: 100
});
let jsonPerson = {
name: 'Some person',
address: [],
mobile: [],
email: []
};
for (let i = 0; i < 3; i++) jsonPerson.address.push(address);
for (let i = 0; i < 3; i++) jsonPerson.mobile.push('(1) 732-757-2923');
for (let i = 0; i < 3; i++) jsonPerson.email.push('someone@somewhere.com');
let person = new Person(jsonPerson);
let people = new People({
person: [person]
});
let post = {
method: 'POST',
url: '/protobuf/people',
data: People.encode(people).finish(),
transformRequest: [],
headers: {
'Content-Type': 'application/x-protobuf'
}
};
return $http(post).then(function() {
recordTime(Date.now() - startTime, postProtobuf);
});
}
function protobuRequest() {
let startTime = Date.now();
let req = {
method: 'GET',
responseType: protobufResp,
url: `/protobuf/${peopleEndpoint}`
};
return $http(req).then(function(response) {
ctrl.people = People.decode(new Uint8Array(response.data)).person;
recordTime(Date.now() - startTime, protobuRequest);
});
}
function jsonRequest() {
let startTime = Date.now();
$http.get(`/json/${peopleEndpoint}`).then(function(response) {
ctrl.people = response.data.person;
recordTime(Date.now() - startTime, jsonRequest);
});
}
function recordTime(time, callback) {
ctrl.time.push(time);
if (ctrl.time.length < NUMBER_OF_CALLS) {
return callback();
}
let totalTime = 0;
ctrl.time.forEach(time => {
totalTime += time;
});
console.log(`Took ${totalTime / NUMBER_OF_CALLS}ms to handle the request and response.`);
ctrl.time = [];
}
});
}());
</script>
</body>
</html>