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

更新计算

parent 2bf176bd
......@@ -2,7 +2,7 @@
* @Author: ygy
* @Date: 2018-03-14 19:40:01
* @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 send = require('koa-send');
......@@ -62,7 +62,7 @@ app.use(router());
const schedule = require('node-schedule');
const j = schedule.scheduleJob({ second: 4 }, function() {
const j = schedule.scheduleJob({ hour: 4 }, function() {
console.log('执行任务开始');
task.runTiming();
console.log('执行任务结束');
......
......@@ -12,7 +12,6 @@ const checkLogin = async (ctx, next) => {
) {
await next();
} else {
console.log('1213')
const wdata = ctx.cookies.get('wdata-oss'); //获取cookie
if (wdata && wdata.indexOf('_') > 0) {
const uid = wdata.split('_')[0];
......
......@@ -2,7 +2,7 @@
* @Author: ygy
* @Date: 2018-03-14 19:39:46
* @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');
......@@ -10,30 +10,49 @@ const date = require('../utils/date');
const timing = require('./timing');
const runTiming = async () => {
const urls = [
const task = [
{
id: 'tuia',
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`,
arr: []
id: 'turnCircle_1',
name: '圆形大转盘1',
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 resultToSave = urls[0]['arr'];
const taskResults = await timing.iterateTimes(task, 2);
const total = resultToSave.reduce((item, index) => {
console.log(item, index);
return item + index;
});
taskResults.map(async page => {
const arr = page['arr'].map(item => item['whiteTime']);
await historyService.createOne({
result: JSON.stringify({ all: total / (resultToSave.length || 1) }),
owner: 'test',
type: 'zhuanpan1',
day: date.dateFormat(new Date(), 'yyyy-MM-dd')
const total = arr.reduce((total, item) => {
return total + item;
});
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 = {
runTiming
};
const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors');
const iPhone = devices['iPhone 6'];
/**
* 启用无头浏览器,根据列表里的页面地址去循环执行
* 每个页面一个任务跑n次来获取测试数据
*
*/
const runBrowser = async (urls) => {
const browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
const iterateTimes = async (task, n = 2) => {
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 = [];
for (const url of urls) {
requests.push(await runPage(browser, url));
allTaskResult.forEach(taskResult => {
taskResult.forEach((page, index) => {
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();
return taskResult;
};
/**
* 打开某个页面,执行并收集数据
*
* @param {*} browser
* @param {*} url
* @param {*} unitTask
*/
const runPage = async (browser, url) => {
const runPage = async (browser, unitTask) => {
try {
const page = await browser.newPage();
const { href, arr } = url;
let eventType = 'domcontentloaded';
const { href, arr } = unitTask;
await page.emulate(iPhone);
......@@ -36,16 +68,13 @@ const runPage = async (browser, url) => {
await page.waitForFunction('window.allEnd > 0');
const dimensions = await collectTiming(page, url);
console.log(dimensions.allEnd);
arr.push(dimensions.allEnd);
const testResult = await collectTiming(page, unitTask);
await page.close();
return testResult;
} catch (err) {
console.log(err);
return null;
throw err;
}
};
......@@ -53,31 +82,16 @@ const runPage = async (browser, url) => {
* 执行页面,收集性能数据信息
*
* @param {*} page
* @param {*} url
* @param {*} unitTask
*/
const collectTiming = async (page, url) => {
return await page.evaluate(url => {
const collectTiming = async page => {
return await page.evaluate(() => {
const pt = window.performance.timing;
const { href, id } = url;
return {
href,
id,
allEnd: window.allEnd - pt.navigationStart
whiteTime: 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 = {
......
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