blog_projekty/obliczanie_pi/index.html

140 lines
4.1 KiB
HTML
Raw Normal View History

2024-11-04 09:30:41 +00:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>PI Calculator Comparison</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.results {
margin-top: 20px;
}
.result {
margin: 10px 0;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
input,
button {
margin: 10px 0;
padding: 5px;
}
</style>
</head>
<body>
<h1>PI Calculator Comparison</h1>
<div>
<label for="precision">Precision (decimal places):</label>
<input type="number" id="precision" value="1000" min="1" max="100" />
<button onclick="calculateAll()">Calculate in All Languages</button>
<button onclick="autoCalc()">Calculate to</button>
</div>
<div class="results" id="results"></div>
<script>
const endpoints = [
"http://localhost:8081", // Go
"http://localhost:8082", // Python
"http://localhost:8083", // Node.js
];
async function calculatePi(endpoint, precision, index) {
try {
await new Promise((resolve) =>
setTimeout(resolve, (index - 1) * 500)
);
const response = await fetch(`${endpoint}/pi?precision=${precision}`);
const data = await response.json();
return data;
} catch (error) {
return {
error: `Failed to fetch from ${endpoint}: ${error.message}`,
};
}
}
async function autoCalc() {
const resultsDiv = document.getElementById("results");
const precision_max = document.getElementById("precision").value;
resultsDiv.innerHTML = "<h2>Calculating...</h2>";
let allResults = "";
for (
let precision = 100;
precision <= precision_max;
precision += 100
) {
allResults += `\n\nPrecision ${precision}:\n`;
// Wykonaj 5 testów dla każdej precyzji
for (let test = 1; test <= 5; test++) {
const results = await Promise.all(
endpoints.map((endpoint, index) =>
calculatePi(endpoint, precision, index)
)
);
allResults += `Test ${test}:\n`;
results.forEach((result) => {
if (!result.error) {
allResults += `${result.language},${result.time}\n`;
}
});
if (test < 5) {
await new Promise((resolve) => setTimeout(resolve, 500)); // 1 sekunda przerwy między testami
}
}
if (precision < precision_max) {
await new Promise((resolve) => setTimeout(resolve, 500)); // 2 sekundy przerwy przed następną precyzją
}
}
resultsDiv.innerHTML = "<h2>Results:</h2>";
const pre = document.createElement("pre");
pre.textContent = allResults;
resultsDiv.appendChild(pre);
}
async function calculateAll() {
const precision = document.getElementById("precision").value;
const resultsDiv = document.getElementById("results");
resultsDiv.innerHTML = "<h2>Calculating...</h2>";
const results = await Promise.all(
endpoints.map((endpoint, index) =>
calculatePi(endpoint, precision, index)
)
);
resultsDiv.innerHTML = "<h2>Results:</h2>";
results.forEach((result) => {
const resultDiv = document.createElement("div");
resultDiv.className = "result";
if (result.error) {
resultDiv.innerHTML = `<p>Error: ${result.error}</p>`;
} else {
resultDiv.innerHTML = `
<h3>${result.language}</h3>
<p>PI: ${result.pi}</p>
<p>Calculation time: ${result.time}</p>
`;
}
resultsDiv.appendChild(resultDiv);
});
}
</script>
</body>
</html>