|
a |
|
b/tests/model11.spec.cjs |
|
|
1 |
const { test, expect} = require('@playwright/test'); |
|
|
2 |
|
|
|
3 |
// we want to save timing info from the models to timing.json |
|
|
4 |
// so we can compare the performance of the models in different browsers |
|
|
5 |
const fs = require('fs'); |
|
|
6 |
const timing = {}; |
|
|
7 |
|
|
|
8 |
// Helper function to wait for the specific log message |
|
|
9 |
async function waitForLogMessage(page, browserName, modelIndex) { |
|
|
10 |
// Create a promise that resolves when the specific log message is found |
|
|
11 |
const logMessagePromise = new Promise((resolve, reject) => { |
|
|
12 |
page.on('console', (msg) => { |
|
|
13 |
const msgText = msg.text(); |
|
|
14 |
// log all messages to help debug github actions |
|
|
15 |
console.log(msgText); |
|
|
16 |
if (msgText.includes('Processing the whole brain volume in tfjs for multi-class output mask took :')) { |
|
|
17 |
// Parse the time in milliseconds |
|
|
18 |
const time = parseFloat(msgText.split('took : ')[1].split(' Seconds')[0]) * 1000; |
|
|
19 |
// Save timing info based on browser name and the model index |
|
|
20 |
if (!timing[browserName]) { |
|
|
21 |
timing[browserName] = {}; |
|
|
22 |
} |
|
|
23 |
timing[browserName][`modelIndex${modelIndex}`] = time; |
|
|
24 |
// log it |
|
|
25 |
console.log(`Model index ${modelIndex} took ${time} ms in ${browserName}`); |
|
|
26 |
// Write to file |
|
|
27 |
fs.writeFileSync('timing.json', JSON.stringify(timing, null, 2)); |
|
|
28 |
resolve(); // Resolve the promise |
|
|
29 |
} |
|
|
30 |
}); |
|
|
31 |
|
|
|
32 |
// Set a timeout to reject the promise if the message is not found within a reasonable time |
|
|
33 |
setTimeout(() => { |
|
|
34 |
reject(new Error('Log message not found within the timeout period')); |
|
|
35 |
}, 30000); // 30 second timeout |
|
|
36 |
}); |
|
|
37 |
|
|
|
38 |
try { |
|
|
39 |
// Wait for the log message promise to resolve |
|
|
40 |
await logMessagePromise; |
|
|
41 |
} catch (error) { |
|
|
42 |
console.error(error.message); |
|
|
43 |
} |
|
|
44 |
} |
|
|
45 |
|
|
|
46 |
|
|
|
47 |
// test a quick (small) model |
|
|
48 |
test('model 11', async ({ page, browserName }, testInfo) => { |
|
|
49 |
// Load timing file if it exists |
|
|
50 |
if (fs.existsSync('timing.json')) { |
|
|
51 |
const data = fs.readFileSync('timing.json', 'utf8'); |
|
|
52 |
if (data) { |
|
|
53 |
Object.assign(timing, JSON.parse(data)); |
|
|
54 |
} |
|
|
55 |
} |
|
|
56 |
const modelIndex = 11; |
|
|
57 |
await page.goto(`http://127.0.0.1:8088/?model=${modelIndex}`); |
|
|
58 |
await waitForLogMessage(page, browserName, modelIndex); |
|
|
59 |
// take snapshot of canvas element gl1 |
|
|
60 |
const canvas = await page.$('canvas#gl1'); |
|
|
61 |
const snapshot = await canvas.screenshot(); |
|
|
62 |
// attach the snapshot to the test report |
|
|
63 |
await testInfo.attach('Snapshot', { body: snapshot, contentType: 'image/png' }); |
|
|
64 |
expect(snapshot).toMatchSnapshot(); |
|
|
65 |
}); |