Skip to content

Commit

Permalink
feat: use rust merge coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Zhang (张涛) committed Jul 1, 2024
1 parent 1ed7343 commit e1424c5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 54 deletions.
2 changes: 1 addition & 1 deletion packages/canyon-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
71 changes: 20 additions & 51 deletions packages/canyon-backend/src/utils/coverage.ts
Original file line number Diff line number Diff line change
@@ -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('=>');
Expand Down Expand Up @@ -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;
}

0 comments on commit e1424c5

Please sign in to comment.