浏览代码

优化 - 子线程自动释放池添加,内存及时清理

zhudongyong 1 年之前
父节点
当前提交
3f5a724076

+ 118 - 91
KdanAutoTest/KdanAuto/Class/AutoTestCase/AutoTest.swift

@@ -169,92 +169,94 @@ class AutoTest : NSObject, AutoTestProtocal {
         if hasOriginFile() {
             //目录中有需要执行对照的文件
             DispatchQueue.global().async {
-                self._status = .Process
-                self.reportString = NSMutableAttributedString.init(string: "\n【\(String(self.fileType())) - \(self.name())】快照比对开始!\n",
-                                                              attributes:[.foregroundColor : NSColor.blue])
-                let files = DataModel.shared.originFilesFor(self.fileType(), type: self.type()) as [String]
-                self.testFiles = NSArray(array: files);
-                
-                let checkDirectory = self.checkFileDirectory()
-                let originDirectory = self.originFileDirectory()
-                let resultDirectory = self.resultFileDirectory()
-                
-                if (files.count > 0) {
-                    try? FileManager.default.createDirectory(atPath: checkDirectory, withIntermediateDirectories: true);
-                    try? FileManager.default.createDirectory(atPath: resultDirectory, withIntermediateDirectories: true);
-                }
-                
-                var tDegree = Double(0);
-                var tCount = Int(0)
-                var fileIndex = 0
-                var compareIndex = 0;
-                var convertFinished = false;
-                
-                // 用 Block 递归调用形式,确保转档单任务执行
-                var convertFileBlock = { (files:[String]) in }
-                convertFileBlock = { (files:[String]) in
-                    if (fileIndex >= files.count) {
-                        convertFinished = true;
-                        return
+                autoreleasepool {
+                    self._status = .Process
+                    self.reportString = NSMutableAttributedString.init(string: "\n【\(String(self.fileType())) - \(self.name())】快照比对开始!\n",
+                                                                       attributes:[.foregroundColor : NSColor.blue])
+                    let files = DataModel.shared.originFilesFor(self.fileType(), type: self.type()) as [String]
+                    self.testFiles = NSArray(array: files);
+                    
+                    let checkDirectory = self.checkFileDirectory()
+                    let originDirectory = self.originFileDirectory()
+                    let resultDirectory = self.resultFileDirectory()
+                    
+                    if (files.count > 0) {
+                        try? FileManager.default.createDirectory(atPath: checkDirectory, withIntermediateDirectories: true);
+                        try? FileManager.default.createDirectory(atPath: resultDirectory, withIntermediateDirectories: true);
                     }
                     
-                    //
-                    let fileName = files[fileIndex];
-                    autoreleasepool {
-                        let checkDirectory = self.checkFileDirectory()
-                        let originDirectory = self.originFileDirectory()
-                        let resultDirectory = self.resultFileDirectory()
-                        
-                        let fName = NSString(string: fileName).deletingPathExtension
-                        let originPath = NSString(string: originDirectory).appendingPathComponent(fName+".pdf")
-                        let resultPath = NSString(string: resultDirectory).appendingPathComponent(fName+"."+self.extention())
-                        //                let checkPath = NSString(string: checkDirectory).appendingPathComponent(fName+"."+_extention)
-                        
-                        
-//                        self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】开始转换文件 \"\(fName)\"\n",
-//                                                                            attributes:[.foregroundColor : NSColor.blue]))
-                        // ...
-                        // 执行异步转换过程
-                        let index = self.testFiles.index(of: fileName);
-                        if (index != NSNotFound) {
-                            self.convertProgress = Double(index) / Double(self.testFiles.count)
+                    var tDegree = Double(0);
+                    var tCount = Int(0)
+                    var fileIndex = 0
+                    var compareIndex = 0;
+                    var convertFinished = false;
+                    
+                    // 用 Block 递归调用形式,确保转档单任务执行
+                    var convertFileBlock = { (files:[String]) in }
+                    convertFileBlock = { (files:[String]) in
+                        if (fileIndex >= files.count) {
+                            convertFinished = true;
+                            return
                         }
-                        self.convertFiles.add(fileName);
-                        self.testlog("开始转换:"+fileName, (self.compareProgress + self.convertProgress)/2.0)
-                        self.process(originPath, resultPath: resultPath) { status in
-                            DispatchQueue.global().async {
-                                Thread.sleep(forTimeInterval: 0.3)
-                                fileIndex += 1;
-                                convertFileBlock(files)
-                                
-                                self.compareResult(fileName, resultPath: resultPath, status: status, needCompare: needCompare) { degree in
-                                    autoreleasepool {
-                                        if (degree >= 0.0) {
-                                            tDegree += degree;
-                                            tCount += 1;
-                                        }
-                                        
-                                        compareIndex += 1;
-                                        if (convertFinished && compareIndex >= files.count) {
-                                            //所有任务已经执行完成
-                                            TestDegreeManager.shared().set(((tCount != 0) ? tDegree/Double(tCount) : 0.0),
-                                                                           fileType: self.fileType(),
-                                                                           type: self.type())
+                        
+                        //
+                        let fileName = files[fileIndex];
+                        autoreleasepool {
+                            let checkDirectory = self.checkFileDirectory()
+                            let originDirectory = self.originFileDirectory()
+                            let resultDirectory = self.resultFileDirectory()
+                            
+                            let fName = NSString(string: fileName).deletingPathExtension
+                            let originPath = NSString(string: originDirectory).appendingPathComponent(fName+".pdf")
+                            let resultPath = NSString(string: resultDirectory).appendingPathComponent(fName+"."+self.extention())
+                            //                let checkPath = NSString(string: checkDirectory).appendingPathComponent(fName+"."+_extention)
+                            
+                            
+                            //                        self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】开始转换文件 \"\(fName)\"\n",
+                            //                                                                            attributes:[.foregroundColor : NSColor.blue]))
+                            // ...
+                            // 执行异步转换过程
+                            let index = self.testFiles.index(of: fileName);
+                            if (index != NSNotFound) {
+                                self.convertProgress = Double(index) / Double(self.testFiles.count)
+                            }
+                            self.convertFiles.add(fileName);
+                            self.testlog("开始转换:"+fileName, (self.compareProgress + self.convertProgress)/2.0)
+                            self.process(originPath, resultPath: resultPath) { status in
+                                DispatchQueue.global().async {
+                                    Thread.sleep(forTimeInterval: 0.3)
+                                    fileIndex += 1;
+                                    convertFileBlock(files)
+                                    
+                                    self.compareResult(fileName, resultPath: resultPath, status: status, needCompare: needCompare) { degree in
+                                        autoreleasepool {
+                                            if (degree >= 0.0) {
+                                                tDegree += degree;
+                                                tCount += 1;
+                                            }
                                             
-                                            self._status = .Finished
-                                            DispatchQueue.main.async {
-                                                complention(self, self.reportString);
+                                            compareIndex += 1;
+                                            if (convertFinished && compareIndex >= files.count) {
+                                                //所有任务已经执行完成
+                                                TestDegreeManager.shared().set(((tCount != 0) ? tDegree/Double(tCount) : 0.0),
+                                                                               fileType: self.fileType(),
+                                                                               type: self.type())
+                                                
+                                                self._status = .Finished
+                                                DispatchQueue.main.async {
+                                                    complention(self, self.reportString);
+                                                }
                                             }
                                         }
-                                    }
-                                };
+                                    };
+                                }
                             }
+                            
                         }
-                        
                     }
+                    // 启动第0号任务
+                    convertFileBlock(files);
                 }
-                // 启动第0号任务
-                convertFileBlock(files);
             }
             
             
@@ -262,7 +264,9 @@ class AutoTest : NSObject, AutoTestProtocal {
             _status = .Normal
             
             DispatchQueue.main.async {
-                complention(self, nil)
+                autoreleasepool {
+                    complention(self, nil)
+                }
             }
         }
     }
@@ -410,8 +414,10 @@ class AutoTest : NSObject, AutoTestProtocal {
                                                         compareCount += 1
                                                         if (compareCount >= subImages.count) {
                                                             DispatchQueue.global().async {
-                                                                itemIndex += 1
-                                                                processItemBlock(items)
+                                                                autoreleasepool {
+                                                                    itemIndex += 1
+                                                                    processItemBlock(items)
+                                                                }
                                                             }
                                                         }
                                                     })
@@ -421,8 +427,10 @@ class AutoTest : NSObject, AutoTestProtocal {
                                             if (subImages.count == 0) {
                                                 // complention
                                                 DispatchQueue.global().async {
-                                                    itemIndex += 1
-                                                    processItemBlock(items)
+                                                    autoreleasepool {
+                                                        itemIndex += 1
+                                                        processItemBlock(items)
+                                                    }
                                                 }
                                                 return
                                             }
@@ -435,8 +443,10 @@ class AutoTest : NSObject, AutoTestProtocal {
                                                                                fileName: fileName, refFilePath: subFileName+".jpg")
                                                 
                                                 if degree == -1 {
+                                                    objc_sync_enter(self)
                                                     self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subResultPath)\"快照对比失败,生成快照失败或无比对文件\n",
                                                                                                              attributes:[.foregroundColor : NSColor.red]))
+                                                    objc_sync_exit(self)
                                                 }else {
                                                     var color = NSColor.black
                                                     if fabs(degree-100.0) >= 0.01 {
@@ -445,25 +455,33 @@ class AutoTest : NSObject, AutoTestProtocal {
                                                     subDegree += degree
                                                     subCount += 1
                                                     
+                                                    objc_sync_enter(self)
                                                     self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subResultPath)\"快照对比完成,图像相似度 \(degree)%\n",
                                                                                                              attributes:[.foregroundColor : color]))
+                                                    objc_sync_exit(self)
                                                 }
                                                 
                                                 // complention
                                                 DispatchQueue.global().async {
-                                                    itemIndex += 1
-                                                    processItemBlock(items)
+                                                    autoreleasepool {
+                                                        itemIndex += 1
+                                                        processItemBlock(items)
+                                                    }
                                                 }
                                             })
                                         }
                                     }else {
+                                        objc_sync_enter(self)
                                         self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(subResultPath)\"快照生成失败\n",
                                                                                                  attributes:[.foregroundColor : NSColor.red]))
