SwiftGithubUser.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. //
  2. // SwiftGithubUser.swift
  3. // ModelBenchmark
  4. //
  5. // Created by ibireme on 2017/8/6.
  6. // Copyright © 2017年 ibireme. All rights reserved.
  7. //
  8. import Foundation
  9. import QuartzCore
  10. import ObjectMapper
  11. import HandyJSON
  12. import SwiftyJSON
  13. extension GithubUserObjectMapper {
  14. static func benchmark() {
  15. var start, end, time: Double
  16. var data: Data
  17. var json: [String: Any]
  18. do {
  19. let path = Bundle.main.url(forResource: "user", withExtension: "json")
  20. data = try Data(contentsOf: path!);
  21. json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String: Any]
  22. } catch let error {
  23. print("Decode `user.json` in main bundle failed.")
  24. print(error)
  25. return
  26. }
  27. print("Codable(*#): ", separator: "", terminator: "")
  28. let decoder = JSONDecoder()
  29. let encoder = JSONEncoder()
  30. start = CACurrentMediaTime()
  31. for _ in 1...10000 {
  32. _ = try! decoder.decode(GithubUserCodable.self, from: data)
  33. }
  34. end = CACurrentMediaTime()
  35. time = (end - start) * 1000.0
  36. print(String(format:"%8.2f", time) + " ", separator: "", terminator: "")
  37. let githubUserCodable = try! decoder.decode(GithubUserCodable.self, from: data)
  38. start = CACurrentMediaTime()
  39. for _ in 1...10000 {
  40. _ = try! encoder.encode(githubUserCodable)
  41. }
  42. end = CACurrentMediaTime()
  43. time = (end - start) * 1000.0
  44. print(String(format:"%8.2f", time), separator: "", terminator: "")
  45. print("")
  46. print("ObjectMapper(#): ", separator: "", terminator: "")
  47. start = CACurrentMediaTime()
  48. for _ in 1...10000 {
  49. _ = GithubUserObjectMapper(JSON: json)
  50. }
  51. end = CACurrentMediaTime()
  52. time = (end - start) * 1000.0
  53. print(String(format:"%8.2f", time) + " ", separator: "", terminator: "")
  54. let githubUserObjectMapper = GithubUserObjectMapper(JSON: json)
  55. start = CACurrentMediaTime()
  56. for _ in 1...10000 {
  57. _ = githubUserObjectMapper?.toJSON()
  58. }
  59. end = CACurrentMediaTime()
  60. time = (end - start) * 1000.0
  61. print(String(format:"%8.2f", time), separator: "", terminator: "")
  62. print("")
  63. print("HandyJSON(#): ", separator: "", terminator: "")
  64. start = CACurrentMediaTime()
  65. for _ in 1...10000 {
  66. _ = GithubUserHandyJSON.deserialize(from: json as NSDictionary)
  67. }
  68. end = CACurrentMediaTime()
  69. time = (end - start) * 1000.0
  70. print(String(format:"%8.2f", time) + " ", separator: "", terminator: "")
  71. let githubUserHandyJSON = GithubUserHandyJSON.deserialize(from: json as NSDictionary)
  72. start = CACurrentMediaTime()
  73. for _ in 1...10000 {
  74. _ = githubUserHandyJSON?.toJSON()
  75. }
  76. end = CACurrentMediaTime()
  77. time = (end - start) * 1000.0
  78. print(String(format:"%8.2f", time), separator: "", terminator: "")
  79. print("")
  80. print("SwiftyJSON(#): ", separator: "", terminator: "")
  81. let swiftJson = JSON(data: data)
  82. start = CACurrentMediaTime()
  83. for _ in 1...10000 {
  84. _ = GithubUserSwifty(fromJson: swiftJson)
  85. }
  86. end = CACurrentMediaTime()
  87. time = (end - start) * 1000.0
  88. print(String(format:"%8.2f", time) + " ", separator: "", terminator: "")
  89. let githubUserSwifty = GithubUserSwifty(fromJson: swiftJson)
  90. start = CACurrentMediaTime()
  91. for _ in 1...10000 {
  92. _ = githubUserSwifty.toDictionary()
  93. }
  94. end = CACurrentMediaTime()
  95. time = (end - start) * 1000.0
  96. print(String(format:"%8.2f", time), separator: "", terminator: "")
  97. print("")
  98. }
  99. }
  100. // Swift Codable
  101. struct GithubUserCodable: Codable {
  102. var login: String?
  103. var userID: UInt64?
  104. var avatarURL: String?
  105. var gravatarID: String?
  106. var url: String?
  107. var htmlURL: String?
  108. var followersURL: String?
  109. var followingURL: String?
  110. var gistsURL: String?
  111. var starredURL: String?
  112. var subscriptionsURL: String?
  113. var organizationsURL: String?
  114. var reposURL: String?
  115. var eventsURL: String?
  116. var receivedEventsURL: String?
  117. var type: String?
  118. var siteAdmin : Bool?
  119. var name: String?
  120. var company: String?
  121. var blog: String?
  122. var location: String?
  123. var email: String?
  124. var hireable: String?
  125. var bio: String?
  126. var publicRepos: Int?
  127. var publicGists: Int?
  128. var followers: Int?
  129. var following: Int?
  130. var createdAt: Date?
  131. var updatedAt: Date?
  132. enum CodingKeys : String, CodingKey {
  133. case login = "login"
  134. case userID = "id"
  135. case avatarURL = "avatar_url"
  136. case gravatarID = "gravatar_id"
  137. case url = "url"
  138. case htmlURL = "html_url"
  139. case followersURL = "followers_url"
  140. case followingURL = "following_url"
  141. case gistsURL = "gists_url"
  142. case starredURL = "starred_url"
  143. case subscriptionsURL = "subscriptions_url"
  144. case organizationsURL = "organizations_url"
  145. case reposURL = "repos_url"
  146. case eventsURL = "events_url"
  147. case receivedEventsURL = "received_events_url"
  148. case type = "type"
  149. case siteAdmin = "site_admin"
  150. case name = "name"
  151. case company = "company"
  152. case blog = "blog"
  153. case location = "location"
  154. case email = "email"
  155. case hireable = "hireable"
  156. case bio = "bio"
  157. case publicRepos = "public_repos"
  158. case publicGists = "public_gists"
  159. case followers = "followers"
  160. case following = "following"
  161. case createdAt = "created_at"
  162. case updatedAt = "updated_at"
  163. }
  164. }
  165. // ObjectMapper
  166. class GithubUserObjectMapper : Mappable {
  167. var login: String?
  168. var userID: UInt64?
  169. var avatarURL: String?
  170. var gravatarID: String?
  171. var url: String?
  172. var htmlURL: String?
  173. var followersURL: String?
  174. var followingURL: String?
  175. var gistsURL: String?
  176. var starredURL: String?
  177. var subscriptionsURL: String?
  178. var organizationsURL: String?
  179. var reposURL: String?
  180. var eventsURL: String?
  181. var receivedEventsURL: String?
  182. var type: String?
  183. var siteAdmin : Bool?
  184. var name: String?
  185. var company: String?
  186. var blog: String?
  187. var location: String?
  188. var email: String?
  189. var hireable: String?
  190. var bio: String?
  191. var publicRepos: Int?
  192. var publicGists: Int?
  193. var followers: Int?
  194. var following: Int?
  195. var createdAt: Date?
  196. var updatedAt: Date?
  197. var test: NSValue?
  198. required init?(map: Map) {
  199. }
  200. func mapping(map: Map) {
  201. login <- map["login"]
  202. userID <- map["id"]
  203. avatarURL <- map["avatar_url"]
  204. gravatarID <- map["gravatar_id"]
  205. url <- map["url"]
  206. htmlURL <- map["html_url"]
  207. followersURL <- map["followers_url"]
  208. followingURL <- map["following_url"]
  209. gistsURL <- map["gists_url"]
  210. starredURL <- map["starred_url"]
  211. subscriptionsURL <- map["subscriptions_url"]
  212. organizationsURL <- map["organizations_url"]
  213. reposURL <- map["repos_url"]
  214. eventsURL <- map["events_url"]
  215. receivedEventsURL <- map["received_events_url"]
  216. type <- map["type"]
  217. siteAdmin <- map["site_admin"]
  218. name <- map["name"]
  219. company <- map["company"]
  220. blog <- map["blog"]
  221. location <- map["location"]
  222. email <- map["email"]
  223. hireable <- map["hireable"]
  224. bio <- map["bio"]
  225. publicRepos <- map["public_repos"]
  226. publicGists <- map["public_gists"]
  227. followers <- map["followers"]
  228. following <- map["following"]
  229. createdAt <- (map["created_at"], DateTransform())
  230. updatedAt <- (map["updated_at"], DateTransform())
  231. test <- map["test"]
  232. }
  233. }
  234. // HandyJSON
  235. class GithubUserHandyJSON: HandyJSON {
  236. var login: String?
  237. var userID: UInt64?
  238. var avatarURL: String?
  239. var gravatarID: String?
  240. var url: String?
  241. var htmlURL: String?
  242. var followersURL: String?
  243. var followingURL: String?
  244. var gistsURL: String?
  245. var starredURL: String?
  246. var subscriptionsURL: String?
  247. var organizationsURL: String?
  248. var reposURL: String?
  249. var eventsURL: String?
  250. var receivedEventsURL: String?
  251. var type: String?
  252. var siteAdmin : Bool?
  253. var name: String?
  254. var company: String?
  255. var blog: String?
  256. var location: String?
  257. var email: String?
  258. var hireable: String?
  259. var bio: String?
  260. var publicRepos: Int?
  261. var publicGists: Int?
  262. var followers: Int?
  263. var following: Int?
  264. var createdAt: Date?
  265. var updatedAt: Date?
  266. var test: NSValue?
  267. required init() {}
  268. }
  269. // SwiftyJSON
  270. // Model file generated using JSONExport: https://github.com/Ahmed-Ali/JSONExport
  271. class GithubUserSwifty : NSObject, NSCoding{
  272. var avatarUrl : String!
  273. var bio : String!
  274. var blog : String!
  275. var company : String!
  276. var email : String!
  277. var eventsUrl : String!
  278. var followers : Int!
  279. var followersUrl : String!
  280. var following : Int!
  281. var followingUrl : String!
  282. var gistsUrl : String!
  283. var gravatarId : String!
  284. var hireable : String!
  285. var htmlUrl : String!
  286. var id : Int!
  287. var location : String!
  288. var login : String!
  289. var name : String!
  290. var organizationsUrl : String!
  291. var publicGists : Int!
  292. var publicRepos : Int!
  293. var receivedEventsUrl : String!
  294. var reposUrl : String!
  295. var siteAdmin : Bool!
  296. var starredUrl : String!
  297. var subscriptionsUrl : String!
  298. var type : String!
  299. var url : String!
  300. /**
  301. * Instantiate the instance using the passed json values to set the properties values
  302. */
  303. init(fromJson json: JSON!){
  304. if json.isEmpty{
  305. return
  306. }
  307. avatarUrl = json["avatar_url"].stringValue
  308. bio = json["bio"].stringValue
  309. blog = json["blog"].stringValue
  310. company = json["company"].stringValue
  311. email = json["email"].stringValue
  312. eventsUrl = json["events_url"].stringValue
  313. followers = json["followers"].intValue
  314. followersUrl = json["followers_url"].stringValue
  315. following = json["following"].intValue
  316. followingUrl = json["following_url"].stringValue
  317. gistsUrl = json["gists_url"].stringValue
  318. gravatarId = json["gravatar_id"].stringValue
  319. hireable = json["hireable"].stringValue
  320. htmlUrl = json["html_url"].stringValue
  321. id = json["id"].intValue
  322. location = json["location"].stringValue
  323. login = json["login"].stringValue
  324. name = json["name"].stringValue
  325. organizationsUrl = json["organizations_url"].stringValue
  326. publicGists = json["public_gists"].intValue
  327. publicRepos = json["public_repos"].intValue
  328. receivedEventsUrl = json["received_events_url"].stringValue
  329. reposUrl = json["repos_url"].stringValue
  330. siteAdmin = json["site_admin"].boolValue
  331. starredUrl = json["starred_url"].stringValue
  332. subscriptionsUrl = json["subscriptions_url"].stringValue
  333. type = json["type"].stringValue
  334. url = json["url"].stringValue
  335. }
  336. /**
  337. * Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
  338. */
  339. func toDictionary() -> [String:Any]
  340. {
  341. var dictionary = [String:Any]()
  342. if avatarUrl != nil{
  343. dictionary["avatar_url"] = avatarUrl
  344. }
  345. if bio != nil{
  346. dictionary["bio"] = bio
  347. }
  348. if blog != nil{
  349. dictionary["blog"] = blog
  350. }
  351. if company != nil{
  352. dictionary["company"] = company
  353. }
  354. if email != nil{
  355. dictionary["email"] = email
  356. }
  357. if eventsUrl != nil{
  358. dictionary["events_url"] = eventsUrl
  359. }
  360. if followers != nil{
  361. dictionary["followers"] = followers
  362. }
  363. if followersUrl != nil{
  364. dictionary["followers_url"] = followersUrl
  365. }
  366. if following != nil{
  367. dictionary["following"] = following
  368. }
  369. if followingUrl != nil{
  370. dictionary["following_url"] = followingUrl
  371. }
  372. if gistsUrl != nil{
  373. dictionary["gists_url"] = gistsUrl
  374. }
  375. if gravatarId != nil{
  376. dictionary["gravatar_id"] = gravatarId
  377. }
  378. if hireable != nil{
  379. dictionary["hireable"] = hireable
  380. }
  381. if htmlUrl != nil{
  382. dictionary["html_url"] = htmlUrl
  383. }
  384. if id != nil{
  385. dictionary["id"] = id
  386. }
  387. if location != nil{
  388. dictionary["location"] = location
  389. }
  390. if login != nil{
  391. dictionary["login"] = login
  392. }
  393. if name != nil{
  394. dictionary["name"] = name
  395. }
  396. if organizationsUrl != nil{
  397. dictionary["organizations_url"] = organizationsUrl
  398. }
  399. if publicGists != nil{
  400. dictionary["public_gists"] = publicGists
  401. }
  402. if publicRepos != nil{
  403. dictionary["public_repos"] = publicRepos
  404. }
  405. if receivedEventsUrl != nil{
  406. dictionary["received_events_url"] = receivedEventsUrl
  407. }
  408. if reposUrl != nil{
  409. dictionary["repos_url"] = reposUrl
  410. }
  411. if siteAdmin != nil{
  412. dictionary["site_admin"] = siteAdmin
  413. }
  414. if starredUrl != nil{
  415. dictionary["starred_url"] = starredUrl
  416. }
  417. if subscriptionsUrl != nil{
  418. dictionary["subscriptions_url"] = subscriptionsUrl
  419. }
  420. if type != nil{
  421. dictionary["type"] = type
  422. }
  423. if url != nil{
  424. dictionary["url"] = url
  425. }
  426. return dictionary
  427. }
  428. /**
  429. * NSCoding required initializer.
  430. * Fills the data from the passed decoder
  431. */
  432. @objc required init(coder aDecoder: NSCoder)
  433. {
  434. avatarUrl = aDecoder.decodeObject(forKey: "avatar_url") as? String
  435. bio = aDecoder.decodeObject(forKey: "bio") as? String
  436. blog = aDecoder.decodeObject(forKey: "blog") as? String
  437. company = aDecoder.decodeObject(forKey: "company") as? String
  438. email = aDecoder.decodeObject(forKey: "email") as? String
  439. eventsUrl = aDecoder.decodeObject(forKey: "events_url") as? String
  440. followers = aDecoder.decodeObject(forKey: "followers") as? Int
  441. followersUrl = aDecoder.decodeObject(forKey: "followers_url") as? String
  442. following = aDecoder.decodeObject(forKey: "following") as? Int
  443. followingUrl = aDecoder.decodeObject(forKey: "following_url") as? String
  444. gistsUrl = aDecoder.decodeObject(forKey: "gists_url") as? String
  445. gravatarId = aDecoder.decodeObject(forKey: "gravatar_id") as? String
  446. hireable = aDecoder.decodeObject(forKey: "hireable") as? String
  447. htmlUrl = aDecoder.decodeObject(forKey: "html_url") as? String
  448. id = aDecoder.decodeObject(forKey: "id") as? Int
  449. location = aDecoder.decodeObject(forKey: "location") as? String
  450. login = aDecoder.decodeObject(forKey: "login") as? String
  451. name = aDecoder.decodeObject(forKey: "name") as? String
  452. organizationsUrl = aDecoder.decodeObject(forKey: "organizations_url") as? String
  453. publicGists = aDecoder.decodeObject(forKey: "public_gists") as? Int
  454. publicRepos = aDecoder.decodeObject(forKey: "public_repos") as? Int
  455. receivedEventsUrl = aDecoder.decodeObject(forKey: "received_events_url") as? String
  456. reposUrl = aDecoder.decodeObject(forKey: "repos_url") as? String
  457. siteAdmin = aDecoder.decodeObject(forKey: "site_admin") as? Bool
  458. starredUrl = aDecoder.decodeObject(forKey: "starred_url") as? String
  459. subscriptionsUrl = aDecoder.decodeObject(forKey: "subscriptions_url") as? String
  460. type = aDecoder.decodeObject(forKey: "type") as? String
  461. url = aDecoder.decodeObject(forKey: "url") as? String
  462. }
  463. /**
  464. * NSCoding required method.
  465. * Encodes mode properties into the decoder
  466. */
  467. func encode(with aCoder: NSCoder)
  468. {
  469. if avatarUrl != nil{
  470. aCoder.encode(avatarUrl, forKey: "avatar_url")
  471. }
  472. if bio != nil{
  473. aCoder.encode(bio, forKey: "bio")
  474. }
  475. if blog != nil{
  476. aCoder.encode(blog, forKey: "blog")
  477. }
  478. if company != nil{
  479. aCoder.encode(company, forKey: "company")
  480. }
  481. if email != nil{
  482. aCoder.encode(email, forKey: "email")
  483. }
  484. if eventsUrl != nil{
  485. aCoder.encode(eventsUrl, forKey: "events_url")
  486. }
  487. if followers != nil{
  488. aCoder.encode(followers, forKey: "followers")
  489. }
  490. if followersUrl != nil{
  491. aCoder.encode(followersUrl, forKey: "followers_url")
  492. }
  493. if following != nil{
  494. aCoder.encode(following, forKey: "following")
  495. }
  496. if followingUrl != nil{
  497. aCoder.encode(followingUrl, forKey: "following_url")
  498. }
  499. if gistsUrl != nil{
  500. aCoder.encode(gistsUrl, forKey: "gists_url")
  501. }
  502. if gravatarId != nil{
  503. aCoder.encode(gravatarId, forKey: "gravatar_id")
  504. }
  505. if hireable != nil{
  506. aCoder.encode(hireable, forKey: "hireable")
  507. }
  508. if htmlUrl != nil{
  509. aCoder.encode(htmlUrl, forKey: "html_url")
  510. }
  511. if id != nil{
  512. aCoder.encode(id, forKey: "id")
  513. }
  514. if location != nil{
  515. aCoder.encode(location, forKey: "location")
  516. }
  517. if login != nil{
  518. aCoder.encode(login, forKey: "login")
  519. }
  520. if name != nil{
  521. aCoder.encode(name, forKey: "name")
  522. }
  523. if organizationsUrl != nil{
  524. aCoder.encode(organizationsUrl, forKey: "organizations_url")
  525. }
  526. if publicGists != nil{
  527. aCoder.encode(publicGists, forKey: "public_gists")
  528. }
  529. if publicRepos != nil{
  530. aCoder.encode(publicRepos, forKey: "public_repos")
  531. }
  532. if receivedEventsUrl != nil{
  533. aCoder.encode(receivedEventsUrl, forKey: "received_events_url")
  534. }
  535. if reposUrl != nil{
  536. aCoder.encode(reposUrl, forKey: "repos_url")
  537. }
  538. if siteAdmin != nil{
  539. aCoder.encode(siteAdmin, forKey: "site_admin")
  540. }
  541. if starredUrl != nil{
  542. aCoder.encode(starredUrl, forKey: "starred_url")
  543. }
  544. if subscriptionsUrl != nil{
  545. aCoder.encode(subscriptionsUrl, forKey: "subscriptions_url")
  546. }
  547. if type != nil{
  548. aCoder.encode(type, forKey: "type")
  549. }
  550. if url != nil{
  551. aCoder.encode(url, forKey: "url")
  552. }
  553. }
  554. }