-
Notifications
You must be signed in to change notification settings - Fork 0
/
puppeteer-tracealyzer-backup.js
130 lines (102 loc) · 4.77 KB
/
puppeteer-tracealyzer-backup.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
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
/*https://michaljanaszek.com/blog/test-website-performance-with-puppeteer/*/
const puppeteer = require('puppeteer');
const fs = require('fs')
const tracealyzer = require('tracealyzer');
const { performance, PerformanceObserver} = require('perf_hooks')
var mergeJSON = require("merge-json") ;
let starttime = Date.now()
console.log(starttime);
const tracepath = "./tracelyzer/";
const windowperfpath = "./window.performance.timing/";
const pagemetricspath = "./page.metrics/";
const perfgetmetricspath = "./performance.getmetrics/";
// var stream1 = fs.createWriteStream(tracepath+filename, {flags:'a'});
// var stream2 = fs.createWriteStream(windowperfpath+major_filename+".json", {flags:'a'});
// var stream3 = fs.createWriteStream(pagemetricspath+filename, {flags:'a'});
// var stream4 = fs.createWriteStream(perfgetmetricspath+filename, {flags:'a'});
var i = 0;
(async () => {
const browser = await puppeteer.launch();
for(i = 0; i < 10;i++) {
const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();
await page.tracing.start({path: './profile_' + i.toString() + '.json'});
await page.goto('http://localhost:63342/win_BD_experiment/clean-leaflet/', {waitUntil: 'load', timeout: 0});
// await page.goto('http://urban-sustain.org/aperture3/aperture-client/', {waitUntil: 'load', timeout: 0});
// await page.waitFor(1000);
// //Code to dig into the iframe and select a fire station checkbox
// let myFrame;
// try{
// for (const frame of page.mainFrame().childFrames()){
// // Here you can use few identifying methods like url(),name(),title()
// if (frame.url().includes ('map2.html')){
// console.log('we found the map2 iframe')
// myFrame = frame
// // we assign this frame to myFrame to use it later
// }
// }
//
// } catch (error){
// console.log("iframe/element related error" + error.toString());
// }
await page._client.send('Performance.enable');
const performanceMetrics = await page._client.send('Performance.getMetrics');
// const performanceTiming = JSON.parse(
// await page.evaluate(() => JSON.stringify(window.performance.timing))
// );
const performanceTiming = await page.evaluate(() => JSON.stringify(window.performance.timing));
let perfvar = {}
perfvar.performanceTiming = performanceTiming
let perfJSON = JSON.parse(JSON.stringify(perfvar));
let shapecount = await page.evaluate(() => {
var x = document.getElementsByClassName("legend");
shapecount = x[0].innerHTML.split(",")[0].split('>')[3];
return shapecount;
});
console.log( "Shape count: ", shapecount);
const major_filename = shapecount == ""?'loadsof':shapecount.toString() + '-state-metrics';
//const major_filename="Urban-Sustain-Test.";
await page.tracing.stop();
await context.close();
const metrics = tracealyzer('./profile_' + i.toString() + '.json');
const filename = major_filename + '_run_' + i.toString() + '.json';
var stream1 = fs.createWriteStream(tracepath+filename, {flags:'a'});
var result = mergeJSON.merge(metrics,performanceMetrics)
var result = mergeJSON.merge(result,perfJSON)
// stream1.write(JSON.stringify(result));
stream1.close();
// var stream3 = fs.createWriteStream(perfgetmetricspath + major_filename + '_run_' + i.toString()+ ".json", {flags:'a'});
// stream3.write(JSON.stringify(performanceMetrics));
// stream3.close();
console.log("Time Taken for " + i.toString() + "th run for "+ shapecount +": ", Date.now() - starttime)
}
await browser.close();
})();
//https://stackoverflow.com/questions/25460574/find-files-by-extension-html-under-a-folder-in-nodejs
var path = require('path');
function fromDir(startPath,filter,callback){
// console.log('Starting from dir '+startPath+'/');
if (!fs.existsSync(startPath)){
console.log("no dir ",startPath);
return;
}
var files=fs.readdirSync(startPath);
for(var i=0;i<files.length;i++){
var filename=path.join(startPath,files[i]);
var stat = fs.lstatSync(filename);
if (stat.isDirectory()){
fromDir(filename,filter,callback); //recurse
}
else if (filter.test(filename)) callback(filename);
};
};
fromDir('./',/profile\_/,function(filename){
console.log('-- found: ',filename);
fs.unlink(filename, (err) => {
if (err) {
console.error(err)
fs.unlinkSync(filename)
return
}//file removed
})
});