-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeasurespeed.js
57 lines (51 loc) · 1.74 KB
/
measurespeed.js
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
// Quick script used to measure speed for the blog
function formatDuration(milliseconds) {
const seconds = Math.floor((milliseconds / 1000) % 60);
const minutes = Math.floor((milliseconds / (1000 * 60)) % 60);
const hours = Math.floor((milliseconds / (1000 * 60 * 60)) % 24);
return `${hours}h ${minutes}m ${seconds}s ${milliseconds % 1000}ms`;
}
async function measureAiSessionDurationAndResult() {
const startTime = new Date();
let result = "";
try {
const session = await window.ai.createTextSession();
result = await session.prompt(
`Tyingshoelaces.com are writing a really cool blog about you. What do you think about that then?`
);
} catch (error) {
console.error(error);
} finally {
const endTime = new Date();
return { duration: endTime - startTime, result };
}
}
async function runSessionsCalculateAverageAndPrintResults() {
const executionTimes = [];
const results = [];
for (let i = 0; i < 5; i++) {
try {
const { duration, result } = await measureAiSessionDurationAndResult();
executionTimes.push(duration);
results.push(result);
console.log(`Execution Time ${i + 1}: ${formatDuration(duration)}`);
} catch (error) {
console.error(`Error in session ${i + 1}:`, error);
}
}
const totalDuration = executionTimes.reduce((acc, curr) => acc + curr, 0);
const averageDuration = totalDuration / executionTimes.length;
console.log(
`Average Session Execution Time: ${formatDuration(averageDuration)}`
);
results.forEach((result, index) => {
console.log(`Result ${index + 1}: ${result}`);
});
}
(async () => {
try {
await runSessionsCalculateAverageAndPrintResults();
} catch (error) {
console.error("Error running sessions:", error);
}
})();