Skip to content

Instantly share code, notes, and snippets.

@holtwick
Created February 3, 2018 20:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holtwick/e0a15c66d6ad06b9bc6b1d1bcc4c8e67 to your computer and use it in GitHub Desktop.
Save holtwick/e0a15c66d6ad06b9bc6b1d1bcc4c8e67 to your computer and use it in GitHub Desktop.
Speed and size testing for JSON, MessagePack, Plist and KeyedArchiver
// (C)opyright 2018-02-03 Dirk Holtwick, holtwick.it. All rights reserved.
#import <XCTest/XCTest.h>
#import "MPMessagePack.h"
#import "NSData+GZIP.h"
@interface SeaDataSpeedTests : XCTestCase
@end
#define LOOPS 10000
// 5
// 10000
@implementation SeaDataSpeedTests {
NSDictionary *obj;
}
- (void)setUp {
// id url = [[NSBundle bundleForClass:[self class]] URLForResource:@"Receipts" withExtension:@"json"];
// obj = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:url]
// options:0
// error:nil];
obj = @{ @"a": @1,
@"b": @[ @2, @3],
@"c": @"World",
};
}
- (NSData *)toDataPlistBinary {
return [NSPropertyListSerialization dataWithPropertyList:obj
format:NSPropertyListBinaryFormat_v1_0
options:0
error:nil];
}
- (NSData *)toDataPlistXML {
return [NSPropertyListSerialization dataWithPropertyList:obj
format:NSPropertyListXMLFormat_v1_0
options:0
error:nil];
}
- (NSData *)toDataKeyedArchiver {
return [NSKeyedArchiver archivedDataWithRootObject:obj];
}
- (NSData *)toDataJSON {
return [NSJSONSerialization dataWithJSONObject:obj
options:0
error:nil];
}
- (NSData *)toDataMessagePack {
return [MPMessagePackWriter writeObject:obj
error:nil];
}
- (void)testPerformancePListXML {
[self measureBlock:^{
for (int i=0; i < LOOPS; i++) {
[self toDataPlistXML];
}
}];
}
- (void)testPerformancePListBinary {
[self measureBlock:^{
for (int i=0; i < LOOPS; i++) {
[self toDataPlistBinary];
}
}];
}
- (void)testPerformanceKeyedArchiver {
[self measureBlock:^{
for (int i=0; i < LOOPS; i++) {
[self toDataKeyedArchiver];
}
}];
}
- (void)testPerformanceJSON {
[self measureBlock:^{
for (int i=0; i < LOOPS; i++) {
[self toDataJSON];
}
}];
}
- (void)testPerformanceMessagePack {
[self measureBlock:^{
for (int i=0; i < LOOPS; i++) {
[self toDataMessagePack];
}
}];
}
- (void)testSizes {
id results =
@{ @"PlistXML": [self toDataPlistXML],
@"PlistBinary": [self toDataPlistBinary],
@"KeyedArchiver": [self toDataKeyedArchiver],
@"JSON": [self toDataJSON],
@"MessagePack": [self toDataMessagePack] };
double maxlen = 0;
for (id key in results) {
maxlen = MAX(maxlen, [results[key] length]);
}
for (id key in results) {
id value = results[key];
NSLog(@"%@\t%@\t%.2f%%",
key,
@([value length]),
(100. * [value length] / maxlen));
value = [value gzippedData];
NSLog(@"%@ with GZip\t%@\t%.2f%%",
key,
@([value length]),
(100. * [value length] / maxlen));
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment