Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.