forked from richorama/AzureSpeedTest2
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.jsx
More file actions
320 lines (292 loc) · 9.36 KB
/
index.jsx
File metadata and controls
320 lines (292 loc) · 9.36 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
const React = require("react");
const ReactDom = require("react-dom");
const speedtest = require("./lib/speed-test");
const history = require("./lib/history");
const sl = require("react-sparklines");
const Sparklines = sl.Sparklines;
const SparklinesLine = sl.SparklinesCurve;
// record history
speedtest.on(history.record);
speedtest.on(() => {
const scrollPosition = window.scrollY;
render(<Table history={history.read()} blockList={globalBlockList} isPaused={globalIsPaused} />);
window.scrollY = scrollPosition;
});
let globalBlockList = [];
let globalIsPaused = false;
speedtest.onBlocklistUpdate((blockList) => (globalBlockList = blockList));
speedtest.onStatusChange((status) => {
globalIsPaused = status.paused;
const scrollPosition = window.scrollY;
render(<Table history={history.read()} blockList={globalBlockList} isPaused={globalIsPaused} />);
window.scrollY = scrollPosition;
});
function render(jsx) {
ReactDom.render(jsx, document.getElementById("content"));
}
const Table = class extends React.Component {
constructor(props) {
super(props);
this.state = {
darkMode: localStorage.getItem('darkMode') === 'true',
isPaused: props.isPaused || false
};
this.renderButton = this.renderButton.bind(this);
this.renderFlag = this.renderFlag.bind(this);
this.renderFlag2 = this.renderFlag2.bind(this);
this.renderRow = this.renderRow.bind(this);
this.renderError = this.renderError.bind(this);
this.toggleDarkMode = this.toggleDarkMode.bind(this);
this.exportToCSV = this.exportToCSV.bind(this);
this.exportToJSON = this.exportToJSON.bind(this);
this.togglePauseResume = this.togglePauseResume.bind(this);
}
componentDidMount() {
// Apply dark mode on mount
if (this.state.darkMode) {
document.body.classList.add('dark-mode');
}
// Save history to localStorage
this.saveHistoryToLocalStorage();
}
componentDidUpdate(prevProps) {
// Save history to localStorage on updates
this.saveHistoryToLocalStorage();
// Update paused state if prop changed
if (prevProps.isPaused !== this.props.isPaused) {
this.setState({ isPaused: this.props.isPaused });
}
}
saveHistoryToLocalStorage() {
try {
const historyData = {
timestamp: new Date().toISOString(),
results: this.props.history.slice(0, 20) // Save last 20 results
};
localStorage.setItem('speedTestHistory', JSON.stringify(historyData));
} catch (e) {
console.error('Failed to save to localStorage', e);
}
}
toggleDarkMode() {
const newDarkMode = !this.state.darkMode;
this.setState({ darkMode: newDarkMode });
localStorage.setItem('darkMode', newDarkMode);
if (newDarkMode) {
document.body.classList.add('dark-mode');
} else {
document.body.classList.remove('dark-mode');
}
}
exportToCSV() {
const headers = ['Data Center', 'Average Latency (ms)', 'Min', 'Max'];
const rows = this.props.history.map(item => [
item.name,
Math.round(item.average),
item.values && item.values.length > 0 ? Math.min(...item.values) : 'N/A',
item.values && item.values.length > 0 ? Math.max(...item.values) : 'N/A'
]);
const csvContent = [
headers.join(','),
...rows.map(row => row.join(','))
].join('\n');
const blob = new Blob([csvContent], { type: 'text/csv' });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `azure-devops-speed-test-${new Date().toISOString().slice(0, 10)}.csv`;
a.click();
window.URL.revokeObjectURL(url);
}
exportToJSON() {
const data = {
timestamp: new Date().toISOString(),
results: this.props.history.map(item => ({
name: item.name,
domain: item.domain,
average: Math.round(item.average),
values: item.values || [],
icon: item.icon,
icon2: item.icon2
}))
};
const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `azure-devops-speed-test-${new Date().toISOString().slice(0, 10)}.json`;
a.click();
window.URL.revokeObjectURL(url);
}
togglePauseResume() {
const newPausedState = !this.state.isPaused;
this.setState({ isPaused: newPausedState });
if (newPausedState) {
speedtest.pause();
} else {
speedtest.resume();
}
}
renderButton() {
let item = this.props.history[0];
if (!item) return "";
if (item.cdn || false) item = this.props.history[1];
if (!item) return "";
return (
<a
href={
"https://twitter.com/intent/tweet?button_hashtag=GitHubAzureDevOpsSpeedTest&text=My%20nearest%20%23AzureDevOps%20%23GitHub%20is%20" +
item.name +
"%20(" +
Math.round(item.average) +
"ms).+Find+out+yours+https%3A%2F%2Fazure4devops.com%2FGithubAzureDevOpsSpeedTest%2F+#GitHubAzureDevOpsSpeedTest"
}
className="btn btn-primary btn-large"
data-size="large"
data-related="two10degrees"
data-dnt="true"
>
Tweet your results
</a>
);
}
renderFlag(item) {
if (!item.icon) return "";
return <img src={item.icon} className="icon" itemType="image/svg" />;
}
renderFlag2(item) {
if (!item.icon2) return "";
return <img src={item.icon2} className="icon" itemType="image/svg" />;
}
renderRow(item) {
const rowStyle = {
backgroundImage:
"linear-gradient(to right, #e9ecef " +
Math.round(item.percent) +
"%, #ffffff " +
Math.round(item.percent) +
"%)",
};
return (
<tr key={item.name} style={rowStyle}>
<td>
{this.renderFlag(item)}
{this.renderFlag2(item)}
{item.name}
</td>
<td>{Math.round(item.average)}ms</td>
<td style={{ padding: 0 }} className="no-mobile">
<Sparklines
data={item.values || []}
width={200}
height={48}
limit={100}
>
<SparklinesLine
color="#B8BABC"
vector-effect="non-scaling-stroke"
/>
</Sparklines>
</td>
</tr>
);
}
renderError(item) {
return (
<tr key={item.name}>
<td>
{this.renderFlag(item)}
{this.renderFlag2(item)}
{item.name}
</td>
<td>
<span className="badge badge-danger">NO RESPONSE</span>
</td>
<td className="no-mobile">
<a
href="javascript:void(0);"
onClick={speedtest.retry.bind(null, item.domain)}
>
Retry
</a>
</td>
</tr>
);
}
render() {
return (
<div>
<button className="dark-mode-toggle" onClick={this.toggleDarkMode}>
{this.state.darkMode ? '☀️ Light Mode' : '🌙 Dark Mode'}
</button>
<div className="export-buttons">
<button
className={`btn ${this.state.isPaused ? 'btn-success' : 'btn-warning'}`}
onClick={this.togglePauseResume}
>
{this.state.isPaused ? '▶️ Resume Testing' : '⏸️ Pause Testing'}
</button>
<button className="btn btn-success" onClick={this.exportToCSV}>
📊 Export to CSV
</button>
<button className="btn btn-info" onClick={this.exportToJSON}>
📄 Export to JSON
</button>
</div>
<table className="table results-table">
<thead>
<tr>
<th>Data Center</th>
<th>Average Latency</th>
<th className="no-mobile">History</th>
</tr>
</thead>
<tbody>{this.props.history.map(this.renderRow)}</tbody>
<tbody>{this.props.blockList.map(this.renderError)}</tbody>
</table>
<p>
Share your results with other people on twitter {this.renderButton()}
</p>
<p>
Compare your speed with others by watching the{" "}
<a href="https://twitter.com/search?q=%23AzureSpeedTest&src=hash&mode=realtime">
#GitHubAzureDevOpsSpeedTest
</a>{" "}
hashtag.
</p>
<p>
<a href="https://github.com/Azure4DevOps/GithubAzureDevOpsSpeedTest">
Fork
</a>{" "}
on GitHub.
</p>
<p>
<a href="https://github.com/richorama/AzureSpeedTest2">
Forked from and inspired from
</a>{" "}
on GitHub.
</p>
<p>
Created by <a href="https://www.twitter.com/jnowwwak/">@jnowwwak</a>
</p>
<p>
The{" "}
<a href="https://azure.microsoft.com/en-us/regions/">Azure Website</a>{" "}
has a map with all data centers, and a{" "}
<a href="https://azure.microsoft.com/en-us/regions/services/">
feature matrix
</a>
.
</p>
<p>
<small>
The latency times are indicative only, and do not represent the
maxium performance, achievable from GitHub and Azure DevOps. Use
this website purely as a tool to gauge which Azure Data Center could
be the best for your location.
</small>
</p>
</div>
);
}
};