Created
January 7, 2014 10:20
-
-
Save takke/8297439 to your computer and use it in GitHub Desktop.
ShowStatusをもとに作ったSPDY動作検証&ベンチマーク用のサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2007 Yusuke Yamamoto | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package twitter4j.examples.tweets; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.lang.reflect.Field; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.security.GeneralSecurityException; | |
import java.util.List; | |
import java.util.Map; | |
import javax.net.ssl.HostnameVerifier; | |
import javax.net.ssl.SSLContext; | |
import javax.net.ssl.SSLSession; | |
import twitter4j.RateLimitStatus; | |
import twitter4j.Status; | |
import twitter4j.Twitter; | |
import twitter4j.TwitterException; | |
import twitter4j.TwitterFactory; | |
import twitter4j.internal.http.HttpClientWrapper; | |
import com.squareup.okhttp.ConnectionPool; | |
import com.squareup.okhttp.OkHttpClient; | |
/** | |
* Shows one single status. | |
* | |
* @author Yusuke Yamamoto - yusuke at mac.com | |
*/ | |
public final class ShowStatusBenchmark { | |
// private static final String ENDPOINT = "https://www.twitter.com"; | |
private static final String ENDPOINT = "https://www.google.co.jp"; | |
// private static final String ENDPOINT = "https://www.github.com"; | |
/** | |
* Usage: java twitter4j.examples.tweets.ShowStatus [status id] | |
* | |
* @param args message | |
*/ | |
public static void main(String[] args) { | |
if (args.length < 1) { | |
System.out.println("Usage: java twitter4j.examples.tweets.ShowStatusBenchmark [status id]"); | |
System.exit(-1); | |
} | |
System.out.println("java: " + System.getProperty("java.runtime.version") + " " | |
+ System.getProperty("java.version") + " " + System.getProperty("java.vm.version")); | |
doSimpleOkhttpRequest(); | |
// doShowStatusBenchmark(args); | |
} | |
private static void doSimpleOkhttpRequest() throws AssertionError { | |
OkHttpClient client = new OkHttpClient(); | |
client.setConnectionPool(new ConnectionPool(5, 5*60)); | |
// Ignore invalid SSL endpoints. | |
client.setHostnameVerifier(new HostnameVerifier() { | |
@Override | |
public boolean verify(String s, SSLSession sslSession) { | |
return true; | |
} | |
}); | |
// to avoid the crash default to using a single global SSL context. | |
// see https://github.com/square/okhttp/issues/184#issuecomment-18772733 | |
SSLContext sslContext; | |
try { | |
sslContext = SSLContext.getInstance("TLS"); | |
sslContext.init(null, null, null); | |
} catch (GeneralSecurityException e) { | |
throw new AssertionError(); // The system has no TLS. Just give up. | |
} | |
client.setSslSocketFactory(sslContext.getSocketFactory()); | |
// NPNスキップ設定 | |
// try { | |
// client.setSpdyAddress(new URL(ENDPOINT)); | |
// } catch (MalformedURLException e1) { | |
// } | |
try { | |
// Create request for remote resource. | |
HttpURLConnection connection = client.open(new URL(ENDPOINT)); | |
// System.out.println("---request---"); | |
// Map<String, List<String>> props = connection.getRequestProperties(); | |
// for (String key : props.keySet()) { | |
// System.out.println(key + ":" + props.get(key)); | |
// } | |
InputStream is = connection.getInputStream(); | |
// Deserialize HTTP response to concrete type. | |
String result; | |
BufferedReader br = null; | |
InputStream stream = is; | |
try { | |
br = new BufferedReader(new InputStreamReader(stream, "UTF-8")); | |
StringBuilder buf = new StringBuilder(); | |
String line; | |
while ((line = br.readLine()) != null) { | |
buf.append(line).append("\n"); | |
} | |
result = buf.toString(); | |
stream.close(); | |
} catch (IOException ioe) { | |
throw new TwitterException(ioe.getMessage(), ioe); | |
} finally { | |
if (stream != null) { | |
try { | |
stream.close(); | |
} catch (IOException ignore) { | |
} | |
} | |
if (br != null) { | |
try { | |
br.close(); | |
} catch (IOException ignore) { | |
} | |
} | |
} | |
System.out.println(result); | |
System.out.println("---headers---"); | |
Map<String, List<String>> headers = connection.getHeaderFields(); | |
for (String key : headers.keySet()) { | |
System.out.println(key + ":" + headers.get(key)); | |
} | |
ConnectionPool p = client.getConnectionPool(); | |
if (p == null) { | |
System.out.println("SPDY : Disabled"); | |
} else { | |
System.out.println("SPDY : [" + p.getSpdyConnectionCount() + "/" + p.getConnectionCount() + "]"); | |
} | |
System.exit(0); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private static void doShowStatusBenchmark(String[] args) { | |
final Twitter twitter = new TwitterFactory().getInstance(); | |
System.out.println("start"); | |
twitter4j.internal.http.alternative.HttpClientImpl.sPreferSpdy = true; | |
final long statusId = Long.parseLong(args[0]); | |
// kick parallel (for benchmark) | |
class Box { | |
long total = 0; | |
int done = 0; | |
} | |
final Box box = new Box(); | |
final int count = 2; | |
for (int i=0; i<count; i++) { | |
// System.out.println("kick[" + i + "]"); | |
new Thread(new Runnable() { | |
@Override | |
public void run() { | |
final long startTick = System.currentTimeMillis(); | |
try { | |
final Status status = twitter.showStatus(statusId); | |
final long elapsed = System.currentTimeMillis() - startTick; | |
box.total += elapsed; | |
System.out.println(" " + elapsed + "\tms"); | |
box.done ++; | |
if (box.done == count) { | |
System.out.println("average:[" + box.total/count + "ms]"); | |
RateLimitStatus r = status.getRateLimitStatus(); | |
System.out.println("rate limit:[" + r.getRemaining() + "/" + r.getLimit() + "], [" + r.getSecondsUntilReset() + "sec]"); | |
final twitter4j.internal.http.alternative.HttpClientImpl http = getHttpClientImpl(twitter); | |
ConnectionPool p = getSpdyConnectionPool(http); | |
if (p == null) { | |
System.out.println("SPDY : Disabled"); | |
} else { | |
System.out.println("SPDY : [" + p.getSpdyConnectionCount() + "/" + p.getConnectionCount() + "]"); | |
System.out.println("[" + http.getLastRequestTransport() + "]"); | |
} | |
System.exit(0); | |
} | |
} catch (Exception te) { | |
te.printStackTrace(); | |
System.out.println("Failed to show status: " + te.getMessage()); | |
System.exit(-1); | |
} | |
} | |
}).run(); | |
try { | |
Thread.sleep(100); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
public static ConnectionPool getSpdyConnectionPool(final Twitter twitter) { | |
try { | |
final twitter4j.internal.http.alternative.HttpClientImpl http = getHttpClientImpl(twitter); | |
return getSpdyConnectionPool(http); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
public static ConnectionPool getSpdyConnectionPool(final twitter4j.internal.http.alternative.HttpClientImpl http) { | |
try { | |
final Field f3 = http.getClass().getDeclaredField("client"); | |
f3.setAccessible(true); | |
// client = http.client | |
final OkHttpClient client = (OkHttpClient) f3.get(http); | |
if (client != null) { | |
final ConnectionPool p = client.getConnectionPool(); | |
return p; | |
} else { | |
return null; | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
public static twitter4j.internal.http.alternative.HttpClientImpl getHttpClientImpl( | |
final Twitter twitter) throws ClassNotFoundException, | |
NoSuchFieldException, IllegalAccessException { | |
final Class<?> clazz = Class.forName("twitter4j.TwitterBaseImpl"); | |
final Field f1 = clazz.getDeclaredField("http"); | |
f1.setAccessible(true); | |
// wrapper = twitter.http | |
final HttpClientWrapper wrapper = (HttpClientWrapper) f1.get(twitter); | |
final Field f2 = HttpClientWrapper.class.getDeclaredField("http"); | |
f2.setAccessible(true); | |
// http = wrapper.http | |
return (twitter4j.internal.http.alternative.HttpClientImpl) f2.get(wrapper); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment