-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.vue
161 lines (142 loc) · 4.4 KB
/
app.vue
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
<script setup>
const isCalculating = ref(false);
const isFinished = ref(false);
const currentRequests = ref([]);
// Ignore the first 200ms of download as buffer.
const isBufferTime = ref(false);
const bufferTimeDownload = ref([0, 0, 0, 0, 0]);
const bytesDownloaded = ref([0, 0, 0, 0, 0]);
const startTime = ref(null);
const formatSpeed = (bitsPerSecond) => {
const kbps = bitsPerSecond / 1_000;
const mbps = bitsPerSecond / 1_000_000;
if (mbps >= 10) return { speed: mbps, unit: "M" };
if (kbps >= 10) return { speed: kbps, unit: "K" };
return { speed: bitsPerSecond, unit: "" };
};
const downloadSpeed = computed(() => {
if (!startTime.value || isBufferTime.value)
return { speed: null, unit: null };
const elapsedTimeMs = Date.now() - startTime.value - 200;
const totalBytes = bytesDownloaded.value.reduce(
(sum, bytes, index) => sum + bytes - bufferTimeDownload.value[index],
);
const bitsPerSecond = Math.abs((totalBytes * 8) / (elapsedTimeMs / 1_000));
return formatSpeed(bitsPerSecond);
});
const fetchFile = (url, index) => {
const xhr = new XMLHttpRequest();
const promise = new Promise((resolve, reject) => {
xhr.addEventListener("progress", (e) => {
bytesDownloaded.value[index] = e.loaded;
if (isBufferTime.value) bufferTimeDownload.value[index] = e.loaded;
});
xhr.addEventListener("loadend", resolve);
xhr.addEventListener("abort", resolve);
xhr.addEventListener("error", reject);
xhr.open("GET", url);
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.send();
});
return { xhr, promise };
};
const calculateDownloadSpeed = async () => {
isBufferTime.value = true;
isCalculating.value = true;
isFinished.value = false;
startTime.value = new Date();
currentRequests.value.forEach((req) => req.abort());
bytesDownloaded.value = [0, 0, 0, 0, 0];
bufferTimeDownload.value = [0, 0, 0, 0, 0];
const urls = [
"/assets/sample.jpg?step=1",
"/assets/sample.jpg?step=2",
"/assets/sample.jpg?step=3",
"/assets/sample.jpg?step=4",
"/assets/sample.jpg?step=5",
];
const requests = urls.map((url, index) => fetchFile(url, index));
currentRequests.value = requests.map((req) => req.xhr);
const bufferTimeout = setTimeout(() => {
isBufferTime.value = false;
clearTimeout(bufferTimeout);
}, 400);
const timeout = setTimeout(
() => currentRequests.value.forEach((req) => req.abort()),
20_000,
);
await Promise.all(requests.map((req) => req.promise));
clearTimeout(timeout);
isFinished.value = true;
};
useHead({
htmlAttrs: { lang: "en" },
link: [
{ rel: "icon", type: "image/png", href: "/favicon.png" },
{ rel: "preconnect", href: "https://fast.abhay.app/assets/sample.jpg" },
],
});
useSeoMeta({
title: "Fast | Internet speed test",
ogTitle: "Fast | Internet speed test",
twitterTitle: "Fast | Internet speed test",
description: "Check your internet speed using this simple web app.",
ogDescription: "Check your internet speed using this simple web app.",
twitterDescription: "Check your internet speed using this simple web app.",
twitterCreatorId: "@abhayvashokan",
twitterSite: "@abhayvashokan",
ogUrl: "https://fast.abhay.app",
keywords: [
"speedtest",
"speed test",
"bandwidth",
"ping",
"throughput",
"nuxt",
"nuxt3",
"vue3",
],
ogImage: "https://fast.abhay.app/cover.png",
twitterImage: "https://fast.abhay.app/cover.png",
twitterCard: "summary_large_image",
});
</script>
<template>
<main class="max-w-6xl mx-auto p-8 flex flex-col h-screen relative">
<NuxtRouteAnnouncer />
<Button @calculate-download-speed="calculateDownloadSpeed" :is-calculating="isCalculating"
:is-finished="isFinished">
<p v-if="!isCalculating" class="text-5xl">Go</p>
<div v-else class="text-8xl md:text-9xl flex items-end justify-center">
<p>{{ Math.round(downloadSpeed.speed) }}</p>
<p class="text-3xl md:text-5xl leading-normal">
{{ downloadSpeed.unit }}bps
</p>
</div>
</Button>
<copyright></copyright>
</main>
</template>
<style>
:root {
--background: #535353;
--primary-button-foreground: #1e212b;
}
body {
font-family:
Orbitron,
system-ui,
-apple-system,
BlinkMacSystemFont,
"Segoe UI",
Roboto,
Oxygen,
Ubuntu,
Cantarell,
"Open Sans",
"Helvetica Neue",
sans-serif;
background-color: var(--background);
overflow: hidden;
}
</style>