2012-10-15 19 views
7

कुछ कोड जो मैं कभी-कभी काम कर रहा हूं, लंबे समय तक यूएनसी पथ (जैसे \\? \ UNC \ MachineName \ Path) का उल्लेख करने की आवश्यकता है, लेकिन हमने पाया है कि कोई फर्क नहीं पड़ता जहां निर्देशिका स्थित है, यहां तक ​​कि एक ही मशीन पर, स्थानीय पथ की तुलना में यूएनसी पथ के माध्यम से पहुंचते समय यह बहुत धीमी है।स्थानीय पहुंच की ओर इशारा करते हुए यूएनसी पथ स्थानीय पहुंच

उदाहरण के लिए, हमने कुछ बेंचमार्किंग कोड लिखा है जो फ़ाइल में गंदगी की एक स्ट्रिंग लिखता है, फिर बाद में इसे कई बार पढ़ता है।

  • C: \ अस्थायी
  • \\ MachineName \ अस्थायी
  • मैं कोड एक ही मशीन पर चल रहा है के साथ, मेरे देव मशीन पर एक ही साझा निर्देशिका का उपयोग करने के 6 अलग अलग तरीकों के साथ यह परीक्षण कर रहा हूँ ? \\ \ सी? \ अस्थायी
  • \\ \ यूएनसी \ MachineName \ अस्थायी
  • \\ 127.0.0.1 \ अस्थायी
  • \\ \ यूएनसी \ 127.0.0.1 \ अस्थायी

और यहां परिणाम हैं:

Testing: C:\Temp 
Wrote 1000 files to C:\Temp in 861.0647 ms 
Read 1000 files from C:\Temp in 60.0744 ms 
Testing: \\MachineName\Temp 
Wrote 1000 files to \\MachineName\Temp in 2270.2051 ms 
Read 1000 files from \\MachineName\Temp in 1655.0815 ms 
Testing: \\?\C:\Temp 
Wrote 1000 files to \\?\C:\Temp in 916.0596 ms 
Read 1000 files from \\?\C:\Temp in 60.0517 ms 
Testing: \\?\UNC\MachineName\Temp 
Wrote 1000 files to \\?\UNC\MachineName\Temp in 2499.3235 ms 
Read 1000 files from \\?\UNC\MachineName\Temp in 1684.2291 ms 
Testing: \\127.0.0.1\Temp 
Wrote 1000 files to \\127.0.0.1\Temp in 2516.2847 ms 
Read 1000 files from \\127.0.0.1\Temp in 1721.1925 ms 
Testing: \\?\UNC\127.0.0.1\Temp 
Wrote 1000 files to \\?\UNC\127.0.0.1\Temp in 2499.3211 ms 
Read 1000 files from \\?\UNC\127.0.0.1\Temp in 1678.18 ms 

मैंने आईपी पते को DNS समस्या को रद्द करने का प्रयास किया। क्या यह प्रत्येक फ़ाइल पहुंच पर प्रमाण-पत्र या अनुमतियों की जांच कर सकता है? यदि हां, तो क्या इसे कैश करने का कोई तरीका है? क्या यह मानता है कि यह एक यूएनसी पथ है कि इसे सीधे डिस्क तक पहुंचने के बजाय टीसीपी/आईपी पर सबकुछ करना चाहिए? क्या कोड के साथ कुछ गलत है जिसे हम पढ़ रहे हैं/लिखने के लिए उपयोग कर रहे हैं? मैंने बेंचमार्किंग के लिए प्रासंगिक भागों को बाहर निकाल दिया है, नीचे देखा गया:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Runtime.InteropServices; 
using System.Text; 
using Microsoft.Win32.SafeHandles; 
using Util.FileSystem; 

