refactor(性能测试): 性能测试Git下载采用流式下载法

This commit is contained in:
Jianguo-Genius 2024-07-12 16:18:19 +08:00 committed by 刘瑞斌
parent 1e91f20744
commit 6ffac87d3f
2 changed files with 20 additions and 2 deletions

View File

@ -10,7 +10,6 @@ import io.metersphere.metadata.vo.RepositoryRequest;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
@ -47,7 +46,10 @@ public class GitFileRepository implements FileRepository {
@Override @Override
public InputStream getFileAsStream(FileRequest request) throws Exception { public InputStream getFileAsStream(FileRequest request) throws Exception {
return new ByteArrayInputStream(getFile(request)); RemoteFileAttachInfo gitFileInfo = request.getFileAttachInfo();
GitRepositoryUtil repositoryUtils = new GitRepositoryUtil(
gitFileInfo.getRepositoryPath(), gitFileInfo.getUserName(), gitFileInfo.getToken());
return repositoryUtils.getFileAsStream(gitFileInfo.getCommitId(), gitFileInfo.getFilePath());
} }
@Override @Override

View File

@ -23,6 +23,7 @@ import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter; import org.eclipse.jgit.treewalk.filter.PathFilter;
import java.io.InputStream;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -214,4 +215,19 @@ public class GitRepositoryUtil {
return returnList; return returnList;
} }
public InputStream getFileAsStream(String commitId, String filePath) throws Exception {
InMemoryRepository repo = this.getGitRepositoryInMemory(this.repositoryUrl, this.userName, this.token);
ObjectId fileCommitObjectId = repo.resolve(commitId);
RevWalk revWalk = new RevWalk(repo);
RevCommit commit = revWalk.parseCommit(fileCommitObjectId);
RevTree tree = commit.getTree();
TreeWalk treeWalk = new TreeWalk(repo);
treeWalk.addTree(tree);
treeWalk.setRecursive(true);
treeWalk.setFilter(PathFilter.create(filePath));
ObjectId objectId = treeWalk.getObjectId(0);
ObjectLoader loader = repo.open(objectId);
return loader.openStream();
}
} }