├── .gitignore ├── FastSharedPreferences ├── .gitignore ├── app │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── jeremy │ │ │ └── fspdemo │ │ │ └── FastSharedPreferencesAndroidTest.java │ │ └── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ └── com │ │ │ └── jeremy │ │ │ └── fspdemo │ │ │ ├── DemoApplication.java │ │ │ ├── InterProcessService.java │ │ │ ├── MainActivity.java │ │ │ ├── bean │ │ │ └── IPSData.java │ │ │ └── benchmark │ │ │ ├── BenchmarkActivity.java │ │ │ ├── BenchmarkController.java │ │ │ ├── BenchmarkManager.java │ │ │ ├── BenchmarkResult.java │ │ │ ├── IBenchmark.java │ │ │ ├── RunTimer.java │ │ │ └── SQLIteKV.java │ │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_benchmark.xml │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml ├── build.gradle ├── fastsharedpreferences │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── jeremy │ │ │ │ └── fastsharedpreferences │ │ │ │ ├── EnhancedEditor.java │ │ │ │ ├── EnhancedSharedPreferences.java │ │ │ │ ├── FastSharedPreferences.java │ │ │ │ └── io │ │ │ │ ├── IoUtil.java │ │ │ │ └── ReadWriteManager.java │ │ └── res │ │ │ └── values │ │ │ └── strings.xml │ │ └── test │ │ └── java │ │ └── com │ │ └── jeremy │ │ └── fastsharedpreferences │ │ ├── FastSharedPreferencesTest.java │ │ └── TestBean.java ├── gradle.properties ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # IntelliJ 36 | *.iml 37 | .idea/workspace.xml 38 | .idea/tasks.xml 39 | .idea/gradle.xml 40 | .idea/assetWizardSettings.xml 41 | .idea/dictionaries 42 | .idea/libraries 43 | .idea/caches 44 | 45 | # Keystore files 46 | # Uncomment the following line if you do not want to check your keystore files in. 47 | #*.jks 48 | 49 | # External native build folder generated in Android Studio 2.2 and later 50 | .externalNativeBuild 51 | 52 | # Google Services (e.g. APIs or Firebase) 53 | google-services.json 54 | 55 | # Freeline 56 | freeline.py 57 | freeline/ 58 | freeline_project_description.json 59 | 60 | # fastlane 61 | fastlane/report.xml 62 | fastlane/Preview.html 63 | fastlane/screenshots 64 | fastlane/test_output 65 | fastlane/readme.md 66 | 67 | release 68 | target 69 | build 70 | .settings 71 | .project 72 | .classpath 73 | .idea 74 | .DS_Store 75 | bin 76 | gen 77 | proguard 78 | .pmd 79 | *~ 80 | *.iml 81 | tmp 82 | gen-external-apklibs 83 | out 84 | tmp 85 | coverage 86 | build/ 87 | .gradle/ 88 | local.properties 89 | .gradletasknamecache 90 | src/main/provided_libs 91 | src/main/assets/*.dex 92 | src/main/assets/mtdata*.json 93 | reports/ 94 | aartojar/ 95 | keystore 96 | captures 97 | repo/ 98 | -------------------------------------------------------------------------------- /FastSharedPreferences/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches/build_file_checksums.ser 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | .DS_Store 9 | /build 10 | /captures 11 | .externalNativeBuild 12 | -------------------------------------------------------------------------------- /FastSharedPreferences/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /FastSharedPreferences/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | defaultConfig { 6 | applicationId "com.jeremy.fastsharedpreferences" 7 | minSdkVersion 16 8 | targetSdkVersion 27 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(include: ['*.jar'], dir: 'libs') 23 | implementation 'com.android.support:appcompat-v7:27.1.1' 24 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 28 | 29 | implementation group: 'com.tencent', name: 'mmkv', version: '1.0.12' 30 | implementation project(':fastsharedpreferences') 31 | } 32 | -------------------------------------------------------------------------------- /FastSharedPreferences/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /FastSharedPreferences/app/src/androidTest/java/com/jeremy/fspdemo/FastSharedPreferencesAndroidTest.java: -------------------------------------------------------------------------------- 1 | package com.jeremy.fspdemo; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.support.test.InstrumentationRegistry; 6 | import android.support.test.runner.AndroidJUnit4; 7 | import android.util.Log; 8 | 9 | import com.jeremy.fastsharedpreferences.FastSharedPreferences; 10 | import com.jeremy.fspdemo.bean.IPSData; 11 | 12 | import org.junit.Before; 13 | import org.junit.Ignore; 14 | import org.junit.Test; 15 | import org.junit.runner.RunWith; 16 | 17 | import static org.junit.Assert.assertEquals; 18 | import static org.junit.Assert.assertNotNull; 19 | import static org.junit.Assert.assertTrue; 20 | import static org.junit.Assert.fail; 21 | 22 | /** 23 | * Created by liaohailiang on 2018/10/24. 24 | */ 25 | @RunWith(AndroidJUnit4.class) 26 | public class FastSharedPreferencesAndroidTest { 27 | 28 | private Context context; 29 | 30 | @Before 31 | public void setup() { 32 | context = InstrumentationRegistry.getTargetContext(); 33 | FastSharedPreferences.init(context); 34 | } 35 | 36 | @Test 37 | public void testLoad() { 38 | FastSharedPreferences sharedPreferences = FastSharedPreferences.get("test_load"); 39 | assertNotNull(sharedPreferences); 40 | } 41 | 42 | @Test 43 | public void testWriteString() { 44 | FastSharedPreferences sharedPreferences = FastSharedPreferences.get("test_write_string"); 45 | sharedPreferences.edit().putString("test_key", "test_value").apply(); 46 | sleep(100); 47 | sharedPreferences = FastSharedPreferences.get("test_write_string"); 48 | assertEquals(sharedPreferences.getString("test_key", ""), "test_value"); 49 | } 50 | 51 | @Test 52 | public void testWriteInteger() { 53 | FastSharedPreferences sharedPreferences = FastSharedPreferences.get("test_write_integer"); 54 | sharedPreferences.edit().putInt("test_key", 100).apply(); 55 | sleep(100); 56 | sharedPreferences = FastSharedPreferences.get("test_write_integer"); 57 | assertEquals(sharedPreferences.getInt("test_key", -1), 100); 58 | } 59 | 60 | @Test 61 | public void testWriteLong() { 62 | FastSharedPreferences sharedPreferences = FastSharedPreferences.get("test_write_long"); 63 | sharedPreferences.edit().putLong("test_key", 100).apply(); 64 | sleep(100); 65 | sharedPreferences = FastSharedPreferences.get("test_write_long"); 66 | assertEquals(sharedPreferences.getLong("test_key", -1), 100); 67 | } 68 | 69 | @Test 70 | public void testWriteFloat() { 71 | FastSharedPreferences sharedPreferences = FastSharedPreferences.get("test_write_float"); 72 | sharedPreferences.edit().putFloat("test_key", 100.0f).apply(); 73 | sleep(100); 74 | sharedPreferences = FastSharedPreferences.get("test_write_float"); 75 | assertTrue(sharedPreferences.getFloat("test_key", -1) == 100.0f); 76 | } 77 | 78 | @Test 79 | public void testWriteBoolean() { 80 | FastSharedPreferences sharedPreferences = FastSharedPreferences.get("test_write_bool"); 81 | sharedPreferences.edit().putBoolean("test_key", true).apply(); 82 | sleep(100); 83 | sharedPreferences = FastSharedPreferences.get("test_write_bool"); 84 | assertEquals(sharedPreferences.getBoolean("test_key", false), true); 85 | } 86 | 87 | @Test 88 | public void testWriteIntegers() { 89 | FastSharedPreferences sharedPreferences = FastSharedPreferences.get("test_write_int"); 90 | for (int i = 0; i < 1000; i++) { 91 | sharedPreferences.edit().putInt("test_key_" + i, i).apply(); 92 | } 93 | sleep(200); 94 | sharedPreferences = FastSharedPreferences.get("test_write_int"); 95 | for (int i = 0; i < 1000; i++) { 96 | assertEquals(sharedPreferences.getInt("test_key_" + i, -1), i); 97 | } 98 | } 99 | 100 | @Test 101 | public void testWriteCount() { 102 | FastSharedPreferences sharedPreferences = FastSharedPreferences.get("test_write_count"); 103 | sharedPreferences.edit().clear().commit(); 104 | sleep(200); 105 | for (int i = 0; i < 10000; i++) { 106 | sharedPreferences.edit().putInt("test_key_" + i, i).apply(); 107 | } 108 | sleep(1500); 109 | sharedPreferences = FastSharedPreferences.get("test_write_count"); 110 | int size = sharedPreferences.getAll().size(); 111 | assertEquals(size, 10000); 112 | } 113 | 114 | @Test 115 | public void testRemove() { 116 | FastSharedPreferences sharedPreferences = FastSharedPreferences.get("test_write_re"); 117 | sharedPreferences.edit().putString("test_key", "test_value").apply(); 118 | sleep(100); 119 | sharedPreferences.edit().remove("test_key").apply(); 120 | sleep(100); 121 | sharedPreferences = FastSharedPreferences.get("test_write_re"); 122 | assertEquals(sharedPreferences.contains("test_key"), false); 123 | } 124 | 125 | @Test 126 | public void testClear() { 127 | FastSharedPreferences sharedPreferences = FastSharedPreferences.get("test_write_cl"); 128 | for (int i = 0; i < 1000; i++) { 129 | sharedPreferences.edit().putInt("test_key_" + i, i).apply(); 130 | } 131 | sleep(200); 132 | sharedPreferences.edit().clear().apply(); 133 | sleep(100); 134 | sharedPreferences = FastSharedPreferences.get("test_write_cl"); 135 | assertEquals(sharedPreferences.getAll().size(), 0); 136 | } 137 | 138 | @Test 139 | public void testInterProcessWriteInteger() { 140 | IPSData data = new IPSData("ip_test_write_integer", "test_key", 100); 141 | Intent intent = new Intent(context, InterProcessService.class); 142 | intent.putExtra(InterProcessService.EXTRA_KEY, data); 143 | context.startService(intent); 144 | sleep(500); 145 | FastSharedPreferences sharedPreferences = FastSharedPreferences.get("ip_test_write_integer"); 146 | assertEquals(sharedPreferences.getInt("test_key", -1), 100); 147 | } 148 | 149 | @Test 150 | public void testInterProcessWriteInteger1() { 151 | FastSharedPreferences sharedPreferences = FastSharedPreferences.get("ip_test_write_integer_1"); 152 | sharedPreferences.edit().putInt("test_key", 100).apply(); 153 | assertEquals(sharedPreferences.getInt("test_key", -1), 100); 154 | IPSData data = new IPSData("ip_test_write_integer_1", "test_key", 200); 155 | Intent intent = new Intent(context, InterProcessService.class); 156 | intent.putExtra(InterProcessService.EXTRA_KEY, data); 157 | context.startService(intent); 158 | sleep(500); 159 | assertEquals(sharedPreferences.getInt("test_key", -1), 200); 160 | } 161 | 162 | @Test 163 | public void testInterProcessWriteInteger2() { 164 | final String name = "ip_test_write_integer_2"; 165 | FastSharedPreferences sharedPreferences = FastSharedPreferences.get(name); 166 | Log.d("testInterProcessWriteInteger2", "size: " + sharedPreferences.getAll().size()); 167 | sharedPreferences.edit().clear().apply(); 168 | sleep(200); 169 | final int loop = 10; 170 | for (int i = 0; i < loop; i++) { 171 | sharedPreferences.edit().putInt("test_key_" + i, i).apply(); 172 | } 173 | sleep(1000); 174 | for (int i = 0; i < loop; i++) { 175 | int remoteIndex = loop + i; 176 | IPSData data = new IPSData(name, "test_key_" + remoteIndex, remoteIndex); 177 | Intent intent = new Intent(context, InterProcessService.class); 178 | intent.putExtra(InterProcessService.EXTRA_KEY, data); 179 | context.startService(intent); 180 | } 181 | sleep(1500); 182 | for (int i = 0; i < loop * 2; i++) { 183 | assertEquals(sharedPreferences.getInt("test_key_" + i, -1), i); 184 | } 185 | } 186 | 187 | private void sleep(long time) { 188 | try { 189 | Thread.sleep(time); 190 | } catch (Exception e) { 191 | } 192 | } 193 | } 194 | -------------------------------------------------------------------------------- /FastSharedPreferences/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /FastSharedPreferences/app/src/main/java/com/jeremy/fspdemo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package com.jeremy.fspdemo; 2 | 3 | import android.app.Application; 4 | 5 | import com.jeremy.fastsharedpreferences.FastSharedPreferences; 6 | import com.jeremy.fspdemo.benchmark.BenchmarkManager; 7 | 8 | /** 9 | * Created by liaohailiang on 2018/10/24. 10 | */ 11 | public class DemoApplication extends Application { 12 | 13 | @Override 14 | public void onCreate() { 15 | super.onCreate(); 16 | FastSharedPreferences.init(this); 17 | BenchmarkManager.getManager().init(this); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /FastSharedPreferences/app/src/main/java/com/jeremy/fspdemo/InterProcessService.java: -------------------------------------------------------------------------------- 1 | package com.jeremy.fspdemo; 2 | 3 | import android.app.Service; 4 | import android.content.Intent; 5 | import android.os.IBinder; 6 | import android.support.annotation.Nullable; 7 | 8 | import com.jeremy.fastsharedpreferences.FastSharedPreferences; 9 | import com.jeremy.fspdemo.bean.IPSData; 10 | 11 | /** 12 | * Created by liaohailiang on 2019/1/18. 13 | */ 14 | public class InterProcessService extends Service { 15 | 16 | public static final String EXTRA_KEY = "extra_key"; 17 | 18 | @Nullable 19 | @Override 20 | public IBinder onBind(Intent intent) { 21 | return null; 22 | } 23 | 24 | @Override 25 | public void onCreate() { 26 | super.onCreate(); 27 | } 28 | 29 | @Override 30 | public void onDestroy() { 31 | super.onDestroy(); 32 | } 33 | 34 | @Override 35 | public int onStartCommand(Intent intent, int flags, int startId) { 36 | IPSData data = (IPSData) intent.getSerializableExtra(EXTRA_KEY); 37 | if (data == null) { 38 | return super.onStartCommand(intent, flags, startId); 39 | } 40 | FastSharedPreferences sharedPreferences = FastSharedPreferences.get(data.name); 41 | if (data.value instanceof Integer) { 42 | sharedPreferences.edit().putInt(data.key, (int) data.value).apply(); 43 | } else if (data.value instanceof String) { 44 | sharedPreferences.edit().putString(data.key, (String) data.value).apply(); 45 | } else if (data.value instanceof Long) { 46 | sharedPreferences.edit().putLong(data.key, (long) data.value).apply(); 47 | } else if (data.value instanceof Float) { 48 | sharedPreferences.edit().putFloat(data.key, (float) data.value).apply(); 49 | } else if (data.value instanceof Boolean) { 50 | sharedPreferences.edit().putBoolean(data.key, (boolean) data.value).apply(); 51 | } 52 | return super.onStartCommand(intent, flags, startId); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /FastSharedPreferences/app/src/main/java/com/jeremy/fspdemo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.jeremy.fspdemo; 2 | 3 | import android.content.Intent; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.os.Bundle; 6 | import android.view.View; 7 | import android.widget.Toast; 8 | 9 | import com.jeremy.fastsharedpreferences.FastSharedPreferences; 10 | import com.jeremy.fspdemo.bean.IPSData; 11 | import com.jeremy.fspdemo.benchmark.BenchmarkActivity; 12 | 13 | public class MainActivity extends AppCompatActivity { 14 | 15 | private static final String FSP_ID = "test"; 16 | 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | setContentView(R.layout.activity_main); 21 | } 22 | 23 | public void toBenchmark(View v) { 24 | startActivity(new Intent(this, BenchmarkActivity.class)); 25 | } 26 | 27 | public void writeInt(View v) { 28 | FastSharedPreferences sharedPreferences = FastSharedPreferences.get(FSP_ID); 29 | sharedPreferences.edit().putInt("test_key", 100).apply(); 30 | Toast.makeText(this, "Write Int", Toast.LENGTH_SHORT).show(); 31 | } 32 | 33 | public void readInt(View v) { 34 | FastSharedPreferences sharedPreferences = FastSharedPreferences.get(FSP_ID); 35 | int ret = sharedPreferences.getInt("test_key", -1); 36 | Toast.makeText(this, "Read Int: " + ret, Toast.LENGTH_SHORT).show(); 37 | } 38 | 39 | public void interProcessWrite(View v) { 40 | IPSData data = new IPSData(FSP_ID, "test_key", 200); 41 | Intent intent = new Intent(this, InterProcessService.class); 42 | intent.putExtra(InterProcessService.EXTRA_KEY, data); 43 | startService(intent); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /FastSharedPreferences/app/src/main/java/com/jeremy/fspdemo/bean/IPSData.java: -------------------------------------------------------------------------------- 1 | package com.jeremy.fspdemo.bean; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Created by liaohailiang on 2019/1/18. 7 | */ 8 | public class IPSData implements Serializable { 9 | public String name; 10 | public String key; 11 | public Object value; 12 | 13 | public IPSData(String name, String key, Object value) { 14 | this.name = name; 15 | this.key = key; 16 | this.value = value; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /FastSharedPreferences/app/src/main/java/com/jeremy/fspdemo/benchmark/BenchmarkActivity.java: -------------------------------------------------------------------------------- 1 | package com.jeremy.fspdemo.benchmark; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.util.Log; 6 | import android.view.View; 7 | import android.widget.Toast; 8 | 9 | import com.jeremy.fspdemo.R; 10 | 11 | import java.util.List; 12 | 13 | public class BenchmarkActivity extends AppCompatActivity { 14 | 15 | private static final String TAG = "BENCHMARK"; 16 | 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | setContentView(R.layout.activity_benchmark); 21 | } 22 | 23 | public void batchWriteInt(View v) { 24 | Toast.makeText(this, "Start benchmark", Toast.LENGTH_SHORT).show(); 25 | new Thread(new Runnable() { 26 | @Override 27 | public void run() { 28 | List results = BenchmarkManager.getManager().benchmarkWriteInt(); 29 | final String result = results.toString(); 30 | Log.d(TAG, result); 31 | runOnUiThread(new Runnable() { 32 | @Override 33 | public void run() { 34 | Toast.makeText(BenchmarkActivity.this, result, Toast.LENGTH_LONG).show(); 35 | } 36 | }); 37 | } 38 | }).start(); 39 | } 40 | 41 | public void batchReadInt(View v) { 42 | Toast.makeText(this, "Start benchmark", Toast.LENGTH_SHORT).show(); 43 | new Thread(new Runnable() { 44 | @Override 45 | public void run() { 46 | List results = BenchmarkManager.getManager().benchmarkReadInt(); 47 | final String result = results.toString(); 48 | Log.d(TAG, result); 49 | runOnUiThread(new Runnable() { 50 | @Override 51 | public void run() { 52 | Toast.makeText(BenchmarkActivity.this, result, Toast.LENGTH_LONG).show(); 53 | } 54 | }); 55 | } 56 | }).start(); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /FastSharedPreferences/app/src/main/java/com/jeremy/fspdemo/benchmark/BenchmarkController.java: -------------------------------------------------------------------------------- 1 | package com.jeremy.fspdemo.benchmark; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Created by liaohailiang on 2018/11/8. 8 | */ 9 | public class BenchmarkController { 10 | 11 | private List results = new ArrayList<>(); 12 | 13 | public double runBenchmark(int count, IBenchmark benchmark) { 14 | results.clear(); 15 | for (int i = 0; i < count; i++) { 16 | results.add(benchmark.benchmark()); 17 | } 18 | return calculateAverage(results); 19 | } 20 | 21 | private double calculateAverage(List marks) { 22 | Long sum = 0L; 23 | if (!marks.isEmpty()) { 24 | for (Long mark : marks) { 25 | sum += mark; 26 | } 27 | return sum.doubleValue() / marks.size(); 28 | } 29 | return sum; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /FastSharedPreferences/app/src/main/java/com/jeremy/fspdemo/benchmark/BenchmarkManager.java: -------------------------------------------------------------------------------- 1 | package com.jeremy.fspdemo.benchmark; 2 | 3 | import android.content.Context; 4 | import android.content.SharedPreferences; 5 | 6 | import com.jeremy.fastsharedpreferences.FastSharedPreferences; 7 | import com.tencent.mmkv.MMKV; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | import java.util.Random; 12 | 13 | /** 14 | * Created by liaohailiang on 2018/11/8. 15 | */ 16 | public final class BenchmarkManager { 17 | 18 | private static final int LOOPS = 1000; 19 | private static final String SP_ID = "benchmark_sp"; 20 | private static final String FSP_ID = "benchmark_fsp"; 21 | private static final String MMKV_ID = "benchmark_mmkv"; 22 | 23 | private static final String BATCH_WRITE_INT = "BatchWriteInt"; 24 | private static final String BATCH_READ_INT = "BatchReadInt"; 25 | 26 | private static class SingletonHolder { 27 | private static final BenchmarkManager SINGLETON = new BenchmarkManager(); 28 | } 29 | 30 | public static BenchmarkManager getManager() { 31 | return SingletonHolder.SINGLETON; 32 | } 33 | 34 | private Context context; 35 | 36 | private BenchmarkManager() { 37 | } 38 | 39 | public void init(Context context) { 40 | this.context = context.getApplicationContext(); 41 | MMKV.initialize(this.context); 42 | } 43 | 44 | public List benchmarkWriteInt() { 45 | BenchmarkController controller = new BenchmarkController(); 46 | List results = new ArrayList<>(); 47 | double benchmark = 0; 48 | benchmark = controller.runBenchmark(5, new IBenchmark() { 49 | @Override 50 | public long benchmark() { 51 | return spBatchWriteInt(); 52 | } 53 | }); 54 | results.add(new BenchmarkResult("SharedPreferences", BATCH_WRITE_INT, benchmark)); 55 | benchmark = controller.runBenchmark(5, new IBenchmark() { 56 | @Override 57 | public long benchmark() { 58 | return sqliteWriteInt(); 59 | } 60 | }); 61 | results.add(new BenchmarkResult("SQLite", BATCH_WRITE_INT, benchmark)); 62 | benchmark = controller.runBenchmark(200, new IBenchmark() { 63 | @Override 64 | public long benchmark() { 65 | return mmkvBatchWriteInt(); 66 | } 67 | }); 68 | results.add(new BenchmarkResult("MMKV", BATCH_WRITE_INT, benchmark)); 69 | benchmark = controller.runBenchmark(200, new IBenchmark() { 70 | @Override 71 | public long benchmark() { 72 | return fspBatchWriteInt(); 73 | } 74 | }); 75 | results.add(new BenchmarkResult("FastSharedPreferences", BATCH_WRITE_INT, benchmark)); 76 | return results; 77 | } 78 | 79 | public List benchmarkReadInt() { 80 | BenchmarkController controller = new BenchmarkController(); 81 | List results = new ArrayList<>(); 82 | double benchmark = 0; 83 | benchmark = controller.runBenchmark(5, new IBenchmark() { 84 | @Override 85 | public long benchmark() { 86 | return spBatchReadInt(); 87 | } 88 | }); 89 | results.add(new BenchmarkResult("SharedPreferences", BATCH_READ_INT, benchmark)); 90 | benchmark = controller.runBenchmark(5, new IBenchmark() { 91 | @Override 92 | public long benchmark() { 93 | return sqliteBatchReadInt(); 94 | } 95 | }); 96 | results.add(new BenchmarkResult("SQLite", BATCH_READ_INT, benchmark)); 97 | benchmark = controller.runBenchmark(200, new IBenchmark() { 98 | @Override 99 | public long benchmark() { 100 | return mmkvBatchReadInt(); 101 | } 102 | }); 103 | results.add(new BenchmarkResult("MMKV", BATCH_READ_INT, benchmark)); 104 | benchmark = controller.runBenchmark(200, new IBenchmark() { 105 | @Override 106 | public long benchmark() { 107 | return fspBatchReadInt(); 108 | } 109 | }); 110 | results.add(new BenchmarkResult("FastSharedPreferences", BATCH_READ_INT, benchmark)); 111 | return results; 112 | } 113 | 114 | private long spBatchWriteInt() { 115 | RunTimer.start(); 116 | Random rand = new Random(); 117 | SharedPreferences sharedPreferences = context.getSharedPreferences(SP_ID, 0); 118 | SharedPreferences.Editor editor = sharedPreferences.edit(); 119 | for (int i = 0; i < LOOPS; i++) { 120 | String key = i + ""; 121 | int value = rand.nextInt(); 122 | editor.putInt(key, value); 123 | editor.apply(); 124 | } 125 | long cost = RunTimer.end(); 126 | return cost; 127 | } 128 | 129 | private long fspBatchWriteInt() { 130 | RunTimer.start(); 131 | Random rand = new Random(); 132 | FastSharedPreferences sharedPreferences = FastSharedPreferences.get(FSP_ID); 133 | SharedPreferences.Editor editor = sharedPreferences.edit(); 134 | for (int i = 0; i < LOOPS; i++) { 135 | String key = i + ""; 136 | int value = rand.nextInt(); 137 | editor.putInt(key, value); 138 | editor.apply(); 139 | } 140 | long cost = RunTimer.end(); 141 | return cost; 142 | } 143 | 144 | private long sqliteWriteInt() { 145 | RunTimer.start(); 146 | Random rand = new Random(); 147 | SQLIteKV sqlIteKV = new SQLIteKV(context); 148 | for (int i = 0; i < LOOPS; i++) { 149 | String key = i + ""; 150 | int value = rand.nextInt(); 151 | sqlIteKV.putInt(key, value); 152 | } 153 | long cost = RunTimer.end(); 154 | return cost; 155 | } 156 | 157 | private long mmkvBatchWriteInt() { 158 | RunTimer.start(); 159 | Random rand = new Random(); 160 | MMKV mmkv = MMKV.mmkvWithID(MMKV_ID, MMKV.SINGLE_PROCESS_MODE, null); 161 | for (int i = 0; i < LOOPS; i++) { 162 | String key = i + ""; 163 | int value = rand.nextInt(); 164 | mmkv.encode(key, value); 165 | } 166 | long cost = RunTimer.end(); 167 | return cost; 168 | } 169 | 170 | private long spBatchReadInt() { 171 | RunTimer.start(); 172 | SharedPreferences sharedPreferences = context.getSharedPreferences(SP_ID, 0); 173 | for (int i = 0; i < LOOPS; i++) { 174 | String key = i + ""; 175 | int tmp = sharedPreferences.getInt(key, -1); 176 | } 177 | long cost = RunTimer.end(); 178 | return cost; 179 | } 180 | 181 | private long fspBatchReadInt() { 182 | RunTimer.start(); 183 | FastSharedPreferences sharedPreferences = FastSharedPreferences.get(FSP_ID); 184 | for (int i = 0; i < LOOPS; i++) { 185 | String key = i + ""; 186 | int tmp = sharedPreferences.getInt(key, -1); 187 | } 188 | long cost = RunTimer.end(); 189 | return cost; 190 | } 191 | 192 | private long sqliteBatchReadInt() { 193 | RunTimer.start(); 194 | SQLIteKV sqlIteKV = new SQLIteKV(context); 195 | for (int i = 0; i < LOOPS; i++) { 196 | String key = i + ""; 197 | int tmp = sqlIteKV.getInt(key); 198 | } 199 | long cost = RunTimer.end(); 200 | return cost; 201 | } 202 | 203 | private long mmkvBatchReadInt() { 204 | RunTimer.start(); 205 | MMKV mmkv = MMKV.mmkvWithID(MMKV_ID, MMKV.SINGLE_PROCESS_MODE, null); 206 | for (int i = 0; i < LOOPS; i++) { 207 | String key = i + ""; 208 | int tmp = mmkv.getInt(key, -1); 209 | } 210 | long cost = RunTimer.end(); 211 | return cost; 212 | } 213 | } 214 | -------------------------------------------------------------------------------- /FastSharedPreferences/app/src/main/java/com/jeremy/fspdemo/benchmark/BenchmarkResult.java: -------------------------------------------------------------------------------- 1 | package com.jeremy.fspdemo.benchmark; 2 | 3 | /** 4 | * Created by liaohailiang on 2018/11/8. 5 | */ 6 | public class BenchmarkResult { 7 | 8 | private String method; 9 | private String category; 10 | private double result; 11 | 12 | public BenchmarkResult(String method, String category, double result) { 13 | this.method = method; 14 | this.category = category; 15 | this.result = result; 16 | } 17 | 18 | public String getMethod() { 19 | return method; 20 | } 21 | 22 | public void setMethod(String method) { 23 | this.method = method; 24 | } 25 | 26 | public String getCategory() { 27 | return category; 28 | } 29 | 30 | public void setCategory(String category) { 31 | this.category = category; 32 | } 33 | 34 | public double getResult() { 35 | return result; 36 | } 37 | 38 | public void setResult(long result) { 39 | this.result = result; 40 | } 41 | 42 | @Override 43 | public String toString() { 44 | StringBuilder sb = new StringBuilder(); 45 | sb.append("method: ").append(method).append(" | ") 46 | .append("category: ").append(category).append(" | ") 47 | .append("result: ").append(result); 48 | return sb.toString(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /FastSharedPreferences/app/src/main/java/com/jeremy/fspdemo/benchmark/IBenchmark.java: -------------------------------------------------------------------------------- 1 | package com.jeremy.fspdemo.benchmark; 2 | 3 | /** 4 | * Created by liaohailiang on 2018/11/8. 5 | */ 6 | public interface IBenchmark { 7 | 8 | long benchmark(); 9 | } 10 | -------------------------------------------------------------------------------- /FastSharedPreferences/app/src/main/java/com/jeremy/fspdemo/benchmark/RunTimer.java: -------------------------------------------------------------------------------- 1 | package com.jeremy.fspdemo.benchmark; 2 | 3 | /** 4 | * Created by liaohailiang on 2018/10/23. 5 | */ 6 | public class RunTimer { 7 | 8 | private static long startTime = -1; 9 | 10 | public static void start() { 11 | startTime = System.currentTimeMillis(); 12 | } 13 | 14 | public static long end() { 15 | return System.currentTimeMillis() - startTime; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /FastSharedPreferences/app/src/main/java/com/jeremy/fspdemo/benchmark/SQLIteKV.java: -------------------------------------------------------------------------------- 1 | package com.jeremy.fspdemo.benchmark; 2 | 3 | import android.content.ContentValues; 4 | import android.content.Context; 5 | import android.database.Cursor; 6 | import android.database.sqlite.SQLiteDatabase; 7 | import android.database.sqlite.SQLiteOpenHelper; 8 | 9 | public final class SQLIteKV { 10 | private static class SQLIteKVDBHelper extends SQLiteOpenHelper { 11 | private static final int DB_VERSION = 1; 12 | private static final String DB_NAME = "kv.db"; 13 | public static final String TABLE_NAME_STR = "kv_str"; 14 | public static final String TABLE_NAME_INT = "kv_int"; 15 | 16 | public SQLIteKVDBHelper(Context context) { 17 | super(context, DB_NAME, null, DB_VERSION); 18 | } 19 | 20 | @Override 21 | public void onCreate(SQLiteDatabase sqLiteDatabase) { 22 | String sql = "create table if not exists " + TABLE_NAME_STR + 23 | " (k text UNIQUE on conflict replace, v text)"; 24 | sqLiteDatabase.execSQL(sql); 25 | sql = "create table if not exists " + TABLE_NAME_INT + 26 | " (k text UNIQUE on conflict replace, v integer)"; 27 | sqLiteDatabase.execSQL(sql); 28 | } 29 | 30 | @Override 31 | public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { 32 | String sql = "DROP TABLE IF EXISTS " + TABLE_NAME_STR; 33 | sqLiteDatabase.execSQL(sql); 34 | sql = "DROP TABLE IF EXISTS " + TABLE_NAME_INT; 35 | sqLiteDatabase.execSQL(sql); 36 | onCreate(sqLiteDatabase); 37 | } 38 | } 39 | private SQLIteKVDBHelper m_dbHelper; 40 | private SQLiteDatabase m_writetableDB; 41 | private SQLiteDatabase m_readableDB; 42 | 43 | public SQLIteKV(Context context) { 44 | m_dbHelper = new SQLIteKVDBHelper(context); 45 | m_dbHelper.setWriteAheadLoggingEnabled(true); 46 | } 47 | 48 | public void beginTransaction() { 49 | getWritetableDB().beginTransaction(); 50 | } 51 | 52 | public void endTransaction() { 53 | if (m_writetableDB != null) { 54 | try { 55 | m_writetableDB.setTransactionSuccessful(); 56 | } finally { 57 | m_writetableDB.endTransaction(); 58 | } 59 | } 60 | } 61 | 62 | private SQLiteDatabase getWritetableDB() { 63 | if (m_writetableDB == null) { 64 | m_writetableDB = m_dbHelper.getWritableDatabase(); 65 | } 66 | return m_writetableDB; 67 | } 68 | 69 | private SQLiteDatabase getReadableDatabase() { 70 | if (m_readableDB == null) { 71 | m_readableDB = m_dbHelper.getReadableDatabase(); 72 | } 73 | return m_readableDB; 74 | } 75 | 76 | @Override 77 | protected void finalize() throws Throwable { 78 | if (m_readableDB != null) { 79 | m_readableDB.close(); 80 | } 81 | if (m_writetableDB != null) { 82 | m_writetableDB.close(); 83 | } 84 | super.finalize(); 85 | } 86 | 87 | public boolean putInt(String key, int value) { 88 | ContentValues contentValues = new ContentValues(); 89 | contentValues.put("k", key); 90 | contentValues.put("v", value); 91 | long rowID = getWritetableDB().insert(SQLIteKVDBHelper.TABLE_NAME_INT, null, contentValues); 92 | return rowID != -1; 93 | } 94 | 95 | public int getInt(String key) { 96 | int value = 0; 97 | Cursor cursor = getReadableDatabase().rawQuery( 98 | "select v from " + SQLIteKVDBHelper.TABLE_NAME_INT + " where k=?", new String[] {key}); 99 | if (cursor.moveToFirst()) { 100 | value = cursor.getInt(cursor.getColumnIndex("v")); 101 | } 102 | cursor.close(); 103 | return value; 104 | } 105 | 106 | public boolean putString(String key, String value) { 107 | ContentValues contentValues = new ContentValues(); 108 | contentValues.put("k", key); 109 | contentValues.put("v", value); 110 | long rowID = getWritetableDB().insert(SQLIteKVDBHelper.TABLE_NAME_STR, null, contentValues); 111 | return rowID != -1; 112 | } 113 | 114 | public String getString(String key) { 115 | String value = null; 116 | Cursor cursor = getReadableDatabase().rawQuery( 117 | "select v from " + SQLIteKVDBHelper.TABLE_NAME_STR + " where k=?", new String[] {key}); 118 | if (cursor.moveToFirst()) { 119 | value = cursor.getString(cursor.getColumnIndex("v")); 120 | } 121 | cursor.close(); 122 | return value; 123 | } 124 | } -------------------------------------------------------------------------------- /FastSharedPreferences/app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /FastSharedPreferences/app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /FastSharedPreferences/app/src/main/res/layout/activity_benchmark.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 |