diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..855e33a --- /dev/null +++ b/.gitignore @@ -0,0 +1,21 @@ +gen +bin +target +.idea +.settings +*.iml +.classpath +.project +out +classes +gen-external-apklibs +*.properties +.DS_Store +.gradle +build/ +*.apk +*.hprof +gradle/ +gradlew +gradlew.bat +infer-out/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..9c6ec5a --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# QUIC + +QUIC为Google于2013年开发的基于UDP的多路并发传输协议,主要优势在于减少TCP三次握手及TLS握手,同时因为UDP特性在高丢包环境下依旧能正常工作。在使用情景复杂的移动网络环境下能有效降低延时以及提高请求成功率。(https://www.chromium.org/quic) + +## Bench + +此处选择Android作为测试平台,对同时提供QUIC和HTTP2.0支持的腾讯云CDN图片进行下载测试。 + +**Server(CDN)** + +```Kotlin + "https://stgwhttp2.kof.qq.com/1.jpg", + "https://stgwhttp2.kof.qq.com/2.jpg", + "https://stgwhttp2.kof.qq.com/3.jpg", + "https://stgwhttp2.kof.qq.com/4.jpg", + "https://stgwhttp2.kof.qq.com/5.jpg", + "https://stgwhttp2.kof.qq.com/6.jpg", + "https://stgwhttp2.kof.qq.com/7.jpg", + "https://stgwhttp2.kof.qq.com/8.jpg", + "https://stgwhttp2.kof.qq.com/01.jpg", + "https://stgwhttp2.kof.qq.com/02.jpg", + "https://stgwhttp2.kof.qq.com/03.jpg", + "https://stgwhttp2.kof.qq.com/04.jpg", + "https://stgwhttp2.kof.qq.com/05.jpg", + "https://stgwhttp2.kof.qq.com/06.jpg", + "https://stgwhttp2.kof.qq.com/07.jpg", + "https://stgwhttp2.kof.qq.com/08.jpg" +``` + +**Client:Android** +Android平台上,我们使用从`Chromium`中抽取的[cornet](https://chromium.googlesource.com/chromium/src/+/master/components/cronet?autodive=0%2F%2F)作为QUIC Client,对比`OKHttp`作为Http2.0 Client。为了避免OKHttp本身优化的问题,我们为QUIC提供了hook OKHttp用的`Interceptor`,此数数据均为QUIC over OKHttp的测试结果。直接使用cornet engine的测试结果大家可以自行运行。 + +### 丢包测试 + +1. 模拟正常3G环境 上下行相同 Delay 0ms,Rate 750k + +![bench_3g](./doc/bench_3g.png) + + + diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..a6d6fe1 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,52 @@ +apply plugin: 'com.android.application' + +apply plugin: 'kotlin-android' + +apply plugin: 'kotlin-android-extensions' + +android { + compileSdkVersion 27 + defaultConfig { + applicationId "com.smilehacker.quictest" + minSdkVersion 15 + targetSdkVersion 27 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + + ndk { + abiFilters 'armeabi-v7a' + } + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } + + compileOptions { + sourceCompatibility 1.8 + targetCompatibility 1.8 + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" + implementation 'com.android.support:appcompat-v7:27.1.1' + implementation 'com.android.support.constraint:constraint-layout:1.0.2' + testImplementation 'junit:junit:4.12' + androidTestImplementation 'com.android.support.test:runner:1.0.1' + androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' + + implementation files('libs/cronet_api.jar') + implementation files('libs/cronet_impl_common_java.jar') + implementation files('libs/cronet_impl_native_java.jar') + + implementation 'com.squareup.okio:okio:1.14.0' + implementation 'com.squareup.okhttp3:okhttp:3.10.0' + +// implementation 'com.packetzoom:pz-android-sdk:3.+' +// implementation 'com.packetzoom:pz-okhttp3-interceptor:3.2.+' +} diff --git a/app/libs/cronet_api.jar b/app/libs/cronet_api.jar new file mode 100644 index 0000000..0ade94d Binary files /dev/null and b/app/libs/cronet_api.jar differ diff --git a/app/libs/cronet_impl_common_java.jar b/app/libs/cronet_impl_common_java.jar new file mode 100644 index 0000000..d1a9da1 Binary files /dev/null and b/app/libs/cronet_impl_common_java.jar differ diff --git a/app/libs/cronet_impl_native_java.jar b/app/libs/cronet_impl_native_java.jar new file mode 100644 index 0000000..30a2d22 Binary files /dev/null and b/app/libs/cronet_impl_native_java.jar differ diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/app/src/androidTest/java/com/smilehacker/quictest/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/smilehacker/quictest/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..80eb627 --- /dev/null +++ b/app/src/androidTest/java/com/smilehacker/quictest/ExampleInstrumentedTest.kt @@ -0,0 +1,24 @@ +package com.smilehacker.quictest + +import android.support.test.InstrumentationRegistry +import android.support.test.runner.AndroidJUnit4 + +import org.junit.Test +import org.junit.runner.RunWith + +import org.junit.Assert.* + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getTargetContext() + assertEquals("com.smilehacker.quictest", appContext.packageName) + } +} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..9565789 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/smilehacker/quictest/App.java b/app/src/main/java/com/smilehacker/quictest/App.java new file mode 100644 index 0000000..4a72f4c --- /dev/null +++ b/app/src/main/java/com/smilehacker/quictest/App.java @@ -0,0 +1,27 @@ +package com.smilehacker.quictest; + +import android.app.Application; + + +/** + * Created by quan.zhou on 2017/8/5. + */ + +public class App { + public static Application INSTANCE ;//= AppGlobals.getInitialApplication(); + static { + Application app = null; + try { + app = (Application) Class.forName("android.app.AppGlobals").getMethod("getInitialApplication").invoke(null); + if (app == null) + throw new IllegalStateException("Static initialization of Applications must be on main thread."); + } catch (final Exception e) { + try { + app = (Application) Class.forName("android.app.ActivityThread").getMethod("currentApplication").invoke(null); + } catch (final Exception ex) { + } + } finally { + INSTANCE = app; + } + } +} diff --git a/app/src/main/java/com/smilehacker/quictest/MainActivity.kt b/app/src/main/java/com/smilehacker/quictest/MainActivity.kt new file mode 100644 index 0000000..1e19291 --- /dev/null +++ b/app/src/main/java/com/smilehacker/quictest/MainActivity.kt @@ -0,0 +1,174 @@ +package com.smilehacker.quictest + +import android.os.AsyncTask +import android.os.Bundle +import android.support.v7.app.AppCompatActivity +import android.util.Log +import android.widget.Button +import android.widget.TextView +import okhttp3.OkHttpClient +import okhttp3.Request +import okio.Okio +import org.chromium.net.CronetEngine +import java.io.File +import java.net.HttpURLConnection +import java.net.URL +import java.util.concurrent.TimeUnit + + +class MainActivity : AppCompatActivity() { + + private lateinit var mEngine: CronetEngine + private lateinit var mOkhttpClent: OkHttpClient + private lateinit var mOkhttpClentWithQUIC: OkHttpClient + + private val mTvResultHttp by lazy { findViewById(R.id.tv_http_result) } + private val mTvResultQuic by lazy { findViewById(R.id.tv_quic_result) } + private val mTvResultQuicOverOKHttp by lazy { findViewById(R.id.tv_quic_ok_result) } + + companion object { + val TAG = MainActivity::class.java.simpleName + + + const val TEST_TYPE_QUIC = 1 + const val TEST_TYPE_HTTP = 2 + const val TEST_TYPE_QUIC_OVER_OKHTTP = 3 + } + + private val IMGS = arrayOf( + "https://stgwhttp2.kof.qq.com/1.jpg", + "https://stgwhttp2.kof.qq.com/2.jpg", + "https://stgwhttp2.kof.qq.com/3.jpg", + "https://stgwhttp2.kof.qq.com/4.jpg", + "https://stgwhttp2.kof.qq.com/5.jpg", + "https://stgwhttp2.kof.qq.com/6.jpg", + "https://stgwhttp2.kof.qq.com/7.jpg", + "https://stgwhttp2.kof.qq.com/8.jpg", + "https://stgwhttp2.kof.qq.com/01.jpg", + "https://stgwhttp2.kof.qq.com/02.jpg", + "https://stgwhttp2.kof.qq.com/03.jpg", + "https://stgwhttp2.kof.qq.com/04.jpg", + "https://stgwhttp2.kof.qq.com/05.jpg", + "https://stgwhttp2.kof.qq.com/06.jpg", + "https://stgwhttp2.kof.qq.com/07.jpg", + "https://stgwhttp2.kof.qq.com/08.jpg" + ) + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + + mEngine = CronetEngine.Builder(applicationContext) + .enableQuic(true) + .build() + + mOkhttpClentWithQUIC = OkHttpClient.Builder() + .readTimeout(30, TimeUnit.SECONDS) + .writeTimeout(30, TimeUnit.SECONDS) + .connectTimeout(30, TimeUnit.SECONDS) + .cache(null) + .addInterceptor(QUICInterceptor()) + .build() + mOkhttpClent = OkHttpClient.Builder() + .readTimeout(30, TimeUnit.SECONDS) + .writeTimeout(30, TimeUnit.SECONDS) + .connectTimeout(30, TimeUnit.SECONDS) + .cache(null) + .build() + + findViewById