APITest.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <template>
  2. <ElTabs class="tabs" v-model="activeTab">
  3. <ElTabPane label="接口测试" name="apitest">
  4. <ElTable :data="apitests" width="100%">
  5. <ElTableColumn
  6. prop="testId"
  7. label="ID"
  8. align="center"
  9. width="60">
  10. </ElTableColumn>
  11. <ElTableColumn
  12. prop="testName"
  13. label="测试名称"
  14. align="center"
  15. width="300">
  16. </ElTableColumn>
  17. <ElTableColumn
  18. prop="username"
  19. label="创建者"
  20. align="center"
  21. width="180">
  22. </ElTableColumn>
  23. <ElTableColumn
  24. prop="createTime"
  25. label="创建时间"
  26. align="center"
  27. width="250">
  28. </ElTableColumn>
  29. <ElTableColumn
  30. label="详情"
  31. align="center">
  32. <template #default="scope">
  33. <ElButton
  34. @click="handleClick('latest', scope.row.testId)"
  35. type="text"
  36. size="small">最近执行详情</ElButton>
  37. <ElButton @click="handleClick('detail', scope.row.testId)" type="text" size="small">测试任务详情</ElButton>
  38. </template>
  39. </ElTableColumn>
  40. <ElTableColumn
  41. align="center">
  42. <template #header>
  43. <span>操作</span>
  44. <ElButton @click="handleClick('create')" type="text" class="add-button">新建</ElButton>
  45. </template>
  46. <template #default="scope">
  47. <ElButton @click="handleClick('trigger', scope.row.testId)" type="text" size="small">触发</ElButton>
  48. <ElButton @click="handleClick('delete', scope.row.testId)" type="text" size="small">删除</ElButton>
  49. </template>
  50. </ElTableColumn>
  51. </ElTable>
  52. <!-- 测试任务详情 -->
  53. <ElDrawer
  54. v-model="detailDrawer"
  55. :size="800">
  56. <template #title>
  57. <el-row class="entry-row">
  58. <el-col :span="2">
  59. <div class="node-type">测试</div>
  60. </el-col>
  61. <el-col :span="2">
  62. <template class="entry-row">
  63. <span>#{{detailAPIInfo.testId}}</span>
  64. </template>
  65. </el-col>
  66. <el-col :span="4">
  67. <template class="entry-row">
  68. <span>{{detailAPIInfo.testName}}</span>
  69. </template>
  70. </el-col>
  71. </el-row>
  72. </template>
  73. <template #default>
  74. <el-row type="flex" justify="center">
  75. <el-col :span="20">
  76. <el-tabs
  77. v-model="detailTab">
  78. <el-tab-pane label="基本信息" name="basic_info">
  79. <el-row class="entry-row" style="margin-top: 15px">
  80. <el-col :span="4">
  81. <h4>url: </h4>
  82. </el-col>
  83. <el-col :span="18">
  84. <div>{{detailAPIInfo.url}}</div>
  85. </el-col>
  86. </el-row>
  87. <el-row class="entry-row" style="margin-top: 15px">
  88. <el-col :span="4">
  89. <h4>params: </h4>
  90. </el-col>
  91. <el-col :span="18">
  92. <div>
  93. <vue-json-editor
  94. :height="'400px'"
  95. v-model="detailAPIInfo.params"
  96. :show-btns="false"
  97. :mode="'code'"
  98. :modes="['code']"
  99. lang="zh"></vue-json-editor>
  100. </div>
  101. </el-col>
  102. </el-row>
  103. <el-row class="entry-row" style="margin-top: 15px">
  104. <el-col :span="4">
  105. <h4>method: </h4>
  106. </el-col>
  107. <el-col :span="18">
  108. <div>{{detailAPIInfo.method}}</div>
  109. </el-col>
  110. </el-row>
  111. <el-row class="entry-row" style="margin-top: 15px">
  112. <el-col :span="4">
  113. <h4>qps: </h4>
  114. </el-col>
  115. <el-col :span="18">
  116. <div>{{detailAPIInfo.qps}}</div>
  117. </el-col>
  118. </el-row>
  119. </el-tab-pane>
  120. <el-tab-pane label="执行历史" name="execute_history">
  121. <h5>注:Max、Min、Average的单位均为ms</h5>
  122. <el-table :data="testResults" width="100%">
  123. <el-table-column
  124. prop="id"
  125. label="ID"
  126. align="center"
  127. width="60">
  128. </el-table-column>
  129. <el-table-column
  130. prop="executeTime"
  131. label="执行时间"
  132. align="center">
  133. </el-table-column>
  134. <el-table-column
  135. prop="success"
  136. label="成功"
  137. align="center">
  138. </el-table-column>
  139. <el-table-column
  140. prop="failure"
  141. label="失败"
  142. align="center">
  143. <template #default="scope">
  144. {{scope.row.failure}}
  145. <span v-if="scope.row.success === 0">
  146. <i class="el-icon-warning" title="url/参数有误或者服务器出错!"></i>
  147. </span>
  148. </template>
  149. </el-table-column>
  150. <el-table-column
  151. prop="maxTime"
  152. label="Max"
  153. align="center">
  154. </el-table-column>
  155. <el-table-column
  156. prop="minTime"
  157. label="Min"
  158. align="center">
  159. </el-table-column>
  160. <el-table-column
  161. prop="averageTime"
  162. label="Average"
  163. align="center">
  164. </el-table-column>
  165. </el-table>
  166. </el-tab-pane>
  167. </el-tabs>
  168. </el-col>
  169. </el-row>
  170. </template>
  171. </ElDrawer>
  172. <!-- 新建测试任务 -->
  173. <el-dialog
  174. v-model="showAPITestCreateModel"
  175. width="600px"
  176. :before-close="(done) => {$refs['createAPITestForm'].resetFields();done();}"
  177. destroy-on-close>
  178. <template #title>创建测试任务</template>
  179. <el-form
  180. :model="createAPITestForm"
  181. ref="createAPITestForm"
  182. label-position="right"
  183. label-width="100px"
  184. label-suffix=":"
  185. class="create-APITest-form">
  186. <el-form-item label="测试名称" prop="testName" required>
  187. <el-input v-model="createAPITestForm.testName" placeholder="请输入接口测试的名称"></el-input>
  188. </el-form-item>
  189. <el-form-item label="待测URL" prop="url" required>
  190. <el-input v-model="createAPITestForm.url" placeholder="请输入待测试的URL"></el-input>
  191. </el-form-item>
  192. <el-form-item label="参数" prop="params">
  193. <div>
  194. <vue-json-editor
  195. :height="'400px'"
  196. v-model="createAPITestForm.params"
  197. :show-btns="false"
  198. :mode="'code'"
  199. :modes="['code']"
  200. lang="zh"
  201. @json-change="onJsonChange"
  202. @json-save="onJsonSave"
  203. @has-error="onError"
  204. ></vue-json-editor>
  205. <span v-if="!jsonValid" style="color: #F56C6C; font-size: 12px">JSON is invalid.</span>
  206. </div>
  207. </el-form-item>
  208. <el-form-item label="方法" prop="method" required>
  209. <ElSelect v-model="createAPITestForm.method" placeholder="请选择待测URL的方法">
  210. <ElOption
  211. v-for="(method, index) in methodList"
  212. :key="index" :label="method"
  213. :value="method">
  214. </ElOption>
  215. </ElSelect>
  216. </el-form-item>
  217. <el-form-item label="并发数" prop="qps" required>
  218. <el-input v-model="createAPITestForm.qps" placeholder="请输入并发数"></el-input>
  219. </el-form-item>
  220. </el-form>
  221. <template #footer>
  222. <span class="dialog-footer">
  223. <el-button type="primary" @click="handleAPITestCreateConfirm">确 定</el-button>
  224. </span>
  225. </template>
  226. </el-dialog>
  227. <ElDrawer
  228. v-model="latestDrawer"
  229. :size="750">
  230. <template #title>
  231. <el-row class="entry-row">
  232. <el-col :span="3">
  233. <div class="node-type">测试结果</div>
  234. </el-col>
  235. <el-col :span="2">
  236. <template class="entry-row">
  237. <span>#{{latestResultInfo.id}}</span>
  238. </template>
  239. </el-col>
  240. </el-row>
  241. </template>
  242. <template #default>
  243. <el-row type="flex" justify="center">
  244. <el-col :span="22">
  245. <el-row class="entry-row" style="margin-top: 15px">
  246. <el-col :span="5">
  247. <h4>执行时间:</h4>
  248. </el-col>
  249. <el-col :span="18">
  250. <div>{{latestResultInfo.executeTime}}</div>
  251. </el-col>
  252. </el-row>
  253. <el-row class="entry-row" style="margin-top: 15px">
  254. <el-col :span="5">
  255. <h4>成功:</h4>
  256. </el-col>
  257. <el-col :span="18">
  258. <div>{{latestResultInfo.success}}</div>
  259. </el-col>
  260. </el-row>
  261. <el-row class="entry-row" style="margin-top: 15px">
  262. <el-col :span="5">
  263. <h4>失败:</h4>
  264. </el-col>
  265. <el-col :span="18">
  266. <div>
  267. {{latestResultInfo.failure}}
  268. <span v-if="latestResultInfo.success === 0" style="color: red">(url/参数输入有误或者服务器出错!)</span>
  269. </div>
  270. </el-col>
  271. </el-row>
  272. <el-row class="entry-row" style="margin-top: 15px">
  273. <el-col :span="5">
  274. <h4>maxTime:</h4>
  275. </el-col>
  276. <el-col :span="18">
  277. <div>{{latestResultInfo.maxTime}} ms</div>
  278. </el-col>
  279. </el-row>
  280. <el-row class="entry-row" style="margin-top: 15px">
  281. <el-col :span="5">
  282. <h4>minTime:</h4>
  283. </el-col>
  284. <el-col :span="18">
  285. <div>{{latestResultInfo.minTime}} ms</div>
  286. </el-col>
  287. </el-row>
  288. <el-row class="entry-row" style="margin-top: 15px">
  289. <el-col :span="5">
  290. <h4>averageTime:</h4>
  291. </el-col>
  292. <el-col :span="18">
  293. <div>{{latestResultInfo.averageTime}} ms</div>
  294. </el-col>
  295. </el-row>
  296. </el-col>
  297. </el-row>
  298. <el-row>
  299. <el-col :span="12">
  300. <div id="chart1" style="height: 500px; width: 350px"></div>
  301. </el-col>
  302. <el-col :span="12">
  303. <div id="chart2" style="height: 500px; width: 350px"></div>
  304. </el-col>
  305. </el-row>
  306. </template>
  307. </ElDrawer>
  308. </ElTabPane>
  309. </ElTabs>
  310. </template>
  311. <script>
  312. import vueJsonEditor from 'vue-json-editor';
  313. import { APITestAPI } from '@/api';
  314. import { ElMessage } from 'element-plus';
  315. import * as echarts from 'echarts';
  316. export default {
  317. components: {
  318. vueJsonEditor,
  319. },
  320. data() {
  321. return {
  322. activeTab: 'apitest',
  323. apitests: [],
  324. detailDrawer: false,
  325. detailAPIInfo: [],
  326. detailTab: 'basic_info',
  327. testResults: [],
  328. showAPITestCreateModel: false,
  329. createAPITestForm: {
  330. testName: '',
  331. url: '',
  332. params: {},
  333. method: '',
  334. qps: '',
  335. createTime: '',
  336. username: '',
  337. },
  338. methodList: ['GET', 'POST'],
  339. jsonValid: true,
  340. latestDrawer: false,
  341. latestResultInfo: {},
  342. qps: 0,
  343. timer: null,
  344. };
  345. },
  346. async mounted() {
  347. this.apitests = await APITestAPI.getAPITestInfosByProjectId(this.$store.state.workspace.currentProjectId);
  348. },
  349. methods: {
  350. async handleClick(action, id) {
  351. switch (action) {
  352. case 'create': {
  353. this.showAPITestCreateModel = true;
  354. break;
  355. }
  356. case 'latest': {
  357. this.latestResultInfo = await APITestAPI.getLatestResultByTestId(id);
  358. this.apitests.forEach((item) => {
  359. if (item.testId === id) {
  360. this.qps = item.qps;
  361. }
  362. });
  363. this.latestDrawer = true;
  364. const that = this;
  365. this.timer = setTimeout(() => {
  366. this.$nextTick(() => {
  367. that.initChart1();
  368. that.initChart2();
  369. });
  370. }, 1500);
  371. break;
  372. }
  373. case 'detail': {
  374. this.apitests.forEach((item) => {
  375. if (item.testId === id) {
  376. this.detailAPIInfo = item;
  377. }
  378. });
  379. this.testResults = await APITestAPI.getAPITestResultByTestId(id);
  380. this.detailDrawer = true;
  381. break;
  382. }
  383. case 'trigger': {
  384. await APITestAPI.executeAPITestByTestId(id);
  385. break;
  386. }
  387. case 'delete': {
  388. await APITestAPI.deleteAPITestByTestId(id);
  389. ElMessage.success({
  390. message: '删除成功!',
  391. type: 'success',
  392. });
  393. this.apitests = await APITestAPI.getAPITestInfosByProjectId(this.$store.state.workspace.currentProjectId);
  394. break;
  395. }
  396. default: {
  397. console.warn('Unknown type of action.');
  398. }
  399. }
  400. },
  401. onJsonChange(value) {
  402. this.onJsonSave(value);
  403. },
  404. onJsonSave(value) {
  405. this.createAPITestForm.params = value;
  406. console.log(this.createAPITestForm.params);
  407. this.jsonValid = true;
  408. },
  409. onError(value) {
  410. this.jsonValid = false;
  411. },
  412. async handleAPITestCreateConfirm() {
  413. this.$refs.createAPITestForm.validate(async (valid) => {
  414. if (valid) {
  415. this.createAPITestForm.createTime = this.getCurrentTime();
  416. await APITestAPI.createAPITest({
  417. projectId: this.$store.state.workspace.currentProjectId,
  418. createTime: this.createAPITestForm.createTime,
  419. username: '',
  420. testName: this.createAPITestForm.testName,
  421. url: this.createAPITestForm.url,
  422. params: this.createAPITestForm.params,
  423. method: this.createAPITestForm.method,
  424. qps: this.createAPITestForm.qps,
  425. });
  426. this.apitests = await APITestAPI.getAPITestInfosByProjectId(this.$store.state.workspace.currentProjectId);
  427. this.$refs.createAPITestForm.resetFields();
  428. this.showAPITestCreateModel = false;
  429. ElMessage.success({
  430. message: '创建成功!',
  431. type: 'success',
  432. });
  433. }
  434. });
  435. },
  436. getCurrentTime() {
  437. const yy = new Date().getFullYear();
  438. const mm = new Date().getMonth() + 1;
  439. const dd = new Date().getDate();
  440. const hh = new Date().getHours();
  441. const mf = new Date().getMinutes() < 10 ? `0${new Date().getMinutes()}` : new Date().getMinutes();
  442. const ss = new Date().getSeconds() < 10 ? `0${new Date().getSeconds()}` : new Date().getSeconds();
  443. return `${yy}-${mm}-${dd} ${hh}:${mf}:${ss}`;
  444. },
  445. async initChart1() {
  446. const chart1 = echarts.init(document.getElementById('chart1'));
  447. const options1 = {
  448. title: {
  449. text: `并发数: ${this.qps}`,
  450. x: 'center',
  451. y: 'center',
  452. textStyle: {
  453. fontSize: 16,
  454. },
  455. },
  456. tooltip: {
  457. trigger: 'item',
  458. },
  459. series: [
  460. {
  461. type: 'pie',
  462. radius: ['40%', '50%'],
  463. clockwise: true,
  464. avoidLabelOverlap: true,
  465. hoverOffset: 15,
  466. data: [
  467. { value: this.latestResultInfo.success, name: '成功', itemStyle: { color: '#CCFF99' } },
  468. { value: this.latestResultInfo.failure, name: '失败', itemStyle: { color: '#FF6600' } },
  469. ],
  470. },
  471. ],
  472. };
  473. chart1.setOption(options1);
  474. },
  475. async initChart2() {
  476. const chart2 = echarts.init(document.getElementById('chart2'));
  477. const option2 = {
  478. title: {
  479. text: '访问时间曲线图',
  480. bottom: '5%',
  481. left: 'center',
  482. },
  483. tooltip: {
  484. trigger: 'trigger',
  485. axisPointer: {
  486. type: 'cross',
  487. label: {
  488. backgroundColor: '#6a7985',
  489. },
  490. },
  491. },
  492. xAxis: [
  493. {
  494. type: 'category',
  495. boundaryGap: false,
  496. },
  497. ],
  498. yAxis: [
  499. {
  500. name: '执行时间/单位ms',
  501. type: 'value',
  502. min: 'dataMin',
  503. max: 'dataMax',
  504. },
  505. ],
  506. series: [
  507. {
  508. name: '访问时间',
  509. data: this.latestResultInfo.timeList,
  510. type: 'line',
  511. smooth: true,
  512. showSymbol: true,
  513. symbolSize: 3,
  514. symbol: 'circle',
  515. itemStyle: { color: '#8D67FF' },
  516. markPoint: {
  517. data: [
  518. { type: 'max', name: '最大值' },
  519. { type: 'min', name: '最小值' },
  520. ],
  521. },
  522. markLine: {
  523. data: [
  524. { type: 'average', name: '平均值' },
  525. ],
  526. },
  527. },
  528. ],
  529. };
  530. chart2.setOption(option2);
  531. },
  532. },
  533. };
  534. </script>
  535. <style lang="scss" scoped>
  536. .tabs {
  537. width: 80%;
  538. margin: auto auto;
  539. }
  540. .add-button {
  541. margin-left: 14px;
  542. }
  543. .entry-row {
  544. width: 100%;
  545. margin: 3px 20px;
  546. display: flex;
  547. flex-direction: row;
  548. align-items: center;
  549. justify-content: flex-start;
  550. h3, h4{
  551. color: #172b4d;
  552. }
  553. }
  554. .node-type{
  555. color: #FFFFFF;
  556. border-radius: 3px;
  557. font-size: 14px;
  558. background-color: #42b983;
  559. text-align: center;
  560. }
  561. .create-APITest-form {
  562. width: 80%;
  563. margin: 0 auto;
  564. }
  565. </style>