Skip to content
Permalink
Browse files

Benchmark WKWebView communication too.

  • Loading branch information
mihaip committed Aug 7, 2014
1 parent e67c6a6 commit cfce3ae9d68b76348bd88457b88943cb55570de5
@@ -1,5 +1,6 @@
#import "BenchmarkViewController.h"
#import "UIWebView+TS_JavaScriptContext.h"
#import <WebKit/WebKit.h>

#import <mach/mach_time.h>

@@ -12,6 +13,7 @@
XhrAsync,
CookieChange,
JavaScriptCore,
WKWebViewHandler,
kNumMechanisms
};

@@ -35,12 +37,13 @@ @interface PongUrlProtocol : NSURLProtocol

@end

@interface BenchmarkViewController () <TSWebViewDelegate, WebViewExport>
@interface BenchmarkViewController () <TSWebViewDelegate, WebViewExport, WKScriptMessageHandler>

@end

@implementation BenchmarkViewController {
UIWebView *_uiWebView;
WKWebView *_wkWebView;
UIButton *_benchmarkButton;
NSUInteger _iterationCounter;
UITextView *_results;
@@ -70,8 +73,17 @@ -(void)loadView {

_uiWebView = [[UIWebView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(_benchmarkButton.frame) + 10, width - 30, 56)];
[self.view addSubview:_uiWebView];
UIView *bottomWebView = _uiWebView;

if (WKWebView.class) {
WKWebViewConfiguration *configuration = [WKWebViewConfiguration new];
[configuration.userContentController addScriptMessageHandler:self name:@"pong"];
_wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(_uiWebView.frame) + 10, width - 30, 56) configuration:configuration];
[self.view addSubview:_wkWebView];
bottomWebView = _wkWebView;
}

_results = [[UITextView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(_uiWebView.frame) + 10, width - 30, self.view.bounds.size.height - CGRectGetMaxY(_uiWebView.frame) - 10) textContainer:nil];
_results = [[UITextView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(bottomWebView.frame) + 10, width - 30, self.view.bounds.size.height - CGRectGetMaxY(bottomWebView.frame) - 10) textContainer:nil];
_results.font = [UIFont fontWithName:@"Courier" size:12];
_results.editable = NO;
[self.view addSubview:_results];
@@ -111,6 +123,12 @@ -(void)viewDidLoad {
NSString *benchmarkPath = [NSBundle.mainBundle pathForResource:@"benchmark-uiwebview" ofType:@"html"];
NSURL *benchmarkUrl = [NSURL fileURLWithPath:benchmarkPath];
[_uiWebView loadRequest:[NSURLRequest requestWithURL:benchmarkUrl]];

if (_wkWebView) {
NSString *benchmarkPath = [NSBundle.mainBundle pathForResource:@"benchmark-wkwebview" ofType:@"html"];
NSURL *benchmarkUrl = [NSURL fileURLWithPath:benchmarkPath];
[_wkWebView loadRequest:[NSURLRequest requestWithURL:benchmarkUrl]];
}
}

-(void)runBenchmark:(UIButton *)button {
@@ -133,6 +151,12 @@ -(void)startIteration {
// Cookie changes don't seem to trigger delegate methods on iOS 8. Since it's a slower mechanism,
// it's not work investigating.
[self endIteration:0];
} else if (mechanism == WKWebViewHandler) {
if (_wkWebView) {
[_wkWebView evaluateJavaScript:[NSString stringWithFormat:@"ping(%d, '%qu')", mechanism, start] completionHandler:nil];
} else {
[self endIteration:0];
}
} else {
[_uiWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"ping(%d, '%qu')", mechanism, start]];
}
@@ -176,7 +200,8 @@ -(void)showBenchmarkResults {
case XhrSync: name = @"XHR sync "; break;
case XhrAsync: name = @"XHR async "; break;
case CookieChange: name = @"document.cookie "; break;
case JavaScriptCore: name = @"JavaScriptCore "; break;
case JavaScriptCore: name = @"JavaScriptCore "; break;
case WKWebViewHandler: name = @"WKWebViewHandler"; break;
}
MechanismTiming *timing = &_mechanismTimings[i];
double averageMs = [self machTimeToMs:timing->sum]/(double)kNumIterationsPerMechanisms;
@@ -203,11 +228,17 @@ -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)r
return YES;
}

