我注意到在使用一些自动更新代码的时候,我已经为几个应用程序编写了代码,根据目标机器的操作系统,我从WebClient对象体验到了非常不同的性能。
我有两台运行更新设置的服务器,我在Linux和Windows上以相同的方式编写了这些工作。
在Linux安装程序中,我的更新程序代码运行速度快,每个排队的WebClient对象之间没有延迟。 事情迅速移动,并在几秒钟内完成更新如预期。 但是,当从基于Windows的服务器进行连接和下载时,相同的确切代码在相同的确切设置中会非常慢。 更新代码和设置完全相同。 唯一的区别是一台主机更新的机器是Linux,另一台机器是Windows。
就差异而言,这是两个系统的设置:
基于Linux的机器:
基于Windows的机器:
正在使用的更新代码按以下方式设置:
由于目前大多数更新不超过2MB,所以一次只能处理1个文件,所以目前不需要同时执行多个文件。
这在我的设置中看起来像这样:
while (Interlocked.CompareExchange(ref this.m_TaskThread, null, null) != null && this.State != LauncherState.Closing) { if (this.m_CancelToken.IsCancellationRequested) break; UpdateFile file; if (this.TaskList.TryTake(out file)) { this.CurrentTask = new DownloadTask(this.m_TaskThread, this.m_CancelToken, file) { File = file.RemoteFile, FileName = file.FileName }; this.CurrentTask.DoTask(); } else { // End the update thread and start the application here.. } }
下载任务对象将如下处理文件下载:
public override void DoTask() { this.Progress = 0; this.ProgressTotal = this.Download.FileSize / 1024.0f; try { if (string.IsNullOrEmpty(this.Download?.RemoteFile)) return; if (System.IO.File.Exists(this.Download.LocalFile)) System.IO.File.Delete(this.Download.LocalFile); } catch (Exception ex) { App.ShowException(ex); return; } var request = (HttpWebRequest)WebRequest.Create(this.Download.RemoteFile); request.AllowAutoRedirect = true; request.Method = "GET"; request.Proxy = null; request.ReadWriteTimeout = 30000; request.Referer = null; request.Timeout = 5000; WebResponse response; try { response = request.GetResponse(); } catch (WebException ex) { response = ex.Response; } var httpResponse = response as HttpWebResponse; if (httpResponse == null || httpResponse.StatusCode != HttpStatusCode.OK) return; var responseStream = httpResponse.GetResponseStream(); if (responseStream == null) return; using (responseStream) { var path = Path.GetDirectoryName(this.Download.LocalFile); if (path != null && !Directory.Exists(path)) Directory.CreateDirectory(path); using (var stream = System.IO.File.Open(this.Download.LocalFile, FileMode.Create, FileAccess.ReadWrite)) { var buffer = new byte[1024 * 2]; var total = 0; int count; while (((count = responseStream.Read(buffer, 0, buffer.Length)) > 0) && !this.CancelToken.IsCancellationRequested) { stream.Write(buffer, 0, count); total += count; this.Progress = total / 1024.0f; } } } httpResponse.Close(); httpResponse.Dispose(); }
当连接到Linux服务器进行所有更新时,一切都是完美无缺的。 但是,Windows在每个Web请求之间延迟了将近2-4秒之后非常慢。 我已经尝试了大量的不同的事情来解决这个问题,并加快速度,但是现在我已经不知所措了。
还有什么我可以做的,以加快速度,并使其工作速度与在Linux上一样快?
编辑 – 只是为了澄清,延迟和减速问题是networking请求之间。 一旦发出请求并开始下载文件,就会像预期的那样下载。 一旦下载完成,下一个请求的结束和开始之间的延迟就是问题所在。