namespace UNCWriteTest { 
    internal class Program { 
     [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     public static extern bool DeleteFile(string path); // File.Delete doesn't handle \\?\UNC\ paths 

     private const int N = 1000; 

     private const string TextToSerialize = 
      "asd;lgviajsmfopajwf0923p84jtmpq93worjgfq0394jktp9orgjawefuogahejngfmliqwegfnailsjdhfmasodfhnasjldgifvsdkuhjsmdofasldhjfasolfgiasngouahfmp9284jfqp92384fhjwp90c8jkp04jk34pofj4eo9aWIUEgjaoswdfg8jmp409c8jmwoeifulhnjq34lotgfhnq34g"; 

     private static readonly byte[] _Buffer = Encoding.UTF8.GetBytes(TextToSerialize); 

     public static string WriteFile(string basedir) { 
      string fileName = Path.Combine(basedir, string.Format("{0}.tmp", Guid.NewGuid())); 

      try { 
       IntPtr writeHandle = NativeFileHandler.CreateFile(
        fileName, 
        NativeFileHandler.EFileAccess.GenericWrite, 
        NativeFileHandler.EFileShare.None, 
        IntPtr.Zero, 
        NativeFileHandler.ECreationDisposition.New, 
        NativeFileHandler.EFileAttributes.Normal, 
        IntPtr.Zero); 

       // if file was locked 
       int fileError = Marshal.GetLastWin32Error(); 
       if ((fileError == 32 /* ERROR_SHARING_VIOLATION */) || (fileError == 80 /* ERROR_FILE_EXISTS */)) { 
        throw new Exception("oopsy"); 
       } 

       using (var h = new SafeFileHandle(writeHandle, true)) { 
        using (var fs = new FileStream(h, FileAccess.Write, NativeFileHandler.DiskPageSize)) { 
         fs.Write(_Buffer, 0, _Buffer.Length); 
        } 
       } 
      } 
      catch (IOException) { 
       throw; 
      } 
      catch (Exception ex) { 
       throw new InvalidOperationException(" code " + Marshal.GetLastWin32Error(), ex); 
      } 

      return fileName; 
     } 

     public static void ReadFile(string fileName) { 
      var fileHandle = 
       new SafeFileHandle(
        NativeFileHandler.CreateFile(fileName, NativeFileHandler.EFileAccess.GenericRead, NativeFileHandler.EFileShare.Read, IntPtr.Zero, 
               NativeFileHandler.ECreationDisposition.OpenExisting, NativeFileHandler.EFileAttributes.Normal, IntPtr.Zero), true); 

      using (fileHandle) { 
       //check the handle here to get a bit cleaner exception semantics 
       if (fileHandle.IsInvalid) { 
        //ms-help://MS.MSSDK.1033/MS.WinSDK.1033/debug/base/system_error_codes__0-499_.htm 
        int errorCode = Marshal.GetLastWin32Error(); 
        //now that we've taken more than our allotted share of time, throw the exception 
        throw new IOException(string.Format("file read failed on {0} to {1} with error code {1}", fileName, errorCode)); 
       } 

       //we have a valid handle and can actually read a stream, exceptions from serialization bubble out 
       using (var fs = new FileStream(fileHandle, FileAccess.Read, 1*NativeFileHandler.DiskPageSize)) { 
        //if serialization fails, we'll just let the normal serialization exception flow out 
        var foo = new byte[256]; 
        fs.Read(foo, 0, 256); 
       } 
      } 
     } 

     public static string[] TestWrites(string baseDir) { 
      try { 
       var fileNames = new List<string>(); 
       DateTime start = DateTime.UtcNow; 
       for (int i = 0; i < N; i++) { 
        fileNames.Add(WriteFile(baseDir)); 
       } 
       DateTime end = DateTime.UtcNow; 

       Console.Out.WriteLine("Wrote {0} files to {1} in {2} ms", N, baseDir, end.Subtract(start).TotalMilliseconds); 
       return fileNames.ToArray(); 
      } 
      catch (Exception e) { 
       Console.Out.WriteLine("Failed to write for " + baseDir + " Exception: " + e.Message); 
       return new string[] {}; 
      } 
     } 

     public static void TestReads(string baseDir, string[] fileNames) { 
      try { 
       DateTime start = DateTime.UtcNow; 

       for (int i = 0; i < N; i++) { 
        ReadFile(fileNames[i%fileNames.Length]); 
       } 
       DateTime end = DateTime.UtcNow; 

       Console.Out.WriteLine("Read {0} files from {1} in {2} ms", N, baseDir, end.Subtract(start).TotalMilliseconds); 
      } 
      catch (Exception e) { 
       Console.Out.WriteLine("Failed to read for " + baseDir + " Exception: " + e.Message); 
      } 
     } 

     private static void Main(string[] args) { 
      foreach (string baseDir in args) { 
       Console.Out.WriteLine("Testing: {0}", baseDir); 

       string[] fileNames = TestWrites(baseDir); 

       TestReads(baseDir, fileNames); 

       foreach (string fileName in fileNames) { 
        DeleteFile(fileName); 
       } 
      } 
     } 
    } 
} 

उत्तर

6

यह मुझे आश्चर्य नहीं करता है। आप डेटा की काफी छोटी मात्रा लिख ​​रहे/पढ़ रहे हैं, इसलिए फ़ाइल सिस्टम कैश शायद भौतिक डिस्क I/O के प्रभाव को कम कर रहा है; मूल रूप से, बाधा सीपीयू होने जा रही है। मुझे यकीन नहीं है कि यातायात टीसीपी/आईपी स्टैक के माध्यम से जा रहा है या नहीं, लेकिन कम से कम एसएमबी प्रोटोकॉल शामिल है। एक बात के लिए इसका मतलब है कि अनुरोध एसएमबी क्लाइंट प्रक्रिया और एसएमबी सर्वर प्रक्रिया के बीच आगे और आगे पारित किए जा रहे हैं, इसलिए आपको अपने आप सहित तीन अलग-अलग प्रक्रियाओं के बीच संदर्भ स्विचिंग मिल गई है। स्थानीय फ़ाइल सिस्टम पथ का उपयोग करके आप कर्नेल मोड में और पीछे स्विच कर रहे हैं लेकिन कोई अन्य प्रक्रिया शामिल नहीं है। संदर्भ स्विचिंग बहुत कर्नेल मोड से और उसके संक्रमण से धीमी है।

दो अलग-अलग अतिरिक्त ओवरहेड्स, एक प्रति फ़ाइल और प्रति किलो प्रति किलो होने की संभावना है। इस विशेष परीक्षण में प्रति फ़ाइल एसएमबी ओवरहेड प्रभावी होने की संभावना है। चूंकि इसमें शामिल डेटा की मात्रा भी भौतिक डिस्क I/O के प्रभाव को प्रभावित करती है, इसलिए आप पाएंगे कि बहुत सी छोटी फाइलों से निपटने पर यह वास्तव में एक समस्या है।

संबंधित मुद्दे