From 998850446b53ac56536b037978a8de69b4536952 Mon Sep 17 00:00:00 2001 From: RockfordWei Date: Fri, 19 Jan 2018 15:52:40 -0500 Subject: [PATCH] Improving FileLogger performance by applying a flushable cache --- Sources/PerfectJWTAuth/DataworkUtility.swift | 26 ++++++++++++++------ Sources/PerfectJWTAuth/PerfectJWTAuth.swift | 2 +- Sources/UDBMariaDB/UDBMariaDB.swift | 2 +- Sources/UDBMySQL/UDBMySQL.swift | 2 +- Sources/UDBSQLite/UDBSQLite.swift | 2 +- 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/Sources/PerfectJWTAuth/DataworkUtility.swift b/Sources/PerfectJWTAuth/DataworkUtility.swift index 2f015ea..81971b6 100644 --- a/Sources/PerfectJWTAuth/DataworkUtility.swift +++ b/Sources/PerfectJWTAuth/DataworkUtility.swift @@ -121,6 +121,11 @@ public class FileLogger: LogManager { internal let _gmt: Bool internal let encoder: JSONEncoder + internal var cache = Data() + + /// size to cache the log before flushing + public static let cacheSize = 65536 + /// constructor /// - parameters: /// - path: a local folder to store the log file. **NOTE** the log files will be access.`\(date)`.log under the folder. @@ -141,7 +146,6 @@ public class FileLogger: LogManager { /// - message: an extra text message for this event, could be nil public func report(_ userId: String, level: LogLevel = .event, event: LoginManagementEvent, message: String? = nil) { let t = StdLogger.timestamp(self._gmt) - let fileName = "\(_path)/access.\(t.0).log" var r = LogRecord() r.id = UUID().string r.userId = userId @@ -151,15 +155,23 @@ public class FileLogger: LogManager { if let msg = message { r.message = msg } + guard let data = try? encoder.encode(r) else { + return + } + self.cache.append(data) + self.cache.append(contentsOf: ",\n".utf8) + guard self.cache.count > FileLogger.cacheSize else { + return + } + let fileName = "\(_path)/access.\(t.0).log" _lock.doWithLock { do { let f = File(fileName) try f.open(.append) - let data = try encoder.encode(r) - if let content = String(data: data, encoding: .utf8) { - try f.write(string: content + ",\n") - f.close() - } + let bytes:[UInt8] = self.cache.map { $0 } + try f.write(bytes: bytes) + f.close() + self.cache.removeAll() } catch { print("unable to log \(r)") } @@ -206,7 +218,7 @@ public final class DataworkUtility { /// - throws: Exception public static func explainProperties(of: Profile) throws -> [Field] { let data = try encoder.encode(of) - guard let json = String.init(bytes: data, encoding: .utf8), + guard let json = String(bytes: data, encoding: .utf8), let payload = try json.jsonDecode() as? [String:Any] else { throw Exception.json diff --git a/Sources/PerfectJWTAuth/PerfectJWTAuth.swift b/Sources/PerfectJWTAuth/PerfectJWTAuth.swift index 0d22911..98e2249 100644 --- a/Sources/PerfectJWTAuth/PerfectJWTAuth.swift +++ b/Sources/PerfectJWTAuth/PerfectJWTAuth.swift @@ -848,7 +848,7 @@ public class HTTPAccessControl: HTTPRequestFilter where Profile:Codable internal func profile(of: String) throws -> Profile { let bytes: [UInt8] = of.utf8.map { $0 } - let data = Data.init(bytes: bytes) + let data = Data(bytes: bytes) return try _decoder.decode(Profile.self, from: data) } diff --git a/Sources/UDBMariaDB/UDBMariaDB.swift b/Sources/UDBMariaDB/UDBMariaDB.swift index 821afd6..2b6951d 100644 --- a/Sources/UDBMariaDB/UDBMariaDB.swift +++ b/Sources/UDBMariaDB/UDBMariaDB.swift @@ -188,7 +188,7 @@ public class UDBMariaDB: UserDatabase { } let properties:[String] = fields.map { $0.name } let columns = ["id", "salt", "shadow"] + properties - let qmarks:[String] = Array.init(repeating: "?", count: columns.count) + let qmarks:[String] = Array(repeating: "?", count: columns.count) let col = columns.joined(separator: ",") let que = qmarks.joined(separator: ",") let sql = "INSERT INTO users (\(col)) VALUES(\(que))" diff --git a/Sources/UDBMySQL/UDBMySQL.swift b/Sources/UDBMySQL/UDBMySQL.swift index c16e61f..cfc3323 100644 --- a/Sources/UDBMySQL/UDBMySQL.swift +++ b/Sources/UDBMySQL/UDBMySQL.swift @@ -188,7 +188,7 @@ public class UDBMySQL: UserDatabase { } let properties:[String] = fields.map { $0.name } let columns = ["id", "salt", "shadow"] + properties - let qmarks:[String] = Array.init(repeating: "?", count: columns.count) + let qmarks:[String] = Array(repeating: "?", count: columns.count) let col = columns.joined(separator: ",") let que = qmarks.joined(separator: ",") let sql = "INSERT INTO users (\(col)) VALUES(\(que))" diff --git a/Sources/UDBSQLite/UDBSQLite.swift b/Sources/UDBSQLite/UDBSQLite.swift index 7e8c6c1..79afaf5 100644 --- a/Sources/UDBSQLite/UDBSQLite.swift +++ b/Sources/UDBSQLite/UDBSQLite.swift @@ -151,7 +151,7 @@ public class UDBSQLite: UserDatabase { } let properties:[String] = fields.map { $0.name } let columns = ["id", "salt", "shadow"] + properties - let qmarks:[String] = Array.init(repeating: "?", count: columns.count) + let qmarks:[String] = Array(repeating: "?", count: columns.count) let col = columns.joined(separator: ",") let que = qmarks.joined(separator: ",") let sql = "INSERT INTO users (\(col)) VALUES(\(que))"