Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[engine] Worker를 활용한 깃 로그 받아오기 #713

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/analysis-engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ export class AnalysisEngine {
if (this.isDebugMode) console.log("baseBranchName: ", this.baseBranchName);

const commitRaws = getCommitRaws(this.gitLog);
if (this.isDebugMode) console.log("commitRaws: ", commitRaws);
if (this.isDebugMode){
console.log("commitRaws: ", commitRaws);
}

const commitDict = buildCommitDict(commitRaws);
if (this.isDebugMode) console.log("commitDict: ", commitDict);
Expand Down
3 changes: 2 additions & 1 deletion packages/vscode/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
}
],
"simple-import-sort/exports": "error",
"no-duplicate-imports": "error"
"no-duplicate-imports": "error",
"import/no-commonjs": "off"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추가하신 목적이 궁금합니다~.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분은 사실 초기에 Worker가 빌드되지 않은(dist에 있지 않은) 외부 파일을 실행할 때 발생한 에러를 고치기 위해 삽입한 코드입니다.
import 대신 require를 사용해야해서 위 코드를 삽입했는데 기억은 잘 나지 않지만 require를 사용해도 문제가 해결되지 않았습니다.
해당 문제는 외부 파일(git.worker.ts)를 따로 번들링하여 해결했습니다.