+                                        objc_sync_exit(self)
                                         
                                         // complention
                                         DispatchQueue.global().async {
-                                            itemIndex += 1
-                                            processItemBlock(items)
+                                            autoreleasepool {
+                                                itemIndex += 1
+                                                processItemBlock(items)
+                                            }
                                         }
                                     }
                                 }
@@ -481,14 +499,20 @@ class AutoTest : NSObject, AutoTestProtocal {
             }
         }else {
             if (status == 0) {
+                objc_sync_enter(self)
                 self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"转档失败!\n",
                                                                          attributes:[.foregroundColor : NSColor.red]))
+                objc_sync_exit(self)
             }else if (status == -1 || status == -2) {
+                objc_sync_enter(self)
                 self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"文档无法打开!\n",
                                                                          attributes:[.foregroundColor : NSColor.red]))
+                objc_sync_exit(self)
             }else if (status == -3) {
+                objc_sync_enter(self)
                 self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"转档中 Crash!\n",
                                                                          attributes:[.foregroundColor : NSColor.red]))
+                objc_sync_exit(self)
             }
         }
         
@@ -497,11 +521,14 @@ class AutoTest : NSObject, AutoTestProtocal {
                 // 执行下一个文件转档
                 NSLog("结束识别:\(fileName)");
                 
+                objc_sync_exit(self)
                 let index = self.testFiles.index(of: fileName);
                 if (index != NSNotFound) {
                     self.compareProgress = Double(index) / Double(self.testFiles.count)
                 }
                 self.compareFinishedFiles.add(fileName);
+                objc_sync_exit(self)
+                
                 self.testlog("对比完成:"+fileName, (self.compareProgress + self.convertProgress)/2.0)
                 complention(0)
             }
