Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MeterSphere
metersphere
提交
24be510b
M
metersphere
项目概览
MeterSphere
/
metersphere
上一次同步 大约 3 年
通知
25
Star
1
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
metersphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
24be510b
编写于
4月 24, 2020
作者:
C
Captain.B
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
移除代码
上级
61e88258
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
0 addition
and
325 deletion
+0
-325
backend/src/main/java/io/metersphere/report/GenerateReport.java
...d/src/main/java/io/metersphere/report/GenerateReport.java
+0
-135
backend/src/main/java/io/metersphere/report/parse/ResultDataParse.java
...ain/java/io/metersphere/report/parse/ResultDataParse.java
+0
-190
未找到文件。
backend/src/main/java/io/metersphere/report/GenerateReport.java
已删除
100644 → 0
浏览文件 @
61e88258
package
io.metersphere.report
;
import
com.opencsv.bean.CsvToBean
;
import
com.opencsv.bean.CsvToBeanBuilder
;
import
com.opencsv.bean.HeaderColumnNameMappingStrategy
;
import
io.metersphere.report.base.*
;
import
io.metersphere.report.parse.ResultDataParse
;
import
org.apache.jmeter.report.processor.ErrorsSummaryConsumer
;
import
org.apache.jmeter.report.processor.StatisticsSummaryConsumer
;
import
org.apache.jmeter.report.processor.Top5ErrorsBySamplerConsumer
;
import
org.apache.jmeter.report.processor.graph.impl.ActiveThreadsGraphConsumer
;
import
org.apache.jmeter.report.processor.graph.impl.HitsPerSecondGraphConsumer
;
import
org.apache.jmeter.report.processor.graph.impl.ResponseTimeOverTimeGraphConsumer
;
import
java.io.Reader
;
import
java.io.StringReader
;
import
java.math.BigDecimal
;
import
java.text.DecimalFormat
;
import
java.time.Duration
;
import
java.time.Instant
;
import
java.time.LocalDateTime
;
import
java.time.ZoneId
;
import
java.time.format.DateTimeFormatter
;
import
java.util.*
;
public
class
GenerateReport
{
private
static
List
<
Metric
>
resolver
(
String
jtlString
)
{
HeaderColumnNameMappingStrategy
<
Metric
>
ms
=
new
HeaderColumnNameMappingStrategy
<>();
ms
.
setType
(
Metric
.
class
);
try
(
Reader
reader
=
new
StringReader
(
jtlString
))
{
CsvToBean
<
Metric
>
cb
=
new
CsvToBeanBuilder
<
Metric
>(
reader
)
.
withType
(
Metric
.
class
)
.
withSkipLines
(
0
)
.
withMappingStrategy
(
ms
)
.
withIgnoreLeadingWhiteSpace
(
true
)
.
build
();
return
cb
.
parse
();
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
}
return
null
;
}
public
static
List
<
Errors
>
getErrorsList
(
String
jtlString
)
{
Map
<
String
,
Object
>
statisticsDataMap
=
ResultDataParse
.
getSummryDataMap
(
jtlString
,
new
ErrorsSummaryConsumer
());
return
ResultDataParse
.
summaryMapParsing
(
statisticsDataMap
,
Errors
.
class
);
}
public
static
List
<
ErrorsTop5
>
getErrorsTop5List
(
String
jtlString
)
{
Map
<
String
,
Object
>
statisticsDataMap
=
ResultDataParse
.
getSummryDataMap
(
jtlString
,
new
Top5ErrorsBySamplerConsumer
());
return
ResultDataParse
.
summaryMapParsing
(
statisticsDataMap
,
ErrorsTop5
.
class
);
}
public
static
List
<
Statistics
>
getRequestStatistics
(
String
jtlString
)
{
Map
<
String
,
Object
>
statisticsDataMap
=
ResultDataParse
.
getSummryDataMap
(
jtlString
,
new
StatisticsSummaryConsumer
());
return
ResultDataParse
.
summaryMapParsing
(
statisticsDataMap
,
Statistics
.
class
);
}
public
static
TestOverview
getTestOverview
(
String
jtlString
)
{
DecimalFormat
decimalFormat
=
new
DecimalFormat
(
"0.00"
);
Map
<
String
,
Object
>
activeDataMap
=
ResultDataParse
.
getGraphDataMap
(
jtlString
,
new
ActiveThreadsGraphConsumer
());
List
<
ChartsData
>
usersList
=
ResultDataParse
.
graphMapParsing
(
activeDataMap
,
"users"
);
Optional
<
ChartsData
>
max
=
usersList
.
stream
().
max
(
Comparator
.
comparing
(
ChartsData:
:
getyAxis
));
int
maxUser
=
max
.
get
().
getyAxis
().
setScale
(
0
,
BigDecimal
.
ROUND_UP
).
intValue
();
Map
<
String
,
Object
>
hitsDataMap
=
ResultDataParse
.
getGraphDataMap
(
jtlString
,
new
HitsPerSecondGraphConsumer
());
List
<
ChartsData
>
hitsList
=
ResultDataParse
.
graphMapParsing
(
hitsDataMap
,
"hits"
);
double
hits
=
hitsList
.
stream
().
map
(
ChartsData:
:
getyAxis
)
.
mapToDouble
(
BigDecimal:
:
doubleValue
)
.
average
().
orElse
(
0
);
Map
<
String
,
Object
>
errorDataMap
=
ResultDataParse
.
getSummryDataMap
(
jtlString
,
new
StatisticsSummaryConsumer
());
List
<
Statistics
>
statisticsList
=
ResultDataParse
.
summaryMapParsing
(
errorDataMap
,
Statistics
.
class
);
Optional
<
Double
>
error
=
statisticsList
.
stream
().
map
(
item
->
Double
.
parseDouble
(
item
.
getError
())).
reduce
(
Double:
:
sum
);
double
avgTp90
=
statisticsList
.
stream
().
map
(
item
->
Double
.
parseDouble
(
item
.
getTp90
())).
mapToDouble
(
Double:
:
doubleValue
).
average
().
orElse
(
0
);
double
avgBandwidth
=
statisticsList
.
stream
().
map
(
item
->
Double
.
parseDouble
(
item
.
getReceived
())).
mapToDouble
(
Double:
:
doubleValue
).
average
().
orElse
(
0
);
Map
<
String
,
Object
>
responseDataMap
=
ResultDataParse
.
getGraphDataMap
(
jtlString
,
new
ResponseTimeOverTimeGraphConsumer
());
List
<
ChartsData
>
responseDataList
=
ResultDataParse
.
graphMapParsing
(
responseDataMap
,
"response"
);
double
responseTime
=
responseDataList
.
stream
().
map
(
ChartsData:
:
getyAxis
)
.
mapToDouble
(
BigDecimal:
:
doubleValue
)
.
average
().
orElse
(
0
);
TestOverview
testOverview
=
new
TestOverview
();
testOverview
.
setMaxUsers
(
String
.
valueOf
(
maxUser
));
testOverview
.
setAvgThroughput
(
decimalFormat
.
format
(
hits
));
testOverview
.
setErrors
(
decimalFormat
.
format
(
error
.
get
()));
testOverview
.
setAvgResponseTime
(
decimalFormat
.
format
(
responseTime
/
1000
));
testOverview
.
setResponseTime90
(
decimalFormat
.
format
(
avgTp90
/
1000
));
testOverview
.
setAvgBandwidth
(
decimalFormat
.
format
(
avgBandwidth
));
return
testOverview
;
}
public
static
List
<
ChartsData
>
getLoadChartData
(
String
jtlString
)
{
Map
<
String
,
Object
>
activeThreadMap
=
ResultDataParse
.
getGraphDataMap
(
jtlString
,
new
ActiveThreadsGraphConsumer
());
Map
<
String
,
Object
>
hitsMap
=
ResultDataParse
.
getGraphDataMap
(
jtlString
,
new
HitsPerSecondGraphConsumer
());
List
<
ChartsData
>
resultList
=
ResultDataParse
.
graphMapParsing
(
activeThreadMap
,
"users"
);
List
<
ChartsData
>
hitsList
=
ResultDataParse
.
graphMapParsing
(
hitsMap
,
"hits"
);
resultList
.
addAll
(
hitsList
);
return
resultList
;
}
public
static
List
<
ChartsData
>
getResponseTimeChartData
(
String
jtlString
)
{
Map
<
String
,
Object
>
activeThreadMap
=
ResultDataParse
.
getGraphDataMap
(
jtlString
,
new
ActiveThreadsGraphConsumer
());
Map
<
String
,
Object
>
responseTimeMap
=
ResultDataParse
.
getGraphDataMap
(
jtlString
,
new
ResponseTimeOverTimeGraphConsumer
());
List
<
ChartsData
>
resultList
=
ResultDataParse
.
graphMapParsing
(
activeThreadMap
,
"users"
);
List
<
ChartsData
>
responseTimeList
=
ResultDataParse
.
graphMapParsing
(
responseTimeMap
,
"responseTime"
);
resultList
.
addAll
(
responseTimeList
);
return
resultList
;
}
public
static
ReportTimeInfo
getReportTimeInfo
(
String
jtlString
)
{
List
<
Metric
>
totalLineList
=
GenerateReport
.
resolver
(
jtlString
);
totalLineList
.
sort
(
Comparator
.
comparing
(
t0
->
Long
.
valueOf
(
t0
.
getTimestamp
())));
String
startTimeStamp
=
totalLineList
.
get
(
0
).
getTimestamp
();
String
endTimeStamp
=
totalLineList
.
get
(
totalLineList
.
size
()
-
1
).
getTimestamp
();
DateTimeFormatter
dtf
=
DateTimeFormatter
.
ofPattern
(
"yyyy/MM/dd HH:mm:ss"
);
String
startTime
=
dtf
.
format
(
LocalDateTime
.
ofInstant
(
Instant
.
ofEpochMilli
(
Long
.
parseLong
(
startTimeStamp
)),
ZoneId
.
systemDefault
()));
String
endTime
=
dtf
.
format
(
LocalDateTime
.
ofInstant
(
Instant
.
ofEpochMilli
(
Long
.
parseLong
(
endTimeStamp
)),
ZoneId
.
systemDefault
()));
// todo 时间问题
long
seconds
=
Duration
.
between
(
Instant
.
ofEpochMilli
(
Long
.
parseLong
(
startTimeStamp
)),
Instant
.
ofEpochMilli
(
Long
.
parseLong
(
endTimeStamp
))).
getSeconds
();
ReportTimeInfo
reportTimeInfo
=
new
ReportTimeInfo
();
reportTimeInfo
.
setStartTime
(
startTime
);
reportTimeInfo
.
setEndTime
(
endTime
);
reportTimeInfo
.
setDuration
(
String
.
valueOf
(
seconds
));
return
reportTimeInfo
;
}
}
\ No newline at end of file
backend/src/main/java/io/metersphere/report/parse/ResultDataParse.java
已删除
100644 → 0
浏览文件 @
61e88258
package
io.metersphere.report.parse
;
import
io.metersphere.commons.utils.MsJMeterUtils
;
import
io.metersphere.report.base.ChartsData
;
import
org.apache.jmeter.report.core.Sample
;
import
org.apache.jmeter.report.core.SampleMetadata
;
import
org.apache.jmeter.report.dashboard.JsonizerVisitor
;
import
org.apache.jmeter.report.processor.*
;
import
org.apache.jmeter.report.processor.graph.AbstractOverTimeGraphConsumer
;
import
java.lang.reflect.Field
;
import
java.math.BigDecimal
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.time.Instant
;
import
java.time.LocalDateTime
;
import
java.time.ZoneId
;
import
java.time.format.DateTimeFormatter
;
import
java.util.*
;
public
class
ResultDataParse
{
private
static
final
String
DATE_TIME_PATTERN
=
"yyyy/MM/dd HH:mm:ss"
;
private
static
final
String
TIME_PATTERN
=
"HH:mm:ss"
;
public
static
<
T
>
List
<
T
>
summaryMapParsing
(
Map
<
String
,
Object
>
map
,
Class
<
T
>
clazz
)
{
List
<
T
>
list
=
new
ArrayList
<>();
for
(
String
key
:
map
.
keySet
())
{
MapResultData
mapResultData
=
(
MapResultData
)
map
.
get
(
key
);
ListResultData
items
=
(
ListResultData
)
mapResultData
.
getResult
(
"items"
);
if
(
items
.
getSize
()
>
0
)
{
for
(
int
i
=
0
;
i
<
items
.
getSize
();
i
++)
{
MapResultData
resultData
=
(
MapResultData
)
items
.
get
(
i
);
ListResultData
data
=
(
ListResultData
)
resultData
.
getResult
(
"data"
);
int
size
=
data
.
getSize
();
String
[]
strArray
=
new
String
[
size
];
if
(
size
>
0
)
{
T
t
=
null
;
for
(
int
j
=
0
;
j
<
size
;
j
++)
{
ValueResultData
valueResultData
=
(
ValueResultData
)
data
.
get
(
j
);
if
(
valueResultData
.
getValue
()
==
null
)
{
strArray
[
j
]
=
""
;
}
else
{
String
accept
=
valueResultData
.
accept
(
new
JsonizerVisitor
());
strArray
[
j
]
=
accept
.
replace
(
"\\"
,
""
);
}
}
try
{
t
=
setParam
(
clazz
,
strArray
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
list
.
add
(
t
);
}
}
}
}
return
list
;
}
public
static
List
<
ChartsData
>
graphMapParsing
(
Map
<
String
,
Object
>
map
,
String
seriesName
)
{
List
<
ChartsData
>
list
=
new
ArrayList
<>();
// ThreadGroup
for
(
String
key
:
map
.
keySet
())
{
MapResultData
mapResultData
=
(
MapResultData
)
map
.
get
(
key
);
ResultData
maxY
=
mapResultData
.
getResult
(
"maxY"
);
ListResultData
series
=
(
ListResultData
)
mapResultData
.
getResult
(
"series"
);
if
(
series
.
getSize
()
>
0
)
{
for
(
int
j
=
0
;
j
<
series
.
getSize
();
j
++)
{
MapResultData
resultData
=
(
MapResultData
)
series
.
get
(
j
);
// data, isOverall, label, isController
ListResultData
data
=
(
ListResultData
)
resultData
.
getResult
(
"data"
);
ValueResultData
label
=
(
ValueResultData
)
resultData
.
getResult
(
"label"
);
if
(
data
.
getSize
()
>
0
)
{
for
(
int
i
=
0
;
i
<
data
.
getSize
();
i
++)
{
ListResultData
listResultData
=
(
ListResultData
)
data
.
get
(
i
);
String
result
=
listResultData
.
accept
(
new
JsonizerVisitor
());
result
=
result
.
substring
(
1
,
result
.
length
()
-
1
);
String
[]
split
=
result
.
split
(
","
);
ChartsData
chartsData
=
new
ChartsData
();
BigDecimal
bigDecimal
=
new
BigDecimal
(
split
[
0
]);
String
timeStamp
=
bigDecimal
.
toPlainString
();
String
time
=
null
;
try
{
time
=
formatDate
(
stampToDate
(
DATE_TIME_PATTERN
,
timeStamp
));
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
();
}
chartsData
.
setxAxis
(
time
);
chartsData
.
setyAxis
(
new
BigDecimal
(
split
[
1
].
trim
()));
if
(
series
.
getSize
()
==
1
)
{
chartsData
.
setGroupName
(
seriesName
);
}
else
{
chartsData
.
setGroupName
((
String
)
label
.
getValue
());
}
list
.
add
(
chartsData
);
}
}
}
}
}
return
list
;
}
public
static
Map
<
String
,
Object
>
getGraphDataMap
(
String
jtlString
,
AbstractOverTimeGraphConsumer
timeGraphConsumer
)
{
AbstractOverTimeGraphConsumer
abstractOverTimeGraphConsumer
=
timeGraphConsumer
;
abstractOverTimeGraphConsumer
.
setGranularity
(
60000
);
abstractOverTimeGraphConsumer
.
initialize
();
SampleContext
sampleContext
=
initJmeterConsumer
(
jtlString
,
abstractOverTimeGraphConsumer
);
return
sampleContext
.
getData
();
}
public
static
Map
<
String
,
Object
>
getSummryDataMap
(
String
jtlString
,
AbstractSummaryConsumer
<?>
summaryConsumer
)
{
AbstractSummaryConsumer
<?>
abstractSummaryConsumer
=
summaryConsumer
;
SampleContext
sampleContext
=
initJmeterConsumer
(
jtlString
,
summaryConsumer
);
return
sampleContext
.
getData
();
}
private
static
SampleContext
initJmeterConsumer
(
String
jtlString
,
AbstractSampleConsumer
abstractSampleConsumer
)
{
int
row
=
0
;
// 使用反射获取properties
MsJMeterUtils
.
loadJMeterProperties
(
"jmeter.properties"
);
SampleMetadata
sampleMetaData
=
createTestMetaData
();
SampleContext
sampleContext
=
new
SampleContext
();
abstractSampleConsumer
.
setSampleContext
(
sampleContext
);
abstractSampleConsumer
.
startConsuming
();
StringTokenizer
tokenizer
=
new
StringTokenizer
(
jtlString
,
"\n"
);
// 去掉第一行
tokenizer
.
nextToken
();
while
(
tokenizer
.
hasMoreTokens
())
{
String
line
=
tokenizer
.
nextToken
();
String
[]
data
=
line
.
split
(
","
,
-
1
);
Sample
sample
=
new
Sample
(
row
++,
sampleMetaData
,
data
);
abstractSampleConsumer
.
consume
(
sample
,
0
);
}
abstractSampleConsumer
.
stopConsuming
();
return
sampleContext
;
}
// Create a static SampleMetadataObject
private
static
SampleMetadata
createTestMetaData
()
{
String
columnsString
=
"timeStamp,elapsed,label,responseCode,responseMessage,threadName,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect"
;
columnsString
=
"timeStamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect"
;
String
[]
columns
=
new
String
[
17
];
int
lastComa
=
0
;
int
columnIndex
=
0
;
for
(
int
i
=
0
;
i
<
columnsString
.
length
();
i
++)
{
if
(
columnsString
.
charAt
(
i
)
==
','
)
{
columns
[
columnIndex
]
=
columnsString
.
substring
(
lastComa
,
i
);
lastComa
=
i
+
1
;
columnIndex
++;
}
else
if
(
i
+
1
==
columnsString
.
length
())
{
columns
[
columnIndex
]
=
columnsString
.
substring
(
lastComa
,
i
+
1
);
}
}
return
new
SampleMetadata
(
','
,
columns
);
}
private
static
String
stampToDate
(
String
pattern
,
String
timeStamp
)
{
DateTimeFormatter
dateTimeFormatter
=
DateTimeFormatter
.
ofPattern
(
pattern
);
LocalDateTime
localDateTime
=
LocalDateTime
.
ofInstant
(
Instant
.
ofEpochMilli
(
Long
.
parseLong
(
timeStamp
)),
ZoneId
.
systemDefault
());
return
localDateTime
.
format
(
dateTimeFormatter
);
}
private
static
String
formatDate
(
String
dateString
)
throws
ParseException
{
SimpleDateFormat
before
=
new
SimpleDateFormat
(
DATE_TIME_PATTERN
);
SimpleDateFormat
after
=
new
SimpleDateFormat
(
TIME_PATTERN
);
return
after
.
format
(
before
.
parse
(
dateString
));
}
private
static
<
T
>
T
setParam
(
Class
<
T
>
clazz
,
Object
[]
args
)
throws
Exception
{
if
(
clazz
==
null
||
args
==
null
)
{
throw
new
IllegalArgumentException
();
}
T
t
=
clazz
.
newInstance
();
Field
[]
fields
=
clazz
.
getDeclaredFields
();
if
(
fields
==
null
||
fields
.
length
>
args
.
length
)
{
throw
new
IndexOutOfBoundsException
();
}
for
(
int
i
=
0
;
i
<
fields
.
length
;
i
++)
{
fields
[
i
].
setAccessible
(
true
);
fields
[
i
].
set
(
t
,
args
[
i
]);
}
return
t
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录