为什么模拟并行功能在第一次运行时执行速度慢但随后加速?

问题描述 投票:0回答:1

所以我有一些代码可以将文件复制到网络上的5台远程PC上。我有一个类,它模拟了需要登录的方法

''' <summary>
''' Returns the names of the files including their path that match the regex pattern found in this directory. 
''' </summary>
''' <param name="searchOption">
''' The parameter option SearchOption.AllDirectories indicates that the search should include the current directory and any subdirectories
''' The parameter option SearchOption.TopDirectoryOnly indicates that the search should only include the current directory. 
''' </param> 
Public Function GetFileContent(filePath As String, searchPattern As String, Optional searchOption As SearchOption = Nothing) As String() Implements IEPMADirectoryAccess.GetFileContent
    If searchOption = Nothing Then
        searchOption = SearchOption.TopDirectoryOnly
    End If

    LogonUser(UserName, Domain, Password, LogonType.LOGON32_LOGON_NEW_CREDENTIALS, 0, UserToken)
    Using WindowsIdentity.Impersonate(UserToken)
        Return Directory.GetFiles(filePath, searchPattern, searchOption)
    End Using
End Function
''' <summary>
''' Get the name of the Ward from the directory of the current file path 
''' </summary>
Public Function GetWardName(filePath As String) As String Implements IEPMADirectoryAccess.GetWardName
    LogonUser(UserName, Domain, Password, LogonType.LOGON32_LOGON_NEW_CREDENTIALS, 0, UserToken)
    Using WindowsIdentity.Impersonate(UserToken)
        Return New DirectoryInfo(Path.GetDirectoryName(filePath)).Name
    End Using
End Function
''' <summary>
''' Creates a directory for the given path 
''' </summary>
Public Sub CreateDirectory(directoryPath As String) Implements IEPMADirectoryAccess.CreateDirectory
    LogonUser(UserName, Domain, Password, LogonType.LOGON32_LOGON_NEW_CREDENTIALS, 0, UserToken)
    Using WindowsIdentity.Impersonate(UserToken)
        Directory.CreateDirectory(directoryPath)
    End Using
End Sub
''' <summary>
''' Deletes a file from the specified path
''' </summary>
Public Sub DeleteFile(filePath As String) Implements IEPMADirectoryAccess.DeleteFile
    LogonUser(UserName, Domain, Password, LogonType.LOGON32_LOGON_NEW_CREDENTIALS, 0, UserToken)
    Using WindowsIdentity.Impersonate(UserToken)
        File.Delete(filePath)
    End Using
End Sub
''' <summary>
''' Copies a file from the source path to the destination path 
''' </summary>
Public Sub CopyFile(sourceFilePath As String, destFilePath As String, overwrite As Boolean) Implements IEPMADirectoryAccess.CopyFile
    LogonUser(UserName, Domain, Password, LogonType.LOGON32_LOGON_NEW_CREDENTIALS, 0, UserToken)
    Using WindowsIdentity.Impersonate(UserToken)
        File.Copy(sourceFilePath, destFilePath, overwrite)
    End Using
End Sub

并行循环中的部分代码如下所示:

                                                        'If PC is switched on 
                                                         If IsOnline(device.PCName) Then

                                                             'For each PC of the ward, get the corresponding folder path
                                                             targetPath = "\\" + device.PCName + "\c$Report P\"

                                                             'Requires access to the network share 
                                                             If Not Directory.Exists(targetPath) Then
                                                                 DirectoryAccess.CreateDirectory(targetPath)
                                                             End If

                                                             'Requires access to the network share 
                                                             For Each fileName As String In DirectoryAccess.GetFileContent(targetPath, "*.pdf")
                                                                 'Purge (Delete) previous versions of this file in the destination folder  
                                                                 Dim fileConst As String

                                                                 fileConst = arr(1)
                                                                 If fileName.Contains(fileConst) Then
                                                                     'Requires access to the network share 
                                                                     DirectoryAccess.DeleteFile(fileName)
                                                                 End If
                                                             Next

                                                             DirectoryAccess.CopyFile(f, Path.Combine(targetPath, Path.GetFileName(f), True)


                                                         End If

在主模块中,我计算在使用普通for循环时复制文件的差异与并行for循环相比但在for循环中使用相同的代码内容:

enter image description here

最初,并行复制是18秒,但第二次运行则变为2.82秒。

enter image description here

第三次运行是1.81秒。

我已检查用于模拟的使用块在退出使用块后已正确处理并恢复为原始凭据。我不确定为什么它在第一次运行时速度慢但随后加速?可能是最初这四台PC处于非活动状态,因此复制过程会激活它们但需要更长时间?

我有一个IsOnline功能,可以检查PC是否已开机,如果没有,我只是跳过它们以确保它不会减慢过程。

enter image description here

file parallel-processing impersonation copying
1个回答
0
投票

事实证明它很慢,因为任何时候都在不同数量的电脑上测量时间。有些PC在测量过程中进入睡眠状态,因此影响了处理时间

© www.soinside.com 2019 - 2024. All rights reserved.