@@ -542,7 +569,7 @@ class AutoTest : NSObject, AutoTestProtocal {
         //        let originDirectory = self.originFileDirectory()
         let resultDirectory = self.resultFileDirectory()
         
-        for fileName in files {
+        for fileName:String in files  {
             let fName = NSString(string: fileName).deletingPathExtension
             //            let originPath = NSString(string: originDirectory).appendingPathComponent(fName+".pdf")
             let resultPath = NSString(string: resultDirectory).appendingPathComponent(fName+"."+_extention)
@@ -566,7 +593,7 @@ class AutoTest : NSObject, AutoTestProtocal {
                     items.add(fName)
                 }
                 
-                for item in items {
+                for item in NSArray(array: items) {
                     let subFileName = item as! String
                     let rComparePath = NSString(string: resultDirectory).appendingPathComponent(subFileName+".jpg")
                     let cComparePath = NSString(string: checkDirectory).appendingPathComponent(subFileName+".jpg")
@@ -625,7 +652,7 @@ class AutoTest : NSObject, AutoTestProtocal {
                 items.add(fName)
             }
             
-            for item in items {
+            for item in NSArray(array: items) {
                 let subFileName = item as! String
                 let rComparePath = NSString(string: resultDirectory).appendingPathComponent(subFileName+".jpg")
                 let cComparePath = NSString(string: checkDirectory).appendingPathComponent(subFileName+".jpg")
@@ -675,7 +702,7 @@ class AutoTest : NSObject, AutoTestProtocal {
                 items.add(fName)
             }
             
-            for item in items {
+            for item in NSArray(array: items) {
                 let subFileName = item as! String
                 let rComparePath = NSString(string: resultDirectory).appendingPathComponent(subFileName+".jpg")
                 let cComparePath = NSString(string: checkDirectory).appendingPathComponent(subFileName+".jpg")
@@ -724,8 +751,8 @@ class AutoTest : NSObject, AutoTestProtocal {
             // compare screenshoot between result file with check file
             let items = NSMutableArray()
             if (isDirectory.boolValue) {
-                let searchItems = try! FileManager.default.contentsOfDirectory(atPath: resultPath)
-                for item in NSArray(array: searchItems) {
+                let searchItems = try? FileManager.default.contentsOfDirectory(atPath: resultPath)
+                for item in NSArray(array: searchItems ?? []) {
                     let ext = NSString(string: item as! String).pathExtension.lowercased()
                     if (NSArray(array: [_extention]).contains(ext) &&
                         !NSString(string: item as! String).contains("_cover.png")) {

+ 6 - 4
KdanAutoTest/KdanAuto/Class/AutoTestCase/PDFCompareTest.swift

@@ -21,9 +21,11 @@ class PDFCompareTest : AutoTest {
    
     override func process(_ originPath:String, resultPath:String, complention:@escaping (_ status:Int) -> ()) {
         DispatchQueue.global().async {
-            Thread.sleep(forTimeInterval: 0.3);
-            
-            complention(1)
+            autoreleasepool {
+                Thread.sleep(forTimeInterval: 0.3);
+                
+                complention(1)
+            }
         }
     }
     
@@ -72,7 +74,7 @@ class PDFCompareTest : AutoTest {
                 return NSString(string: s1).compare(s2)
             }
             
-            for item in nitems {
+            for item in NSArray(array: nitems) {
                 let subFileName = item as! String
                 let rComparePath = NSString(string: resultDirectory).appendingPathComponent(subFileName+".jpg")
                 let cComparePath = NSString(string: checkDirectory).appendingPathComponent(subFileName+".jpg")

+ 37 - 33
KdanAutoTest/KdanAuto/Class/AutoTestCase/StringAutoTest.swift

@@ -68,7 +68,9 @@ class CharacterAutoTest : AutoTest {
                 
                 self._status = .Finished
                 DispatchQueue.main.async {
-                    complention(self, self.reportString);
+                    autoreleasepool {
+                        complention(self, self.reportString);
+                    }
                 }
                 return
             }
@@ -248,43 +250,45 @@ class CharacterAutoTest : AutoTest {
                         var processSuccess = false
                         let thumbnailQueue = DispatchQueue.global()
                         thumbnailQueue.async {
-                            processSuccess = ProcessThumbnal.process(resultPath, desPath: rComparePath, outputSize: CGSize.init(width: 2048, height: 2048))
-                            
-                            if (processSuccess &&
-                                FileManager.default.fileExists(atPath: rComparePath)) {
-                                
-                                let degree = ImageProcess.compareJPEG(rComparePath, checkPath: cComparePath, processCover: true)
+                            autoreleasepool {
+                                processSuccess = ProcessThumbnal.process(resultPath, desPath: rComparePath, outputSize: CGSize.init(width: 2048, height: 2048))
                                 
-                                if degree == -1 {
-                                    self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"快照对比失败,生成快照失败或无比对文件\n",
-                                                                                             attributes:[.foregroundColor : NSColor.red]))
-                                }else {
-                                    var color = NSColor.black
-                                    if fabs(degree-100.0) >= 0.01 {
-                                        color = NSColor.red
-                                    }
+                                if (processSuccess &&
+                                    FileManager.default.fileExists(atPath: rComparePath)) {
                                     
-                                    TestDegreeManager.shared().set(degree,
-                                                                   fileType: self.fileType(),
-                                                                   type: self.type(),
-                                                                   fileName: fileName);
-                                    TestDegreeManager.shared().set(degree,
-                                                                   fileType: self.fileType(),
-                                                                   type: self.type(),
-                                                                   fileName: fileName,
-                                                                   refFilePath: fName+".jpg");
+                                    let degree = ImageProcess.compareJPEG(rComparePath, checkPath: cComparePath, processCover: true)
                                     
-                                    tDegree += degree
-                                    tCount += 1
-                                    
-                                    self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"快照对比完成,图像相似度 \(degree)%\n",
-                                                                                             attributes:[.foregroundColor : color]))
+                                    if degree == -1 {
+                                        self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"快照对比失败,生成快照失败或无比对文件\n",
+                                                                                                 attributes:[.foregroundColor : NSColor.red]))
+                                    }else {
+                                        var color = NSColor.black
+                                        if fabs(degree-100.0) >= 0.01 {
+                                            color = NSColor.red
+                                        }
+                                        
+                                        TestDegreeManager.shared().set(degree,
+                                                                       fileType: self.fileType(),
+                                                                       type: self.type(),
+                                                                       fileName: fileName);
+                                        TestDegreeManager.shared().set(degree,
+                                                                       fileType: self.fileType(),
+                                                                       type: self.type(),
+                                                                       fileName: fileName,
+                                                                       refFilePath: fName+".jpg");
+                                        
+                                        tDegree += degree
+                                        tCount += 1
+                                        
+                                        self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"快照对比完成,图像相似度 \(degree)%\n",
+                                                                                                 attributes:[.foregroundColor : color]))
+                                    }
+                                }else {
+                                    self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"快照生成失败\n",
+                                                                                             attributes:[.foregroundColor : NSColor.red]))
                                 }
-                            }else {
-                                self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"快照生成失败\n",
-                                                                                         attributes:[.foregroundColor : NSColor.red]))
+                                processThumbSemaphore.signal()
                             }
-                            processThumbSemaphore.signal()
                         }
                         processThumbSemaphore.wait()
                     }

+ 43 - 39
KdanAutoTest/KdanAuto/Class/AutoTestCase/TextColorAutoTest.swift

@@ -89,7 +89,9 @@ class TextColorAutoTest : AutoTest {
                 
                 self._status = .Finished
                 DispatchQueue.main.async {
-                    complention(self, self.reportString);
+                    autoreleasepool {
+                        complention(self, self.reportString);
+                    }
                 }
                 return
             }
@@ -132,7 +134,7 @@ class TextColorAutoTest : AutoTest {
                         let colorItems = colorString.components(separatedBy: ";") as [String]
                         var colors = NSMutableArray() //存储的颜色数组
                         
-                        for ci in colorItems {
+                        for ci in NSArray(array: colorItems) {
                             var colorItem = ci as! NSString
                             if colorItem.length > 0 {
                                 colorItem = colorItem.replacingOccurrences(of: "\\red", with: "(") as NSString
@@ -155,8 +157,8 @@ class TextColorAutoTest : AutoTest {
                         let oItems = finalString.components(separatedBy: ")") // 用 ) 区分字符段落
                         
                         // 识别
-                        for oi in oItems {
-                            var oItem = oi as NSString
+                        for oi in NSArray(array: oItems) {
+                            var oItem = oi as! NSString
                             if oItem.contains("[") {
                                 oItem = oItem.replacingOccurrences(of: "Color ", with: "Color") as NSString
                                 
@@ -179,8 +181,8 @@ class TextColorAutoTest : AutoTest {
                                 // Detect all strings between Spaces and \ and stitch
                                 let strings = oItem.components(separatedBy: " ")
                                 var resultStr = "" as NSString
-                                for str in strings {
-                                    let markStr = str as NSString
+                                for str in NSArray(array: strings) {
+                                    let markStr = str as! NSString
                                     
                                     if (markStr.contains("\\")) {
                                         let endRange = markStr.range(of: "\\")
@@ -233,7 +235,7 @@ class TextColorAutoTest : AutoTest {
                         let semaphore = DispatchSemaphore.init(value: 1)
                         let items = resultAttString!.string.components(separatedBy: "\n")
                         
-                        for item in items {
+                        for item in NSArray(array: items) {
                             let cItem = item as String
                             if NSString(string: cItem).contains("Color Check(") {
                                 
@@ -289,42 +291,44 @@ class TextColorAutoTest : AutoTest {
                         let thumbnailQueue = DispatchQueue.global()
                         thumbnailQueue.async {
                             //                        processSuccess =
-                            if (ProcessThumbnal.process(resultPath, desPath: rComparePath, outputSize: CGSize.init(width: 2048, height: 2048)) &&
-                                FileManager.default.fileExists(atPath: rComparePath)) {
-                                
-                                let degree = ImageProcess.compareJPEG(rComparePath, checkPath: cComparePath, processCover: true)
-                                
-                                if degree == -1 {
-                                    self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"快照对比失败,生成快照失败或无比对文件\n",
-                                                                                             attributes:[.foregroundColor : NSColor.red]))
-                                }else {
-                                    var color = NSColor.black
-                                    if fabs(degree-100.0) >= 0.01 {
-                                        color = NSColor.red
-                                    }
+                            autoreleasepool {
+                                if (ProcessThumbnal.process(resultPath, desPath: rComparePath, outputSize: CGSize.init(width: 2048, height: 2048)) &&
+                                    FileManager.default.fileExists(atPath: rComparePath)) {
                                     
-                                    TestDegreeManager.shared().set(degree,
-                                                                   fileType: self.fileType(),
-                                                                   type: self.type(),
-                                                                   fileName: fileName)
-                                    TestDegreeManager.shared().set(degree,
-                                                                   fileType: self.fileType(),
-                                                                   type: self.type(),
-                                                                   fileName: fileName,
-                                                                   refFilePath: fName+".jpg");
+                                    let degree = ImageProcess.compareJPEG(rComparePath, checkPath: cComparePath, processCover: true)
                                     
-                                    tDegree += degree
-                                    tCount += 1
-                                    
-                                    self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"快照对比完成,图像相似度 \(degree)%\n",
-                                                                                             attributes:[.foregroundColor : color]))
+                                    if degree == -1 {
+                                        self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"快照对比失败,生成快照失败或无比对文件\n",
+                                                                                                 attributes:[.foregroundColor : NSColor.red]))
+                                    }else {
+                                        var color = NSColor.black
+                                        if fabs(degree-100.0) >= 0.01 {
+                                            color = NSColor.red
+                                        }
+                                        
+                                        TestDegreeManager.shared().set(degree,
+                                                                       fileType: self.fileType(),
+                                                                       type: self.type(),
+                                                                       fileName: fileName)
+                                        TestDegreeManager.shared().set(degree,
+                                                                       fileType: self.fileType(),
+                                                                       type: self.type(),
+                                                                       fileName: fileName,
+                                                                       refFilePath: fName+".jpg");
+                                        
+                                        tDegree += degree
+                                        tCount += 1
+                                        
+                                        self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"快照对比完成,图像相似度 \(degree)%\n",
+                                                                                                 attributes:[.foregroundColor : color]))
+                                    }
+                                }else {
+                                    self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"快照生成失败\n",
+                                                                                             attributes:[.foregroundColor : NSColor.red]))
                                 }
-                            }else {
-                                self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"快照生成失败\n",
-                                                                                         attributes:[.foregroundColor : NSColor.red]))
+                                
+                                processThumbSemaphore.signal()
                             }
-                            
-                            processThumbSemaphore.signal()
                         }
                         processThumbSemaphore.wait()
                         

+ 2 - 2
KdanAutoTest/KdanAuto/Class/CompareViewController/CompareViewController.swift

@@ -77,7 +77,7 @@ class CompareViewController: NSViewController, NSTableViewDataSource, NSTableVie
             
             if (checkBox.state == .on) {
                 
-                for tfile in _files {
+                for tfile in NSArray(array: _files) {
                     let file = tfile as! NSMutableDictionary;
                     
                     let degree = file.degree()
@@ -189,7 +189,7 @@ class CompareViewController: NSViewController, NSTableViewDataSource, NSTableVie
         if (self.filterBtn.state == .on) {
             _showFiles = NSMutableArray()
             
-            for tfile in _files {
+            for tfile in NSArray(array: _files) {
                 let file = tfile as! NSMutableDictionary;
                 
                 let degree = file.degree()

+ 51 - 48
KdanAutoTest/KdanAuto/Class/CompareViewController/ImageCompareCellView.swift

@@ -80,14 +80,16 @@ class ImageCompareCellView : NSTableCellView {
                 _imageView04.image = nil;
                 
                 DispatchQueue.global().async {
-                    let image = NSImage.init(contentsOfFile: comparePath)
-                    
-                    let image2 = NSImage.init(contentsOfFile: resultPath)
-                    
-                    DispatchQueue.main.async {
-                        autoreleasepool {
-                            self._imageView01.image = image;
-                            self._imageView02.image = image2;
+                    autoreleasepool {
+                        let image = NSImage.init(contentsOfFile: comparePath)
+                        
+                        let image2 = NSImage.init(contentsOfFile: resultPath)
+                        
+                        DispatchQueue.main.async {
+                            autoreleasepool {
+                                self._imageView01.image = image;
+                                self._imageView02.image = image2;
+                            }
                         }
                     }
                 }
@@ -176,31 +178,33 @@ class ImageCompareCellView : NSTableCellView {
                     _activityView.isHidden = false
                     _activityView.startAnimation(sender)
                     DispatchQueue.global().async {
-                        var image2 = nil as NSImage?
-                        
                         autoreleasepool {
-                            let coverPath = NSString(format: "%@_cover.png", NSString(string: resultPath).deletingPathExtension) as String
+                            var image2 = nil as NSImage?
                             
-                            if FileManager.default.fileExists(atPath: coverPath) {
-                                image2 = NSImage.init(contentsOfFile: coverPath)
-                            }else {
-                                image2 = ImageProcess.processImage(resultPath, checkPath: comparePath)
+                            autoreleasepool {
+                                let coverPath = NSString(format: "%@_cover.png", NSString(string: resultPath).deletingPathExtension) as String
                                 
-                                if nil != image2 {
-                                    let rep = NSBitmapImageRep.init(cgImage: image2!.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
-                                    let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [:]);
+                                if FileManager.default.fileExists(atPath: coverPath) {
+                                    image2 = NSImage.init(contentsOfFile: coverPath)
+                                }else {
+                                    image2 = ImageProcess.processImage(resultPath, checkPath: comparePath)
                                     
-                                    let url = URL.init(fileURLWithPath: coverPath, isDirectory: false)
-                                    try? data!.write(to: url)
+                                    if nil != image2 {
+                                        let rep = NSBitmapImageRep.init(cgImage: image2!.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
+                                        let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [:]);
+                                        
+                                        let url = URL.init(fileURLWithPath: coverPath, isDirectory: false)
+                                        try? data!.write(to: url)
+                                    }
                                 }
                             }
-                        }
-                        
-                        DispatchQueue.main.sync {
-                            autoreleasepool {
-                                self._imageView04.image = image2
-                                self._activityView.isHidden = true
-                                self._activityView.stopAnimation(sender)
+                            
+                            DispatchQueue.main.sync {
+                                autoreleasepool {
+                                    self._imageView04.image = image2
+                                    self._activityView.isHidden = true
+                                    self._activityView.stopAnimation(sender)
+                                }
                             }
                         }
                     }
@@ -220,14 +224,14 @@ class ImageCompareCellView : NSTableCellView {
             
             let image2 = NSImage.init(contentsOfFile: resultPath)
             DispatchQueue.main.sync {
-                _imageView01.image = image;
-                _imageView02.image = image2;
-                _imageView03.image = nil;
-                _imageView04.image = nil;
-                
-                _showCoverBtn.isHidden = true;
-                //            _segmentedControl.isHidden = true;
-                //            _degreeInfoLbl.isHidden = true;
+                autoreleasepool {
+                    _imageView01.image = image;
+                    _imageView02.image = image2;
+                    _imageView03.image = nil;
+                    _imageView04.image = nil;
+                    
+                    _showCoverBtn.isHidden = true;
+                }
             }
             
             let nimage = self.imageFroView(self)
@@ -243,13 +247,15 @@ class ImageCompareCellView : NSTableCellView {
             
             let image = NSImage.init(contentsOfFile: comparePath)
             DispatchQueue.main.sync {
-                _imageView01.image = nil;
-                _imageView02.image = nil;
-                
-                self._imageView03.image = image;
-                self._imageView03.isHidden = false
-                self._imageView04.image = nil;
-                self._imageView04.isHidden = false
+                autoreleasepool {
+                    _imageView01.image = nil;
+                    _imageView02.image = nil;
+                    
+                    self._imageView03.image = image;
+                    self._imageView03.isHidden = false
+                    self._imageView04.image = nil;
+                    self._imageView04.isHidden = false
+                }
             }
             
             var image2 = nil as NSImage?
@@ -271,12 +277,9 @@ class ImageCompareCellView : NSTableCellView {
             }
             
             DispatchQueue.main.sync {
-                self._imageView04.image = image2
-//                self._activityView.isHidden = true
-                
-//                self._showCoverBtn.isHidden = true;
-                self._segmentedControl.isHidden = true;
-                self._degreeInfoLbl.isHidden = true;
+                autoreleasepool {
+                    self._imageView04.image = image2
+                }
             }
             
             let nimage = self.imageFroView(self)

+ 3 - 3
KdanAutoTest/KdanAuto/Class/Norrmal/AutoTestAdvanceSettingView.swift

@@ -109,7 +109,7 @@ class AutoTestAdvanceSettingView : NSView, NSTableViewDataSource, NSTableViewDel
                 _keyContentView.setFrameSize(NSSize.init(width: _keyScrollView.frame.width - 28, height: height))
             }
             
-            for btn in _keyViews {
+            for btn in NSArray(array: _keyViews) {
                 let i = _keyViews.index(of: btn)
                 let checkBtn = btn as! NSButton
                 
@@ -162,7 +162,7 @@ class AutoTestAdvanceSettingView : NSView, NSTableViewDataSource, NSTableViewDel
         
         let checkKeys = NSArray(array: _autoTestObj?.keys() ?? [])
         
-        for btn in _keyViews {
+        for btn in NSArray(array: _keyViews) {
             let cBtn = btn as! NSButton
             
             if cBtn.state == .on {
@@ -241,7 +241,7 @@ class AutoTestAdvanceSettingView : NSView, NSTableViewDataSource, NSTableViewDel
                 
             }
             
-            for item in paths {
+            for item in NSArray(array: paths) {
                 let path = item as! String
                 
                 if comboBox.indexOfSelectedItem == 0 {

+ 1 - 1
KdanAutoTest/KdanAuto/Class/Norrmal/Cell/TestCaseCellView.swift

@@ -81,7 +81,7 @@ class TestCaseCellView : NSTableCellView {
         
         var width = 0.0;
         
-        for tf in _keyViews {
+        for tf in NSArray(array: _keyViews) {
             let i = _keyViews.index(of: tf)
             let tfView = tf as! NSTextField
             

+ 1 - 1
KdanAutoTest/KdanAuto/Class/Setting/SettingViewController.swift

@@ -56,7 +56,7 @@ class SettingViewController : NSViewController, NSTextFieldDelegate, NSWindowDel
         let path = NSString(string: Bundle.main.sharedFrameworksPath!).deletingLastPathComponent.appending("/Frameworks");
         NSLog("\(path)")
         let subpaths = NSArray(array: try! FileManager.default.contentsOfDirectory(atPath: path))
-        for item in subpaths {
+        for item in NSArray(array: subpaths) {
             var subPath = item as! String
             subPath = NSString(string: subPath).pathComponents.first ?? ""
             subPath = NSString(string: subPath).deletingPathExtension

+ 1 - 0
KdanAutoTest/KdanAuto/Class/Tools/FileConveter/FileConverter.swift

@@ -315,6 +315,7 @@ class FileConverter : NSObject, CPDFConverterDelegate, CPDFConverterFPDelegate {
         autoreleasepool {
             didSuccess = nil == error ? 1 : 0
             self.fpConverter?.stopConvertsionIfNeed()
+            self.fpConverter = nil;
             
             Thread.sleep(forTimeInterval: 0.3)
             

+ 76 - 69
KdanAutoTest/KdanAuto/Class/Tools/ImageProcess/ImageProcess.swift

@@ -109,59 +109,62 @@ class ImageProcess : NSObject {
             let operateQueue = OperationQueue()
             operateQueue.maxConcurrentOperationCount = 20;
             var processCount = Int(0)
+        
             for w in 0...maxWidth {
                 let x = Int(w)
                 
                 operateQueue.addOperation {
-                    for h in 0...maxHeight {
-                        let y = Int(h)
-                        
-                        objc_sync_enter(self)
-                        let cColor = checkImageRep.colorAt(x: x, y: y) as! NSColor
-                        let rColor = resultImageRep.colorAt(x: x, y: y) as! NSColor
-                        objc_sync_exit(self)
-                        
-                        let cr = Int(cColor.redComponent*255)
-                        let cg = Int(cColor.greenComponent*255)
-                        let cb = Int(cColor.blueComponent*255)
-                        let ca = Int(cColor.blueComponent*255)
-                        
-                        let rr = Int(rColor.redComponent*255)
-                        let rg = Int(rColor.greenComponent*255)
-                        let rb = Int(rColor.blueComponent*255)
-                        let ra = Int(rColor.blueComponent*255)
-                        
-                        //                if (cColor.isEqual(to: rColor)) {
-                        if (abs(cr - rr) <= compareDifValue &&
-                            abs(cg - rg) <= compareDifValue &&
-                            abs(cb - rb) <= compareDifValue &&
-                            abs(ca - ra) <= compareDifValue) {
-                            equalCount = equalCount + 1
+                    autoreleasepool {
+                        for h in 0...maxHeight {
+                            let y = Int(h)
                             
-                            if cColor.isEqual(to: bgColor) {
+                            objc_sync_enter(self)
+                            let cColor = checkImageRep.colorAt(x: x, y: y) as! NSColor
+                            let rColor = resultImageRep.colorAt(x: x, y: y) as! NSColor
+                            objc_sync_exit(self)
+                            
+                            let cr = Int(cColor.redComponent*255)
+                            let cg = Int(cColor.greenComponent*255)
+                            let cb = Int(cColor.blueComponent*255)
+                            let ca = Int(cColor.blueComponent*255)
+                            
+                            let rr = Int(rColor.redComponent*255)
+                            let rg = Int(rColor.greenComponent*255)
+                            let rb = Int(rColor.blueComponent*255)
+                            let ra = Int(rColor.blueComponent*255)
+                            
+                            //                if (cColor.isEqual(to: rColor)) {
+                            if (abs(cr - rr) <= compareDifValue &&
+                                abs(cg - rg) <= compareDifValue &&
+                                abs(cb - rb) <= compareDifValue &&
+                                abs(ca - ra) <= compareDifValue) {
+                                equalCount = equalCount + 1
+                                
+                                if cColor.isEqual(to: bgColor) {
+                                    objc_sync_enter(self)
+                                    bgCount += 1
+                                    objc_sync_exit(self)
+                                }
+                            }else if (processCover && nil != data){
+                                let addr = cWidth * 4 * y + x * 4
+                                
                                 objc_sync_enter(self)
-                                bgCount += 1
+                                var r = uint8(255)
+                                data!.replaceBytes(in: NSRange.init(location: addr+0, length: 1), withBytes: &r)
+                                var g = uint8(255 * rColor.greenComponent/2)
+                                data!.replaceBytes(in: NSRange.init(location: addr+1, length: 1), withBytes: &g)
+                                var b = uint8(255 * rColor.blueComponent/2)
+                                data!.replaceBytes(in: NSRange.init(location: addr+2, length: 1), withBytes: &b)
+                                var a = uint8(255 * rColor.alphaComponent * 0.7)
+                                data!.replaceBytes(in: NSRange.init(location: addr+3, length: 1), withBytes: &a)
                                 objc_sync_exit(self)
                             }
-                        }else if (processCover && nil != data){
-                            let addr = cWidth * 4 * y + x * 4
-                            
-                            objc_sync_enter(self)
-                            var r = uint8(255)
-                            data!.replaceBytes(in: NSRange.init(location: addr+0, length: 1), withBytes: &r)
-                            var g = uint8(255 * rColor.greenComponent/2)
-                            data!.replaceBytes(in: NSRange.init(location: addr+1, length: 1), withBytes: &g)
-                            var b = uint8(255 * rColor.blueComponent/2)
-                            data!.replaceBytes(in: NSRange.init(location: addr+2, length: 1), withBytes: &b)
-                            var a = uint8(255 * rColor.alphaComponent * 0.7)
-                            data!.replaceBytes(in: NSRange.init(location: addr+3, length: 1), withBytes: &a)
-                            objc_sync_exit(self)
                         }
-                    }
-                    
-                    processCount = processCount+1
-                    if (processCount >= maxWidth) {
-                        semaphore.signal()
+                        
+                        processCount = processCount+1
+                        if (processCount >= maxWidth) {
+                            semaphore.signal()
+                        }
                     }
                 }
             }
@@ -171,29 +174,31 @@ class ImageProcess : NSObject {
             let outDegree = Double(max(equalCount-bgCount, 1)/(max(Double(cWidth) * Double(cHeight)-bgCount, 1)) * 100.0)
             if (abs(outDegree - 100) > 0 && processCover && nil != data) {
                 DispatchQueue.global().async {
-                    let cfData = CFDataCreate(kCFAllocatorDefault, data?.bytes, data!.length)
-                    let dataProvider = CGDataProvider.init(data: cfData!)
-                    
-                    let colorSpace = CGColorSpaceCreateDeviceRGB()
-                    let cgImage = CGImage.init(width: cWidth,
-                                               height: cHeight,
-                                               bitsPerComponent: 8,
-                                               bitsPerPixel: 8 * 4,
-                                               bytesPerRow: cWidth * 4,
-                                               space: colorSpace,
-                                               bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue | CGBitmapInfo.byteOrderDefault.rawValue) ?? CGBitmapInfo.byteOrderDefault,
-                                               provider: dataProvider!,
-                                               decode: nil,
-                                               shouldInterpolate: true,
-                                               intent: CGColorRenderingIntent.defaultIntent)
-                    
-                    if nil != cgImage {
-                        let coverPath = NSString(format: "%@_cover.png", NSString(string: resultPath).deletingPathExtension) as! String
-                        let rep = NSBitmapImageRep.init(cgImage: cgImage!)
+                    autoreleasepool {
+                        let cfData = CFDataCreate(kCFAllocatorDefault, data?.bytes, data!.length)
+                        let dataProvider = CGDataProvider.init(data: cfData!)
                         
-                        if let saveData = rep.representation(using: .png, properties: [:]) {
-                            let url = URL.init(fileURLWithPath: coverPath, isDirectory: false)
-                            try? saveData.write(to: url)
+                        let colorSpace = CGColorSpaceCreateDeviceRGB()
+                        let cgImage = CGImage.init(width: cWidth,
+                                                   height: cHeight,
+                                                   bitsPerComponent: 8,
+                                                   bitsPerPixel: 8 * 4,
+                                                   bytesPerRow: cWidth * 4,
+                                                   space: colorSpace,
+                                                   bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue | CGBitmapInfo.byteOrderDefault.rawValue) ?? CGBitmapInfo.byteOrderDefault,
+                                                   provider: dataProvider!,
+                                                   decode: nil,
+                                                   shouldInterpolate: true,
+                                                   intent: CGColorRenderingIntent.defaultIntent)
+                        
+                        if nil != cgImage {
+                            let coverPath = NSString(format: "%@_cover.png", NSString(string: resultPath).deletingPathExtension) as! String
+                            let rep = NSBitmapImageRep.init(cgImage: cgImage!)
+                            
+                            if let saveData = rep.representation(using: .png, properties: [:]) {
+                                let url = URL.init(fileURLWithPath: coverPath, isDirectory: false)
+                                try? saveData.write(to: url)
+                            }
                         }
                     }
                 }
@@ -388,9 +393,11 @@ class ImageProcess : NSObject {
             }
             
             DispatchQueue.global().async {
-                NSLog(String("过滤点数目\(bgCount)"))
-                
-                complention(outDegree)
+                autoreleasepool {
+                    NSLog(String("过滤点数目\(bgCount)"))
+                    
+                    complention(outDegree)
+                }
             }
         }
     }

+ 7 - 5
KdanAutoTest/KdanAuto/ViewController.swift

@@ -258,11 +258,13 @@ class ViewController : NSViewController, SettingViewControllerDelegate, AutoTest
             for fileType in testFileTypes {
                 let types = testTypeInfo[fileType] as! NSArray
                 for typeInfo in types {
-                    let ti = typeInfo as! NSDictionary
-                    let type = ti["Type"] as! NSString
-                    
-                    let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
-                    testObject?.updateRefImage()
+                    autoreleasepool {
+                        let ti = typeInfo as! NSDictionary
+                        let type = ti["Type"] as! NSString
+                        
+                        let testObject = AutoTest.autoTestFor(fileType as NSString, type: type)
+                        testObject?.updateRefImage()
+                    }
                 }
                 DispatchQueue.main.sync {
                     self.reloadListData()

+ 16 - 14
KdanAutoTest/ProcessCheckFile/Process/ProcessThumbnal.swift

@@ -161,24 +161,26 @@ class ProcessThumbnal : NSObject {
             let request = QLThumbnailGenerator.Request.init(fileAt: url, size: outputSize, scale: 2.0, representationTypes: QLThumbnailGenerator.Request.RepresentationTypes.thumbnail)
             generate.generateBestRepresentation(for: request,
                                                 completion: { (representation, error) in
-                if nil != representation {
-                    autoreleasepool {
-                        let image = representation!.nsImage as NSImage
-                        
-                        if nil != image {
-                            didFinished = true
-                            
-                            let rep = NSBitmapImageRep.init(cgImage: image.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
-                            let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [:]);
+                autoreleasepool {
+                    if nil != representation {
+                        autoreleasepool {
+                            let image = representation!.nsImage as NSImage
                             
-                            let url = URL.init(fileURLWithPath: desPath, isDirectory: false)
-                            try? data!.write(to: url)
+                            if nil != image {
+                                didFinished = true
+                                
+                                let rep = NSBitmapImageRep.init(cgImage: image.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
+                                let data = rep.representation(using: NSBitmapImageRep.FileType.png, properties: [:]);
+                                
+                                let url = URL.init(fileURLWithPath: desPath, isDirectory: false)
+                                try? data!.write(to: url)
+                            }
                         }
                     }
+                    
+                    generate.cancel(request)
+                    semaphore.signal()
                 }
-                
-                generate.cancel(request)
-                semaphore.signal()
             })
         }