Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
warden
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
姚广胤
warden
Commits
65deda02
Commit
65deda02
authored
Mar 23, 2018
by
姚广胤
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
更新计算
parent
2bf176bd
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
91 additions
and
59 deletions
+91
-59
app.js
server/app.js
+2
-2
index.js
server/filter/index.js
+0
-1
index.js
server/task/index.js
+35
-16
timing.js
server/task/timing.js
+54
-40
No files found.
server/app.js
View file @
65deda02
...
...
@@ -2,7 +2,7 @@
* @Author: ygy
* @Date: 2018-03-14 19:40:01
* @Last Modified by: ygy
* @Last Modified time: 2018-03-2
2 20:43:39
* @Last Modified time: 2018-03-2
3 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
(
'执行任务结束'
);
...
...
server/filter/index.js
View file @
65deda02
...
...
@@ -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
];
...
...
server/task/index.js
View file @
65deda02
...
...
@@ -2,7 +2,7 @@
* @Author: ygy
* @Date: 2018-03-14 19:39:46
* @Last Modified by: ygy
* @Last Modified time: 2018-03-23 1
4:17:56
* @Last Modified time: 2018-03-23 1
8: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
};
server/task/timing.js
View file @
65deda02
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 {*} u
rl
* @param {*} u
nitTask
*/
const
runPage
=
async
(
browser
,
u
rl
)
=>
{
const
runPage
=
async
(
browser
,
u
nitTask
)
=>
{
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 {*} u
rl
* @param {*} u
nitTask
*/
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
=
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment