Parcourir la source

性能优化 - 转档部分支持 Block 替换信号方案;

zhudongyong il y a 2 ans
Parent
commit
200ad96577

+ 0 - 5
KdanAutoTest/KdanAuto/Class/AutoTestCase/AutoTestProperty.plist

@@ -172,11 +172,6 @@
 			<string>AutoTest</string>
 			<key>Name</key>
 			<string>快照对比(新库)</string>
-			<key>Params</key>
-			<dict>
-				<key>useOldLib</key>
-				<true/>
-			</dict>
 		</dict>
 	</array>
 	<key>HTML</key>

+ 49 - 31
KdanAutoTest/KdanAuto/Class/Tools/FileConveter/FileConverter.swift

@@ -34,22 +34,28 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
     var accessSemaphore:DispatchSemaphore? = DispatchSemaphore.init(value: 1)
     var didSuccess:Int = 1
     
+    var complention = { (status:Int) in
+        
+    }
+    
     static var instance = FileConverter()
     class func shared() -> FileConverter {
         return instance
     }
     
-    func converter(_ inSrcPath: String, inDesPath: String) -> Int {
-        return converter(inSrcPath, inDesPath: inDesPath, params: nil)
+    func converter(_ inSrcPath: String, inDesPath: String, complention:@escaping (_ status:Int) -> ()) {
+        return converter(inSrcPath, inDesPath: inDesPath, params: nil, complention: complention)
     }
     
-    func converter(_ inSrcPath: String, inDesPath: String, params:NSDictionary?) -> Int {
+    func converter(_ inSrcPath: String, inDesPath: String, params:NSDictionary?, complention:@escaping (_ status:Int) -> ()) {
+        self.complention = complention;
+        
         if !FileManager.default.fileExists(atPath: inSrcPath) {
-            return -1
+            self.complention(-1)
+            
+            return
         }
         
-        accessSemaphore?.wait()
-        
         self.srcPath = inSrcPath
         self.desPath = inDesPath
         
@@ -68,18 +74,18 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
                 self.srcPath = symbolicLink!
             }
         }
-         
+        
         if FileManager.default.fileExists(atPath: self.desPath) {
             try! FileManager.default.removeItem(atPath: self.desPath)
         }
         
         self.pathExtension = NSString(string: self.desPath).pathExtension
-//        let outputPath = NSString(string: self.desPath).deletingPathExtension
-//        let output = self.desPath
+        //        let outputPath = NSString(string: self.desPath).deletingPathExtension
+        //        let output = self.desPath
         let password = params?.value(forKey: "password")
         let tParams = params ?? [:]
         let useOldLibValue = params?.value(forKey: "useOldLib")
-    
+        
         var useOldLib = false;
         if (nil != useOldLibValue) {
             if ((useOldLibValue as? String) != nil) {
@@ -88,7 +94,7 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
                 useOldLib = (useOldLibValue as? NSNumber)!.boolValue || (useOldLibValue as? NSNumber)!.intValue == 1
             }
         }
-    
+        
         NSLog("%@ - %@", useOldLib ? "老库" : "新库", params ?? "")
         
         self.convertQueue.async {
@@ -106,22 +112,22 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
                 self.pages.append(i)
             }
             
-            if  !useOldLib && NSArray(array: ["jpg", "JPG", "png", "PNG"]).contains(self.pathExtension) {
+            if !useOldLib && NSArray(array: ["jpg", "JPG", "png", "PNG"]).contains(self.pathExtension) {
                 autoreleasepool {
-                        let cachePath = NSString(string: self.desPath).deletingPathExtension+".zip"
-                        self.pdfConverter = CPDFConverterImg.init(url: url, password: nil)
-                        if (nil == self.pdfConverter && nil != password) {
-                            self.pdfConverter = CPDFConverterImg.init(url: url, password: password as? String)
-                        }
-                        self.pdfConverter?.delegate = self
-                        self.options = CPDFConvertImgOptions()
-                        if (NSArray(array: ["jpg", "JPG"]).contains(self.pathExtension)) {
-                            (self.options as! CPDFConvertImgOptions).type = .JPEG
-                        }else {
-                            (self.options as! CPDFConvertImgOptions).type = .PNG
-                        }
-                        self.pdfConverter?.convert(toFilePath: cachePath, pageIndexs: self.pages, options: self.options)
+                    let cachePath = NSString(string: self.desPath).deletingPathExtension+".zip"
+                    self.pdfConverter = CPDFConverterImg.init(url: url, password: nil)
+                    if (nil == self.pdfConverter && nil != password) {
+                        self.pdfConverter = CPDFConverterImg.init(url: url, password: password as? String)
                     }
+                    self.pdfConverter?.delegate = self
+                    self.options = CPDFConvertImgOptions()
+                    if (NSArray(array: ["jpg", "JPG"]).contains(self.pathExtension)) {
+                        (self.options as! CPDFConvertImgOptions).type = .JPEG
+                    }else {
+                        (self.options as! CPDFConvertImgOptions).type = .PNG
+                    }
+                    self.pdfConverter?.convert(toFilePath: cachePath, pageIndexs: self.pages, options: self.options)
+                }
             }else if !useOldLib && NSArray(array: ["ppt", "PPT", "PPTX", "pptx"]).contains(self.pathExtension) {
                 autoreleasepool {
                     self.pdfConverter = CPDFConverterPPT.init(url: url, password: nil)
@@ -230,6 +236,22 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
                 }
             }
         }
+    }
+    
+    func converter(_ inSrcPath: String, inDesPath: String) -> Int {
+        return converter(inSrcPath, inDesPath: inDesPath, params: nil)
+    }
+    
+    func converter(_ inSrcPath: String, inDesPath: String, params:NSDictionary?) -> Int {
+        if !FileManager.default.fileExists(atPath: inSrcPath) {
+            return -1
+        }
+        
+        accessSemaphore?.wait()
+        
+        converter(inSrcPath, inDesPath: inDesPath, params: params) { status in
+            self.semaphore?.signal()
+        };
         
         semaphore?.wait()
         accessSemaphore?.signal()
@@ -268,7 +290,7 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
             self.pdfConverter?.delegate = nil
             self.pdfConverter = nil
             
-            semaphore?.signal()
+            self.complention(didSuccess)
         }
     }
     
@@ -284,11 +306,7 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
             
             sleep(2)
             
-            //        self.fpConverter?.stopConvertsionIfNeed()
-            //        self.fpConverter?.setDelegate(nil)
-            //        self.fpConverter = nil
-            //
-            semaphore?.signal()
+            self.complention(didSuccess)
         }
     }
     

+ 8 - 0
KdanAutoTest/KdanAuto/ViewController.swift

@@ -158,9 +158,11 @@ class ViewController : NSViewController, SettingViewControllerDelegate, AutoTest
             var autotestBlock:(NSMutableArray, Int) -> () = { (objects:NSMutableArray, index:Int) in
                 
             }
+            //创建 Block
             autotestBlock = { (objects:NSMutableArray, index:Int) in
                 NSLog("Auto Test \(index)")
                 if (index >= objects.count) {
+                    // 所有自动化测试对象均执行完成
                     do {
                         let rtfData = try report.data(from: .init(location: 0, length: report.length),
                                                       documentAttributes: [.documentType: NSAttributedString.DocumentType.rtf])
@@ -186,14 +188,18 @@ class ViewController : NSViewController, SettingViewControllerDelegate, AutoTest
                     return
                 }
                 
+                // 执行当前索引自动化测试对象
                 let testobject = objects.object(at: index) as! AutoTest
                 testobject.setStatus(.Process)
                 
+                // 异步执行自动化测试
                 testobject.autoTest {(object, inReport) in
                     if inReport != nil {
+                        // 如果有自动化测试报告,则拼接在后面
                         report.append(inReport!)
                     }
                     
+                    // 更新当前自动化测试对象状态及相关 UI 状态
                     if (object.needCompareTest()) {
                         object.setStatus(.Finished)
                     }else {
@@ -207,11 +213,13 @@ class ViewController : NSViewController, SettingViewControllerDelegate, AutoTest
                         self.advanceView.setAutoTestObj(self.advanceView.getAutoTestObj());
                     }
                     
+                    // 触发下一个自动化测试对象进入自动化测试
                     autotestBlock(objects, index+1)
                     
                     return
                 }
             }
+            // 异步第0号位置自动化测试
             autotestBlock(objects, 0)
         }
     }