- (void)webView:(UIWebView *)webView didCreateJavaScriptContext:(JSContext *)ctx {
-(void)webView:(UIWebView *)webView didCreateJavaScriptContext:(JSContext *)ctx {
_context = ctx;
ctx[@"viewController"] = self;
}

-(void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
NSNumber *start = (NSNumber *)message.body;
uint64_t end = mach_absolute_time();
[self endIteration:end - start.longLongValue];
}

-(void)handlePongRequest:(NSString *)data {
uint64_t start = data.longLongValue;
uint64_t end = mach_absolute_time();
@@ -2,6 +2,7 @@
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1 maximum-scale=1 user-scalable=no">
<title>Benchmark</title>
<link rel="stylesheet" href="benchmark.css">
</head>
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1 maximum-scale=1 user-scalable=no">
<title>Benchmark</title>
<link rel="stylesheet" href="benchmark.css">
</head>
<body>
<h3>WKWebView</h3>

<p>
Pings received: <span id="ping-count">0</span>.
</p>
<script src="benchmark-wkwebview.js"></script>
</body>
</html>
@@ -0,0 +1,20 @@
var Mechanism = {
WkWebViewHandler: 8
};

var pingCount = 0;
function ping(mechanism, startTime) {
pingCount++;
switch (mechanism) {
case Mechanism.WkWebViewHandler:
window.webkit.messageHandlers.pong.postMessage(startTime);
break;
}
}

// Set up a periodic timer to show that timers are not affected by any of the
// communication mechanisms.
var pingCountNode = document.getElementById("ping-count");
setInterval(function() {
pingCountNode.textContent = pingCount;
}, 1000);
@@ -16,6 +16,9 @@
91D3E181180BC37E001FA549 /* benchmark.css in Resources */ = {isa = PBXBuildFile; fileRef = 91D3E17E180BC37E001FA549 /* benchmark.css */; };
91D3E182180BC37E001FA549 /* benchmark-uiwebview.html in Resources */ = {isa = PBXBuildFile; fileRef = 91D3E17F180BC37E001FA549 /* benchmark-uiwebview.html */; };
91D3E183180BC37E001FA549 /* benchmark-uiwebview.js in Resources */ = {isa = PBXBuildFile; fileRef = 91D3E180180BC37E001FA549 /* benchmark-uiwebview.js */; };
91E272DA1993467F007056C1 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 91E272D91993467F007056C1 /* WebKit.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
91E272DD19934A0D007056C1 /* benchmark-wkwebview.html in Resources */ = {isa = PBXBuildFile; fileRef = 91E272DB19934A0D007056C1 /* benchmark-wkwebview.html */; };
91E272DE19934A0D007056C1 /* benchmark-wkwebview.js in Resources */ = {isa = PBXBuildFile; fileRef = 91E272DC19934A0D007056C1 /* benchmark-wkwebview.js */; };
DC73525F187B4B3200ABF917 /* UIWebView+TS_JavaScriptContext.m in Sources */ = {isa = PBXBuildFile; fileRef = DC73525E187B4B3200ABF917 /* UIWebView+TS_JavaScriptContext.m */; };
/* End PBXBuildFile section */

@@ -34,6 +37,9 @@
91D3E17E180BC37E001FA549 /* benchmark.css */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.css; path = benchmark.css; sourceTree = "<group>"; };
91D3E17F180BC37E001FA549 /* benchmark-uiwebview.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "benchmark-uiwebview.html"; sourceTree = "<group>"; };
91D3E180180BC37E001FA549 /* benchmark-uiwebview.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = "benchmark-uiwebview.js"; sourceTree = "<group>"; };
91E272D91993467F007056C1 /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; };
91E272DB19934A0D007056C1 /* benchmark-wkwebview.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "benchmark-wkwebview.html"; sourceTree = "<group>"; };
91E272DC19934A0D007056C1 /* benchmark-wkwebview.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = "benchmark-wkwebview.js"; sourceTree = "<group>"; };
DC73525D187B4B3200ABF917 /* UIWebView+TS_JavaScriptContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIWebView+TS_JavaScriptContext.h"; sourceTree = "<group>"; };
DC73525E187B4B3200ABF917 /* UIWebView+TS_JavaScriptContext.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIWebView+TS_JavaScriptContext.m"; sourceTree = "<group>"; };
/* End PBXFileReference section */
@@ -43,6 +49,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
91E272DA1993467F007056C1 /* WebKit.framework in Frameworks */,
911F221F1809D2EB00328F9A /* CoreGraphics.framework in Frameworks */,
911F22211809D2EB00328F9A /* UIKit.framework in Frameworks */,
911F221D1809D2EB00328F9A /* Foundation.framework in Frameworks */,
@@ -72,6 +79,7 @@
911F221B1809D2EB00328F9A /* Frameworks */ = {
isa = PBXGroup;
children = (
91E272D91993467F007056C1 /* WebKit.framework */,
911F221C1809D2EB00328F9A /* Foundation.framework */,
911F221E1809D2EB00328F9A /* CoreGraphics.framework */,
911F22201809D2EB00328F9A /* UIKit.framework */,
@@ -91,6 +99,8 @@
91D3E17E180BC37E001FA549 /* benchmark.css */,
91D3E17F180BC37E001FA549 /* benchmark-uiwebview.html */,
91D3E180180BC37E001FA549 /* benchmark-uiwebview.js */,
91E272DB19934A0D007056C1 /* benchmark-wkwebview.html */,
91E272DC19934A0D007056C1 /* benchmark-wkwebview.js */,
911F22231809D2EB00328F9A /* Supporting Files */,
);
path = Source;
@@ -157,8 +167,10 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
91E272DE19934A0D007056C1 /* benchmark-wkwebview.js in Resources */,
91D3E183180BC37E001FA549 /* benchmark-uiwebview.js in Resources */,
91D3E182180BC37E001FA549 /* benchmark-uiwebview.html in Resources */,
91E272DD19934A0D007056C1 /* benchmark-wkwebview.html in Resources */,
91D3E181180BC37E001FA549 /* benchmark.css in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;

0 comments on commit cfce3ae

Please sign in to comment.
You can’t perform that action at this time.