Commit 65deda02 authored by 姚广胤's avatar 姚广胤

更新计算

parent 2bf176bd
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: ygy * @Author: ygy
* @Date: 2018-03-14 19:40:01 * @Date: 2018-03-14 19:40:01
* @Last Modified by: ygy * @Last Modified by: ygy
* @Last Modified time: 2018-03-22 20:43:39 * @Last Modified time: 2018-03-23 18:32:48
*/ */
const Koa = require('koa'); const Koa = require('koa');
const send = require('koa-send'); const send = require('koa-send');
...@@ -62,7 +62,7 @@ app.use(router()); ...@@ -62,7 +62,7 @@ app.use(router());
const schedule = require('node-schedule'); const schedule = require('node-schedule');
const j = schedule.scheduleJob({ second: 4 }, function() { const j = schedule.scheduleJob({ hour: 4 }, function() {
console.log('执行任务开始'); console.log('执行任务开始');
task.runTiming(); task.runTiming();
console.log('执行任务结束'); console.log('执行任务结束');
......
...@@ -12,7 +12,6 @@ const checkLogin = async (ctx, next) => { ...@@ -12,7 +12,6 @@ const checkLogin = async (ctx, next) => {
) { ) {
await next(); await next();
} else { } else {
console.log('1213')
const wdata = ctx.cookies.get('wdata-oss'); //获取cookie const wdata = ctx.cookies.get('wdata-oss'); //获取cookie
if (wdata && wdata.indexOf('_') > 0) { if (wdata && wdata.indexOf('_') > 0) {
const uid = wdata.split('_')[0]; const uid = wdata.split('_')[0];
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: ygy * @Author: ygy
* @Date: 2018-03-14 19:39:46 * @Date: 2018-03-14 19:39:46
* @Last Modified by: ygy * @Last Modified by: ygy
* @Last Modified time: 2018-03-23 14:17:56 * @Last Modified time: 2018-03-23 18:02:07
*/ */
const historyService = require('../services/historyService'); const historyService = require('../services/historyService');
...@@ -10,30 +10,49 @@ const date = require('../utils/date'); ...@@ -10,30 +10,49 @@ const date = require('../utils/date');
const timing = require('./timing'); const timing = require('./timing');
const runTiming = async () => { const runTiming = async () => {
const urls = [ const task = [
{ {
id: 'tuia', id: 'turnCircle_1',
href: `http://activity.tuidragon.com/activity/index?id=5644&slotId=2380&login=normal&appKey=3p1fqtfC3x5krVrsgCmdSx68JPPj&deviceId=b17b1d5e-7dba-4a55-bedb-26ff10bec388&dsm=1.2380.0.0&tenter=SOW&tck_rid_6c8=0a1b5a39jat135tf-6340031&tck_loc_c5d=tactivity-4783&dcm=401.2380.0.0&&tenter=SOW&tck_userId_674=null`, name: '圆形大转盘1',
arr: [] href:
'http://activity.tuiapple.com/activity/index?id=5644&slotId=2380&login=normal&appKey=3p1fqtfC3x5krVrsgCmdSx68JPPj&deviceId=b17b1d5e-7dba-4a55-bedb-26ff10bec388&dsm=1.2380.0.0&tenter=SOW&tck_rid_6c8=0a1b5a39jat135tf-6340031&tck_loc_c5d=tactivity-4783&dcm=401.2380.0.0&&tenter=SOW&tck_userId_674=null'
},
{
id: 'smashg_2',
name: '砸彩蛋2',
href:
'https://activity.tuiapple.com/activity/index?id=6322&slotId=2380&login=normal&appKey=3p1fqtfC3x5krVrsgCmdSx68JPPj&deviceId=b17b1d5e-7dba-4a55-bedb-26ff10bec388&dsm=1.2380.0.0&tenter=SOW&tck_rid_6c8=0a1b5a39jat135tf-6340031&tck_loc_c5d=tactivity-4783&dcm=401.2380.0.0&&tenter=SOW&tck_userId_674=null'
},
{
id: 'wallet_3',
name: '卡包3',
href:
'https://activity.tuiapple.com/activity/index?id=6225&slotId=2380&login=normal&appKey=3p1fqtfC3x5krVrsgCmdSx68JPPj&deviceId=b17b1d5e-7dba-4a55-bedb-26ff10bec388&dsm=1.2380.0.0&tenter=SOW&tck_rid_6c8=0a1b5a39jat135tf-6340031&tck_loc_c5d=tactivity-4783&dcm=401.2380.0.0&&tenter=SOW&tck_userId_674=null'
} }
]; ];
await timing.iterateTimes(urls, 2); const taskResults = await timing.iterateTimes(task, 2);
const resultToSave = urls[0]['arr'];
const total = resultToSave.reduce((item, index) => { taskResults.map(async page => {
console.log(item, index); const arr = page['arr'].map(item => item['whiteTime']);
return item + index;
});
await historyService.createOne({ const total = arr.reduce((total, item) => {
result: JSON.stringify({ all: total / (resultToSave.length || 1) }), return total + item;
owner: 'test', });
type: 'zhuanpan1',
day: date.dateFormat(new Date(), 'yyyy-MM-dd') const per = total / (arr.length || 1);
await historyService.createOne({
result: JSON.stringify({ whiteTime: per }),
owner: page.name,
type: page.id,
day: date.dateFormat(new Date(), 'yyyy-MM-dd')
});
}); });
}; };
// runTiming();
module.exports = { module.exports = {
runTiming runTiming
}; };
const puppeteer = require('puppeteer'); const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors'); const devices = require('puppeteer/DeviceDescriptors');
const iPhone = devices['iPhone 6']; const iPhone = devices['iPhone 6'];
/** /**
* 启用无头浏览器,根据列表里的页面地址去循环执行 * 每个页面一个任务跑n次来获取测试数据
*
*/ */
const runBrowser = async (urls) => { const iterateTimes = async (task, n = 2) => {
const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']}); const allTaskResult = [];
for (let i = 0; i < n; i++) {
allTaskResult.push(await runBrowser(task));
}
return await groupPage(allTaskResult, task);
};
const groupPage = async (allTaskResult, task) => {
const taskCopy = JSON.parse(JSON.stringify(task));
const requests = []; allTaskResult.forEach(taskResult => {
for (const url of urls) { taskResult.forEach((page, index) => {
requests.push(await runPage(browser, url)); const unitTaskCopy = taskCopy[index];
if (!unitTaskCopy['arr']) {
unitTaskCopy['arr'] = [];
}
unitTaskCopy['arr'].push(page);
});
});
return taskCopy;
};
/**
* 启用无头浏览器,根据任务里的页面地址去循环执行
*/
const runBrowser = async task => {
const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
const taskResult = [];
for (const unitTask of task) {
taskResult.push(await runPage(browser, unitTask));
} }
// const results = await Promise.all(requests);
// console.log(results);
browser.close(); browser.close();
return taskResult;
}; };
/** /**
* 打开某个页面,执行并收集数据 * 打开某个页面,执行并收集数据
* *
* @param {*} browser * @param {*} browser
* @param {*} url * @param {*} unitTask
*/ */
const runPage = async (browser, url) => { const runPage = async (browser, unitTask) => {
try { try {
const page = await browser.newPage(); const page = await browser.newPage();
const { href, arr } = url; const { href, arr } = unitTask;
let eventType = 'domcontentloaded';
await page.emulate(iPhone); await page.emulate(iPhone);
...@@ -36,16 +68,13 @@ const runPage = async (browser, url) => { ...@@ -36,16 +68,13 @@ const runPage = async (browser, url) => {
await page.waitForFunction('window.allEnd > 0'); await page.waitForFunction('window.allEnd > 0');
const dimensions = await collectTiming(page, url); const testResult = await collectTiming(page, unitTask);
console.log(dimensions.allEnd);
arr.push(dimensions.allEnd);
await page.close(); await page.close();
return testResult;
} catch (err) { } catch (err) {
console.log(err); throw err;
return null;
} }
}; };
...@@ -53,31 +82,16 @@ const runPage = async (browser, url) => { ...@@ -53,31 +82,16 @@ const runPage = async (browser, url) => {
* 执行页面,收集性能数据信息 * 执行页面,收集性能数据信息
* *
* @param {*} page * @param {*} page
* @param {*} url * @param {*} unitTask
*/ */
const collectTiming = async (page, url) => { const collectTiming = async page => {
return await page.evaluate(url => { return await page.evaluate(() => {
const pt = window.performance.timing; const pt = window.performance.timing;
const { href, id } = url;
return { return {
href, whiteTime: window.allEnd - pt.navigationStart
id,
allEnd: window.allEnd - pt.navigationStart
}; };
}, url); });
};
/**
* 每个页面跑n次来获取测试数据
*
* @param {*} n
*/
const iterateTimes = async (urls, n = 2) => {
for (let i = 0; i < n; i++) {
await runBrowser(urls);
}
}; };
module.exports = { module.exports = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment