From e1424c53df96c08ee4d5a9bc021130fa916af990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Allen=20Zhang=20=28=E5=BC=A0=E6=B6=9B=29?= Date: Mon, 1 Jul 2024 13:03:05 +0800 Subject: [PATCH] feat: use rust merge coverage --- packages/canyon-backend/package.json | 2 +- .../core/consumer-coverage.service.ts | 3 +- .../services/core/coveragedisk.service.ts | 2 +- packages/canyon-backend/src/utils/coverage.ts | 71 ++++++------------- 4 files changed, 24 insertions(+), 54 deletions(-) diff --git a/packages/canyon-backend/package.json b/packages/canyon-backend/package.json index 24e8b3be..95ff5dc6 100755 --- a/packages/canyon-backend/package.json +++ b/packages/canyon-backend/package.json @@ -29,7 +29,7 @@ "better-sqlite3": "^9.6.0", "body-parser": "^1.20.2", "camaro": "^6.2.3", - "canyon-data": "0.1.1-alpha.3", + "canyon-data": "^0.1.1-alpha.5", "class-transformer": "^0.5.1", "class-validator": "^0.14.1", "cookie-parser": "^1.4.6", diff --git a/packages/canyon-backend/src/coverage/services/core/consumer-coverage.service.ts b/packages/canyon-backend/src/coverage/services/core/consumer-coverage.service.ts index e4147f34..411d882e 100644 --- a/packages/canyon-backend/src/coverage/services/core/consumer-coverage.service.ts +++ b/packages/canyon-backend/src/coverage/services/core/consumer-coverage.service.ts @@ -3,8 +3,9 @@ import { PrismaService } from '../../../prisma/prisma.service'; import { genSummaryMapByCoverageMap, getSummaryByPath, - mergeCoverageMap, + // mergeCoverageMap, } from '@canyon/data'; +import {mergeCoverageMap} from "../../../utils/coverage"; import { percent, removeNullKeys } from '../../../utils/utils'; import { CoverageDataAdapterService } from '../common/coverage-data-adapter.service'; import { PullChangeCodeAndInsertDbService } from '../common/pull-change-code-and-insert-db.service'; diff --git a/packages/canyon-backend/src/coverage/services/core/coveragedisk.service.ts b/packages/canyon-backend/src/coverage/services/core/coveragedisk.service.ts index 463424f9..0a5b0a13 100644 --- a/packages/canyon-backend/src/coverage/services/core/coveragedisk.service.ts +++ b/packages/canyon-backend/src/coverage/services/core/coveragedisk.service.ts @@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { CoveragediskEntity } from '../../entity/coveragedisk.entity'; import { Repository } from 'typeorm'; -import {mergeCoverageMap} from "@canyon/data"; +import {mergeCoverageMap} from "../../../utils/coverage"; // import {merge_coverage_json_str} from 'canyon-data' @Injectable() diff --git a/packages/canyon-backend/src/utils/coverage.ts b/packages/canyon-backend/src/utils/coverage.ts index 7825203f..7d348674 100755 --- a/packages/canyon-backend/src/utils/coverage.ts +++ b/packages/canyon-backend/src/utils/coverage.ts @@ -1,5 +1,7 @@ import * as libCoverage from 'istanbul-lib-coverage'; import * as libSourceMaps from 'istanbul-lib-source-maps'; +import {mergeCoverageMap as mergeCoverageMapOfCanyonData} from "@canyon/data"; +import {merge_coverage_json_str} from "canyon-data"; function parseInstrumentCwd(instrumentCwd) { if (instrumentCwd.includes('=>')) { const instrumentCwdSplit = instrumentCwd.split('=>'); @@ -67,61 +69,28 @@ async function remapCoverage(obj: any) { return obj_1; } -export function validateObject(obj) { - return Object.keys(obj).reduce((acc, item) => { - return { - ...acc, - [item]: { - ...obj[item], - statementMap: obj[item].statementMap ? obj[item].statementMap : {}, - fnMap: obj[item].fnMap ? obj[item].fnMap : {}, - branchMap: obj[item].branchMap ? obj[item].branchMap : {}, - s: obj[item].s ? obj[item].s : {}, - f: obj[item].f ? obj[item].f : {}, - b: obj[item].b ? obj[item].b : {}, - }, - }; - }, {}); +function getJsonSize(jsonObj) { + // 创建一个 TextEncoder 实例 + const encoder = new TextEncoder(); - // 检查对象是否具有所需的属性,如果不存在,则设置为空对象 - return { - ...obj, - statementMap: obj.statementMap ? obj.statementMap : {}, - fnMap: obj.fnMap ? obj.fnMap : {}, - branchMap: obj.branchMap ? obj.branchMap : {}, - s: obj.s ? obj.s : {}, - f: obj.f ? obj.f : {}, - b: obj.b ? obj.b : {}, - }; -} + // 将 JSON 对象转换为字符串 + const jsonString = JSON.stringify(jsonObj); -export function splitJSONIntoQuarters(jsonObject) { - // 计算JSON对象的长度 - const length = Object.keys(jsonObject).length; + // 将 JSON 字符串转换为字节数组 + const encodedJson = encoder.encode(jsonString); - // 计算四分之一的位置 - const quarterPosition = Math.ceil(length / 8); + // 返回字节数组的长度 + return encodedJson.length/1024/1024; +} - // 初始化四个部分的数据 - const quarters = []; - for (let i = 0; i < 8; i++) { - quarters[i] = {}; - } - // 将原始JSON数据按照四分之一均匀分割到四个部分中 - let i = 0; - let currentQuarterIndex = 0; - for (const key in jsonObject) { - if (!quarters[currentQuarterIndex]) { - quarters[currentQuarterIndex] = {}; - } - quarters[currentQuarterIndex][key] = jsonObject[key]; - i++; - if (i % quarterPosition === 0) { - currentQuarterIndex++; - } +export const mergeCoverageMap = (cov1: any, cov2: any) => { + // 超过2M的数据用js合并 + const size = getJsonSize(cov1); + if (size > 2){ + console.log(`size of cov1: ${size}M`); + return mergeCoverageMapOfCanyonData(cov1, cov2) + } else { + return JSON.parse(merge_coverage_json_str(JSON.stringify(cov1), JSON.stringify(cov2))); } - - // 返回四个部分的数据 - return quarters; }