chen.lin
昨天 9f724c61dfa4dc4c0eea66253ea0780b023622ae
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import React, { useMemo } from 'react';
import {
    Box,
    Typography,
    Paper,
    Table,
    TableBody,
    TableCell,
    TableContainer,
    TableHead,
    TableRow,
    Chip,
    Stack,
} from '@mui/material';
 
/**
 * 汇总报告组件
 * 参考JMeter的SummaryReport,显示统计信息
 */
const SummaryReport = ({ results = [] }) => {
    const statistics = useMemo(() => {
        if (results.length === 0) {
            return {
                total: 0,
                success: 0,
                failed: 0,
                successRate: 0,
                avgResponseTime: 0,
                minResponseTime: 0,
                maxResponseTime: 0,
                totalTime: 0,
            };
        }
 
        const successfulResults = results.filter(r => r.success);
        const failedResults = results.filter(r => !r.success);
        
        const responseTimes = results
            .map(r => {
                if (r.startTime && r.endTime) {
                    return new Date(r.endTime) - new Date(r.startTime);
                }
                return 0;
            })
            .filter(t => t > 0);
 
        const totalTime = results.length > 0 
            ? new Date(results[results.length - 1].endTime || Date.now()) - new Date(results[0].startTime)
            : 0;
 
        const avgResponseTime = responseTimes.length > 0
            ? responseTimes.reduce((sum, t) => sum + t, 0) / responseTimes.length
            : 0;
 
        const minResponseTime = responseTimes.length > 0 
            ? Math.min(...responseTimes) 
            : 0;
 
        const maxResponseTime = responseTimes.length > 0 
            ? Math.max(...responseTimes) 
            : 0;
 
        return {
            total: results.length,
            success: successfulResults.length,
            failed: failedResults.length,
            successRate: results.length > 0 ? (successfulResults.length / results.length * 100) : 0,
            avgResponseTime: Math.round(avgResponseTime),
            minResponseTime: Math.round(minResponseTime),
            maxResponseTime: Math.round(maxResponseTime),
            totalTime: Math.round(totalTime),
            throughput: totalTime > 0 ? (results.length / (totalTime / 1000)).toFixed(2) : 0, // 每秒请求数
        };
    }, [results]);
 
    const formatTime = (ms) => {
        if (ms === 0) return 'N/A';
        return `${ms}ms`;
    };
 
    return (
        <Box>
            <Typography variant="h6" gutterBottom>
                汇总报告
            </Typography>
            
            <Paper variant="outlined" sx={{ p: 2, mb: 2 }}>
                <Stack direction="row" spacing={2} flexWrap="wrap">
                    <Box>
                        <Typography variant="caption" color="text.secondary">样本数</Typography>
                        <Typography variant="h6">{statistics.total}</Typography>
                    </Box>
                    <Box>
                        <Typography variant="caption" color="text.secondary">成功</Typography>
                        <Typography variant="h6" color="success.main">{statistics.success}</Typography>
                    </Box>
                    <Box>
                        <Typography variant="caption" color="text.secondary">失败</Typography>
                        <Typography variant="h6" color="error.main">{statistics.failed}</Typography>
                    </Box>
                    <Box>
                        <Typography variant="caption" color="text.secondary">成功率</Typography>
                        <Typography variant="h6">
                            {statistics.successRate.toFixed(2)}%
                        </Typography>
                    </Box>
                    <Box>
                        <Typography variant="caption" color="text.secondary">平均响应时间</Typography>
                        <Typography variant="h6">{formatTime(statistics.avgResponseTime)}</Typography>
                    </Box>
                    <Box>
                        <Typography variant="caption" color="text.secondary">最小响应时间</Typography>
                        <Typography variant="h6">{formatTime(statistics.minResponseTime)}</Typography>
                    </Box>
                    <Box>
                        <Typography variant="caption" color="text.secondary">最大响应时间</Typography>
                        <Typography variant="h6">{formatTime(statistics.maxResponseTime)}</Typography>
                    </Box>
                    <Box>
                        <Typography variant="caption" color="text.secondary">总耗时</Typography>
                        <Typography variant="h6">{formatTime(statistics.totalTime)}</Typography>
                    </Box>
                    <Box>
                        <Typography variant="caption" color="text.secondary">吞吐量</Typography>
                        <Typography variant="h6">{statistics.throughput} req/s</Typography>
                    </Box>
                </Stack>
            </Paper>
 
            <TableContainer component={Paper} variant="outlined">
                <Table size="small">
                    <TableHead>
                        <TableRow>
                            <TableCell>步骤名称</TableCell>
                            <TableCell align="right">样本数</TableCell>
                            <TableCell align="right">成功</TableCell>
                            <TableCell align="right">失败</TableCell>
                            <TableCell align="right">成功率</TableCell>
                            <TableCell align="right">平均响应时间</TableCell>
                            <TableCell align="right">最小响应时间</TableCell>
                            <TableCell align="right">最大响应时间</TableCell>
                        </TableRow>
                    </TableHead>
                    <TableBody>
                        {(() => {
                            // 按步骤类型分组统计
                            const groupedResults = {};
                            results.forEach(result => {
                                const key = result.nodeName || result.nodeType || 'unknown';
                                if (!groupedResults[key]) {
                                    groupedResults[key] = [];
                                }
                                groupedResults[key].push(result);
                            });
 
                            return Object.keys(groupedResults).map(key => {
                                const groupResults = groupedResults[key];
                                const successCount = groupResults.filter(r => r.success).length;
                                const responseTimes = groupResults
                                    .map(r => {
                                        if (r.startTime && r.endTime) {
                                            return new Date(r.endTime) - new Date(r.startTime);
                                        }
                                        return 0;
                                    })
                                    .filter(t => t > 0);
 
                                const avgTime = responseTimes.length > 0
                                    ? Math.round(responseTimes.reduce((sum, t) => sum + t, 0) / responseTimes.length)
                                    : 0;
                                const minTime = responseTimes.length > 0 ? Math.min(...responseTimes) : 0;
                                const maxTime = responseTimes.length > 0 ? Math.max(...responseTimes) : 0;
 
                                return (
                                    <TableRow key={key}>
                                        <TableCell>{key}</TableCell>
                                        <TableCell align="right">{groupResults.length}</TableCell>
                                        <TableCell align="right">
                                            <Chip 
                                                label={successCount} 
                                                size="small" 
                                                color="success"
                                                variant="outlined"
                                            />
                                        </TableCell>
                                        <TableCell align="right">
                                            <Chip 
                                                label={groupResults.length - successCount} 
                                                size="small" 
                                                color="error"
                                                variant="outlined"
                                            />
                                        </TableCell>
                                        <TableCell align="right">
                                            {groupResults.length > 0 
                                                ? ((successCount / groupResults.length) * 100).toFixed(2) + '%'
                                                : '0%'}
                                        </TableCell>
                                        <TableCell align="right">{formatTime(avgTime)}</TableCell>
                                        <TableCell align="right">{formatTime(minTime)}</TableCell>
                                        <TableCell align="right">{formatTime(maxTime)}</TableCell>
                                    </TableRow>
                                );
                            });
                        })()}
                    </TableBody>
                </Table>
            </TableContainer>
        </Box>
    );
};
 
export default SummaryReport;