10000 Final verison 4.1/1: Version 1. · JayAyAre/PyScript-vs-JavaScript@545ecbb · GitHub
[go: up one dir, main page]

Skip to content

Commit 545ecbb

Browse files
committed
Final verison 4.1/1: Version 1.
1 parent 9d22380 commit 545ecbb

File tree

30 files changed

+306
-417
lines changed

30 files changed

+306
-417
lines changed

Astro-TFG/public/scripts/4.1-calculos-matematicos/prueba-1/version-1/index.html

Lines changed: 0 additions & 92 deletions
This file was deleted.

Astro-TFG/public/scripts/4.1-calculos-matematicos/prueba-1/version-1/javascript/main.js

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ function createMatrix(size) {
1010
}
1111

1212
function multiplyMatrices(size) {
13-
let A = createMatrix(size);
14-
let B = createMatrix(size);
15-
let C = new Array(size).fill(0).map(() => new Array(size).fill(0));
13+
const A = createMatrix(size);
14+
const B = createMatrix(size);
15+
const C = Array.from({ length: size }, () => new Array(size).fill(0));
16+
17+
const start = performance.now();
1618

17-
let start = performance.now();
1819
for (let i = 0; i < size; i++) {
1920
for (let j = 0; j < size; j++) {
2021
let sum = 0;
@@ -24,41 +25,52 @@ function multiplyMatrices(size) {
2425
C[i][j] = sum;
2526
}
2627
}
27-
let end = performance.now();
28+
const executionTime = performance.now() - start;
29+
const memoryUsage = getMemoryUsageJS();
30+
31+
const results = {
32+
execution_time: executionTime,
33+
memory_usage: memoryUsage
34+
};
2835

29-
// ET (Execution Time)
36+
displayResults(results);
37+
window.hideExecutionLoader();
38+
}
3039

31-
let resultTime = Number((end - start).toFixed(2));
32-
let result = `ET: ${resultTime} ms`;
33-
let resultDiv = document.createElement("div");
34-
resultDiv.textContent = result;
35-
document.getElementById("javascript-output").appendChild(resultDiv);
36-
document.getElementById('loading-message').style.display = 'none';
40+
function getMemoryUsageJS() {
41+
if (performance.memory) {
42+
return performance.memory.usedJSHeapSize / (1024 * 1024);
43+
}
44+
return -1;
3745
}
3846

39-
window.runJSBenchmark = function () {
47+
48+
function displayResults(results) {
49+
const output = document.getElementById("javascript-output");
50+
51+
const timeDiv = document.createElement("div");
52+
timeDiv.textContent = `ET: ${results.execution_time.toFixed(2)} ms`;
53+
output.appendChild(timeDiv);
54+
55+
const memoryDiv = document.createElement("div");
56+
if (results.memory_usage !== -1) {
57+
memoryDiv.textContent = `RAM: ${results.memory_usage.toFixed(2)} MB`;
58+
} else {
59+
memoryDiv.textContent = `RAM unsupported measurement in this browser.`;
60+
}
61+
output.appendChild(memoryDiv);
62+
}
63+
64+
window.runJsBenchmark = function () {
4065
clearCell("javascript-output");
4166
window.showExecutionLoader();
4267

4368
requestAnimationFrame(() => {
4469
setTimeout(() => {
4570
document.getElementById("javascript-output").textContent = ""
4671
multiplyMatrices(300);
47-
let memoryDiv = document.createElement("div");
48-
memoryDiv.textContent = getMemoryUsageJS();
49-
document.getElementById("javascript-output").appendChild(memoryDiv);
5072
window.hideExecutionLoader();
5173
}, 0);
5274
});
5375

54-
}
55-
56-
function getMemoryUsageJS() {
57-
// RAM
58-
if (performance.memory) {
59-
let memoryUsed = performance.memory.usedJSHeapSize / (1024 * 1024);
60-
return `RAM: ${memoryUsed.toFixed(2)} MB`;
61-
} else {
62-
return `RAM unsopported measurement in this browser.`;
63-
}
6476
}

Astro-TFG/public/scripts/4.1-calculos-matematicos/prueba-1/version-1/javascript/server.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,23 @@ function createMatrix(size) {
1414
return matrix;
1515
}
1616

17+
function getMemoryUsage() {
18+
return process.memoryUsage().heapUsed / (1024 * 1024);
19+
}
20+
21+
async function getCpuUsage() {
22+
return await cpu.usage();
23+
}
24+
1725
async function multiplyMatrices(size) {
26+
const startMemory = getMemoryUsage();
27+
const startCpu = await getCpuUsage();
28+
1829
let A = createMatrix(size);
1930
let B = createMatrix(size);
2031
let C = new Array(size).fill(0).map(() => new Array(size).fill(0));
2132

22-
const startMemory = process.memoryUsage().heapUsed;
23-
const cpuAvg = await cpu.usage();
24-
const startReal = performance.now();
33+
const startTime = performance.now();
2534

2635
for (let i = 0; i < size; i++) {
2736
for (let j = 0; j < size; j++) {
@@ -33,17 +42,18 @@ async function multiplyMatrices(size) {
3342
}
3443
}
3544

36-
const endReal = performance.now();
37-
const endMemory = process.memoryUsage().heapUsed;
45+
const endTime = performance.now();
46+
const endMemory = getMemoryUsage();
47+
const endCpu = await getCpuUsage();
3848

39-
const time = (endReal - startReal).toFixed(2);
40-
const cpu_usage = ((endMemory - startMemory) / (1024 * 1024)).toFixed(2);
41-
const memory_usage = cpuAvg.toFixed(2);
49+
const executionTime = +(endTime - startTime).toFixed(2);
50+
const memoryUsage = +(endMemory - startMemory).toFixed(2);
51+
const cpuUsage = +(endCpu - startCpu).toFixed(2);
4252

4353
return {
44-
time: `ET: ${time} ms`,
45-
cpu_usage: `CPU: ${cpu_usage} %`,
46-
memory_usage: `RAM: ${memory_usage} MB`
54+
time: `ET: ${executionTime} ms`,
55+
cpu_usage: `CPU: ${cpuUsage} %`,
56+
memory_usage: `RAM: ${memoryUsage} MB`
4757
};
4858
}
4959

Astro-TFG/public/scripts/4.1-calculos-matematicos/prueba-1/version-1/python/main.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import random
44
import js # type: ignore
55
import tracemalloc
6-
from pyscript import display # type: ignore
6+
from pyscript import display
77

88

99
def create_matrix(size):
@@ -25,23 +25,23 @@ def multiply_matrices(size):
2525
_sum += A[i][k] * B[k][j]
2626
C[i][j] = _sum
2727

28-
end = time.time()
29-
30-
# ET (Execution Time)
31-
32-
execution_time = (end - start) * 1000
33-
display(f"ET: {round(execution_time, 2)} ms", target="pyscript-output")
28+
execution_time = (time.time() - start) * 1000
29+
memory_usage = tracemalloc.get_traced_memory()[1] / (1024 * 1024)
30+
tracemalloc.stop()
3431

35-
# PLT
32+
results = {
33+
"execution_time": execution_time,
34+
"memory_usage": memory_usage
35+
}
3636

37+
display_results(results)
3738
js.endTimerWebAssembly()
3839

39-
# RAM
4040

41-
memory_usage = tracemalloc.get_traced_memory()[1] / (1024 * 1024)
42-
display(f"RAM: {round(memory_usage, 2)} MB",
41+
def display_results(results):
42+
display(f"ET: {results['execution_time']:.2f} ms",
4343
target="pyscript-output")
44-
tracemalloc.stop()
44+
display(f"RAM: {results['memory_usage']:.2f} MB", target="pyscript-output")
4545

4646

4747
def run_py_benchmark(event):

Astro-TFG/public/scripts/4.1-calculos-matematicos/prueba-1/version-1/python/server.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# scripts/matrix_benchmark.py
21
import json
32
import random
43
import time

Astro-TFG/public/scripts/4.1-calculos-matematicos/prueba-1/version-1/start_servers.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

Astro-TFG/src/layouts/VersionLayout.astro

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ const {
140140
))
141141
}
142142
<button
143-
onclick="runJSBenchmark()"
143+
onclick="runJsBenchmark()"
144144
class="rounded-lg bg-gray-800 px-4 py-2 transition-all focus:ring focus:ring-purple-400 hover:scale-105 hover:bg-gray-700 hover:text-purple-400 cursor-pointer"
145145
>
146146
Ejecutar JavaScript
@@ -276,7 +276,7 @@ const {
276276
{
277277
useNode && (
278278
<td
279-
id="nodeJs-output"
279+
id="node-js-output"
280280
class="border border-gray-700 px-4 py-2"
281281
>
282282
Esperando...
@@ -286,7 +286,7 @@ const {
286286
{
287287
useBackend && (
288288
<td
289-
id="pythonBackend-output"
289+
id="python-backend-output"
290290
class="border border-gray-700 px-4 py-2"
291291
>
292292
Esperando...
@@ -305,18 +305,12 @@ const {
305305
<script>
306306
let jsPath = document.body.dataset.jsPath;
307307
let pyPath = document.body.dataset.pyPath;
308-
let pyConfig = document.body.dataset.pyConfig;
309308
let useNode = document.body.dataset.useNode === 'true';
310309
let useBackend = document.body.dataset.useBackend === 'true';
311310

312-
console.log('script:', { pyPath, pyConfig });
313-
console.log('jsPath:', { jsPath });
314-
console.log('pyPath:', { pyPath });
315-
console.log('PyConfig:', { pyConfig });
316-
317311
window.runNodeBenchmark = async function () {
318312
if (!useNode) return;
319-
window.clearCell('nodeJs-output');
313+
window.clearCell('node-js-output');
320314
try {
321315
import('../scripts/RunNodeServer.js')
322316
.then((module) => {
@@ -330,10 +324,9 @@ const {
330324
}
331325
};
332326

333-
// Button handler para Python Backend
334327
window.runPyBackendBenchmark = async function () {
335328
if (!useBackend) return;
336-
window.clearCell('pythonBackend-output');
329+
window.clearCell('python-backend-output');
337330
try {
338331
import('../scripts/RunPythonServer.js')
339332
.then((module) => {

0 commit comments

Comments
 (0)
0