Mercurial CLI 在 C# 中很慢?

发布于 2024-10-09 19:26:47 字数 1594 浏览 2 评论 0原文

我正在用 C# 编写一个实用程序,该实用程序将使我的团队使用它的方式更轻松地管理多个 Mercurial 存储库。然而,在我从 hg.exe 获取任何信息之前,似乎总是有大约 300 到 400 毫秒的延迟。我使用下面的代码来运行 hg.exe 和 hgtk.exe (TortoiseHg 的 GUI)。该代码当前包含一个秒表和一些用于计时的变量。在同一会话中多次运行时,延迟大致相同。我也尝试指定 hg.exe 的确切路径,并得到相同的结果。

static string RunCommand(string executable, string path, string arguments)
{
    var psi = new ProcessStartInfo()
    {
        FileName = executable,
        Arguments = arguments,
        WorkingDirectory = path,

        UseShellExecute = false,
        RedirectStandardError = true,
        RedirectStandardInput = true,
        RedirectStandardOutput = true,

        WindowStyle = ProcessWindowStyle.Maximized,
        CreateNoWindow = true
    };

    var sbOut = new StringBuilder();
    var sbErr = new StringBuilder();

    var sw = new Stopwatch();

    sw.Start();

    var process = Process.Start(psi);

    TimeSpan firstRead = TimeSpan.Zero;

    process.OutputDataReceived += (s, e) =>
    {
        if (firstRead == TimeSpan.Zero)
        {
            firstRead = sw.Elapsed;
        }

        sbOut.Append(e.Data);
    };
    process.ErrorDataReceived += (s, e) => sbErr.Append(e.Data);

    process.BeginOutputReadLine();
    process.BeginErrorReadLine();

    var eventsStarted = sw.Elapsed;

    process.WaitForExit();

    var processExited = sw.Elapsed;

    sw.Reset();

    if (process.ExitCode != 0 || sbErr.Length > 0)
    {
        Error.Mercurial(process.ExitCode, sbOut.ToString(), sbErr.ToString());
    }

    return sbOut.ToString();
}

关于如何加快速度有什么想法吗?事实上,除了线程之外,我还必须进行大量缓存,以保持 UI 的敏捷性。

I'm writing a utility in C# that will make managing multiple Mercurial repositories easier for the way my team is using it. However, it seems that there is always about a 300 to 400 millisecond delay before I get anything back from hg.exe. I'm using the code below to run hg.exe and hgtk.exe (TortoiseHg's GUI). The code currently includes a Stopwatch and some variables for timing purposes. The delay is roughly the same on multiple runs within the same session. I have also tried specifying the exact path of hg.exe, and got the same result.

static string RunCommand(string executable, string path, string arguments)
{
    var psi = new ProcessStartInfo()
    {
        FileName = executable,
        Arguments = arguments,
        WorkingDirectory = path,

        UseShellExecute = false,
        RedirectStandardError = true,
        RedirectStandardInput = true,
        RedirectStandardOutput = true,

        WindowStyle = ProcessWindowStyle.Maximized,
        CreateNoWindow = true
    };

    var sbOut = new StringBuilder();
    var sbErr = new StringBuilder();

    var sw = new Stopwatch();

    sw.Start();

    var process = Process.Start(psi);

    TimeSpan firstRead = TimeSpan.Zero;

    process.OutputDataReceived += (s, e) =>
    {
        if (firstRead == TimeSpan.Zero)
        {
            firstRead = sw.Elapsed;
        }

        sbOut.Append(e.Data);
    };
    process.ErrorDataReceived += (s, e) => sbErr.Append(e.Data);

    process.BeginOutputReadLine();
    process.BeginErrorReadLine();

    var eventsStarted = sw.Elapsed;

    process.WaitForExit();

    var processExited = sw.Elapsed;

    sw.Reset();

    if (process.ExitCode != 0 || sbErr.Length > 0)
    {
        Error.Mercurial(process.ExitCode, sbOut.ToString(), sbErr.ToString());
    }

    return sbOut.ToString();
}

Any ideas on how I can speed things up? As it is, I'm going to have to do a lot of caching in addition to threading to keep the UI snappy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_失温 2024-10-16 19:26:47

好吧,事情就是这样。

Mercurial 命令行程序包含开销,仅此而已。

这个命令:

hg --quiet version

它根本不查看任何存储库,在我的机器上平均需要 195 毫秒才能执行。当您开始涉及存储库和变更集时,您将无法将其最小化到接近零。

为什么有 26 个存储库?为什么你需要知道他们在哪个分支?您是否同时使用命名分支和多个存储库?

Well, here's the deal.

The mercurial command line program contains overhead, that's about it.

This command:

hg --quiet version

Which doesn't look at any repository at all, takes 195ms on average on my machine to execute. You're not going to be able to minimize this a lot closer to zero when you start involving repositories and changesets.

Why do you have 26 repositories? Why do you need to know which branch they're on? Are you using named branches and multiple repositores at the same time?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文