뭔가 말이 많았는데 해당 코드는 오류 해결을 위해 시도하다가 생긴 코드로 지금은 필요없습니다!
삭제하도록 하겠습니다!

},
"overrides": [
{
Expand Down
11 changes: 10 additions & 1 deletion packages/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import { deleteGithubToken, getGithubToken, setGithubToken } from "./setting-repository";
import { mapClusterNodesFrom } from "./utils/csm.mapper";
import {
fetchGitLogInParallel,
findGit,
getBranches,
getCurrentBranchName,
getDefaultBranchName,
getGitConfig,
getGitLog,

Check warning on line 16 in packages/vscode/src/extension.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

'getGitLog' is defined but never used
getRepo,
} from "./utils/git.util";
import WebviewLoader from "./webview-loader";
Expand Down Expand Up @@ -74,7 +75,15 @@

const initialBaseBranchName = await fetchCurrentBranch();
const fetchClusterNodes = async (baseBranchName = initialBaseBranchName) => {
const gitLog = await getGitLog(gitPath, currentWorkspacePath);
console.time('Multi log')
const gitLog = await fetchGitLogInParallel(gitPath, currentWorkspacePath);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

혹시 fetchClusterNodes 내에서 await이 걸려있는 getGitConfigengine.analyzeGit는 getGitLog보다 시간이 덜 걸릴까요?? 추후에 뭔가 더 많은 곳에서 적용해봐도 좋을 것 같다는 생각이 들어서요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네, fetchClusterNodes에서 가장 많은 시간이 소요되는 부분은 getGitLog입니다.
정확히 수치화 하지는 않았지만 getGitLog가 거의 95% 이상의 시간이 소요되고 나머지 getGitConfig, engine.analyzeGit 등에 대한 처리는 커밋이 만개 정도 되지 않는 이상 1초 이내로 소요되었습니다.

console.timeEnd('Multi log')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추후(feature 개발이 끝난 이후) 에는 이 부분도 삭제하면 좋겠습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

마지막에 브랜치 병합할 때 삭제하도록하겠습니다


// console.time('Single log')
// const testGitLog = await getGitLog(gitPath, currentWorkspacePath);
// console.timeEnd('Single log')
Comment on lines +82 to +84
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요거 지워도 될 것 같아요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위 코드는 기존 방식으로 깃 로그를 받아오는 코드입니다. 추후에 사용할 가능성이 있어, 주석으로 처리해두었습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다시 생각해보니 굳이 살려둘 필요도 없겠네요, 지우도록 하겠습니다~



const gitConfig = await getGitConfig(gitPath, currentWorkspacePath, "origin");
const { owner, repo: initialRepo } = getRepo(gitConfig);
webLoader.setGlobalOwnerAndRepo(owner, initialRepo);
Expand Down
67 changes: 67 additions & 0 deletions packages/vscode/src/utils/git.util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as cp from "child_process";
import * as fs from "fs";
import os from 'os';
import * as path from "path";
import { Worker } from 'worker_threads';



export interface GitExecutable {
readonly path: string;
Expand Down Expand Up @@ -111,7 +115,7 @@
if (await isExecutable(file)) {
try {
return await getGitExecutable(file);
} catch (_) {}

Check warning on line 118 in packages/vscode/src/utils/git.util.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Empty block statement
}
}
return Promise.reject<GitExecutable>();
Expand Down Expand Up @@ -147,13 +151,14 @@
for (let i = 0; i < paths.length; i++) {
try {
return await getGitExecutable(paths[i]);
} catch (_) {}

Check warning on line 154 in packages/vscode/src/utils/git.util.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Empty block statement
}
throw new Error("None of the provided paths are a Git executable");
}

export async function getGitLog(gitPath: string, currentWorkspacePath: string): Promise<string> {
return new Promise((resolve, reject) => {

const args = [
"--no-pager",
"log",
Expand Down Expand Up @@ -182,6 +187,68 @@
});
}

export async function getLogCount(gitPath: string, currentWorkspacePath: string): Promise<number> {
return new Promise((resolve, reject) => {
const args = [
"rev-list",
"--count",
"--all",
];

resolveSpawnOutput(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os module을 사용하는 경우, multi OS에서의 테스트도 필요합니다.
가끔 os 별로 다른 결과물이 나오는 경우가 있기도 했네요

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

공부해보고 테스트 케이스 작성해보겠습니다!

cp.spawn(gitPath, args, {
cwd: currentWorkspacePath,
env: Object.assign({}, process.env),
})
).then(([status, stdout, stderr]) => {
const { code, error } = status;

if (code === 0 && !error) {
const commitCount = parseInt(stdout.toString().trim(), 10); // Buffer를 문자열로 변환 후 숫자로 변환
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 같은 경우는 변수로 extract 해놓는게 좋겠습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵!

resolve(commitCount); // 숫자를 반환
} else {
reject(stderr);
}
});
});
}


export async function fetchGitLogInParallel(gitPath: string, currentWorkspacePath: string): Promise<string> {
const numCores = os.cpus().length;
const numberOfThreads = Math.max(1, numCores - 1)
const totalCnt = await getLogCount(gitPath, currentWorkspacePath);
console.log("total logs", totalCnt);
const chunkSize = Math.ceil(totalCnt/ numberOfThreads);
const promises: Promise<string>[] = [];

for (let i = 0; i < numberOfThreads; i++) {
const skipCount = i * chunkSize;
const limitCount = chunkSize;

console.log('__dirname:', __dirname);
const worker = new Worker(path.resolve(__dirname, './worker.js'), {
workerData: {
gitPath,
currentWorkspacePath,
skipCount,
limitCount,
},
});

promises.push(
new Promise((resolve, reject) => {
worker.on('message', resolve);
worker.on('error', reject);
})
);
}

return Promise.all(promises).then((logs) => logs.join('\n'));
}



export async function getGitConfig(
gitPath: string,
currentWorkspacePath: string,
Expand Down
41 changes: 41 additions & 0 deletions packages/vscode/src/utils/git.worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as cp from 'child_process';
import { parentPort, workerData } from 'worker_threads';

import { resolveSpawnOutput } from './git.util'

const { gitPath, currentWorkspacePath, skipCount, limitCount } = workerData;

async function getPartialGitLog() {
const args = [
'--no-pager',
'log',
'--all',
'--parents',
'--numstat',
'--date-order',
'--pretty=fuller',
'--decorate',
'-c',
`--skip=${skipCount}`,
`-n ${limitCount}`,
];

resolveSpawnOutput(
cp.spawn(gitPath, args, {
cwd: currentWorkspacePath,
env: Object.assign({}, process.env),
})
).then(([status, stdout, stderr]) => {
const { code, error } = status;

if (code === 0 && !error && parentPort !== null) {
parentPort.postMessage(stdout.toString());
} else {
if (parentPort !== null) parentPort.postMessage(stderr);
}
}).catch(error => {
console.error('Spawn Error:', error);
});
}

getPartialGitLog();
8 changes: 6 additions & 2 deletions packages/vscode/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ const extensionConfig = {
target: "node", // vscode extensions run in a Node.js-context 📖 -> https://webpack.js.org/configuration/node/
mode: "none", // this leaves the source code as close as possible to the original (when packaging we set this to 'production')

entry: "./src/extension.ts", // the entry point of this extension, 📖 -> https://webpack.js.org/configuration/entry-context/
entry: {
extension: "./src/extension.ts", // the entry point of this extension, 📖 -> https://webpack.js.org/configuration/entry-context/
worker: "./src/utils/git.worker.ts"
},
output: {
// the bundle is stored in the 'dist' folder (check package.json), 📖 -> https://webpack.js.org/configuration/output/
path: path.resolve(__dirname, "dist"),
filename: "extension.js",
// filename: "extension.js",
filename: "[name].js",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(질문) 설정을 이렇게 해두면 extension.js, worker.js 이렇게 두벌 나오는 건가용?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

소현님 예리하시군요!! 저도 궁금합니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵, 정확하십니다!
생성된 worker.js는 추후에 Worker가 특정 개수의 로그를 받아오는데 사용됩니다.

const worker = new Worker(path.resolve(__dirname, './worker.js'), {
      workerData: {
        gitPath,
        currentWorkspacePath,
        skipCount,
        limitCount,
      },
    });

libraryTarget: "commonjs2",
},
externals: {
Expand Down
Loading