|
| 1 | +package com.aliyun.sls.android.producer.example.example.benchmark; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.util.Pair; |
| 5 | +import androidx.annotation.Nullable; |
| 6 | +import androidx.appcompat.app.AppCompatActivity; |
| 7 | +import com.aliyun.sls.android.producer.Log; |
| 8 | +import com.aliyun.sls.android.producer.LogProducerClient; |
| 9 | +import com.aliyun.sls.android.producer.LogProducerConfig; |
| 10 | +import com.aliyun.sls.android.producer.LogProducerException; |
| 11 | +import com.aliyun.sls.android.producer.R; |
| 12 | +import com.aliyun.sls.android.producer.example.example.producer.LogUtils; |
| 13 | +import com.aliyun.sls.android.producer.example.utils.PreferenceUtils; |
| 14 | + |
| 15 | +/** |
| 16 | + * @author yulong.gyl |
| 17 | + * @date 2023/8/23 |
| 18 | + */ |
| 19 | +@SuppressWarnings("ConstantConditions") |
| 20 | +public class BenchmarkActivity extends AppCompatActivity { |
| 21 | + private static final int BENCHMARK_DURATION = 1 * 60; |
| 22 | + |
| 23 | + @Override |
| 24 | + protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 25 | + super.onCreate(savedInstanceState); |
| 26 | + setContentView(R.layout.activity_benchmark); |
| 27 | + findViewById(R.id.benchmark_mem_write_1).setOnClickListener(v -> benchmarkMem1s()); |
| 28 | + findViewById(R.id.benchmark_mem_write_10).setOnClickListener(v -> benchmarkMem10s()); |
| 29 | + findViewById(R.id.benchmark_mem_write_50).setOnClickListener(v -> benchmarkMem50s()); |
| 30 | + findViewById(R.id.benchmark_mem_write_100).setOnClickListener(v -> benchmarkMem100s()); |
| 31 | + findViewById(R.id.benchmark_mem_write_200).setOnClickListener(v -> benchmarkMem200s()); |
| 32 | + findViewById(R.id.benchmark_file_write_1).setOnClickListener(v -> benchmarkFile1s()); |
| 33 | + findViewById(R.id.benchmark_file_write_10).setOnClickListener(v -> benchmarkFile10s()); |
| 34 | + findViewById(R.id.benchmark_file_write_50).setOnClickListener(v -> benchmarkFile50s()); |
| 35 | + findViewById(R.id.benchmark_file_write_100).setOnClickListener(v -> benchmarkFile100s()); |
| 36 | + findViewById(R.id.benchmark_file_write_200).setOnClickListener(v -> benchmarkFile200s()); |
| 37 | + } |
| 38 | + |
| 39 | + private static void sleep(long start, long end, int wantSleep) { |
| 40 | + long diff = end - start; |
| 41 | + if (diff / 1000000 < wantSleep) { |
| 42 | + try { |
| 43 | + android.util.Log.d("sleep", wantSleep - diff / 1000000 + ", " + diff % 1000000); |
| 44 | + Thread.sleep(wantSleep - diff / 1000000, (int)(diff % 1000000)); |
| 45 | + } catch (InterruptedException e) { |
| 46 | + // ignore |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private static Log oneLog() { |
| 52 | + return LogUtils.createLog(); |
| 53 | + } |
| 54 | + |
| 55 | + private Pair<LogProducerConfig, LogProducerClient> initLogProducer() { |
| 56 | + LogProducerConfig config; |
| 57 | + LogProducerClient client; |
| 58 | + try { |
| 59 | + // 使用默认参数初始化 |
| 60 | + config = new LogProducerConfig( |
| 61 | + PreferenceUtils.getEndpoint(this), |
| 62 | + PreferenceUtils.getLogProject(this), |
| 63 | + PreferenceUtils.getLogStore(this), |
| 64 | + PreferenceUtils.getAccessKeyId(this), |
| 65 | + PreferenceUtils.getAccessKeyToken(this) |
| 66 | + ); |
| 67 | + client = new LogProducerClient(config); |
| 68 | + } catch (LogProducerException e) { |
| 69 | + e.printStackTrace(); |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + return Pair.create(config, client); |
| 74 | + } |
| 75 | + |
| 76 | + private void benchmarkMem1s() { |
| 77 | + Pair<LogProducerConfig, LogProducerClient> pair = initLogProducer(); |
| 78 | + |
| 79 | + // 测试持续一分钟的性能信息 |
| 80 | + for (int i = 0; i < BENCHMARK_DURATION; i++) { |
| 81 | + long start = System.nanoTime(); |
| 82 | + boolean succ = pair.second.addLog(oneLog()).isLogProducerResultOk(); |
| 83 | + android.util.Log.d("benchmarkMem1s", "add log: " + succ); |
| 84 | + long end = System.nanoTime(); |
| 85 | + if (!succ) { |
| 86 | + throw new RuntimeException("add log to producer error."); |
| 87 | + } |
| 88 | + |
| 89 | + sleep(start, end, 1000); |
| 90 | + } |
| 91 | + |
| 92 | + pair.second.destroyLogProducer(); |
| 93 | + } |
| 94 | + |
| 95 | + private void benchmarkMem10s() { |
| 96 | + Pair<LogProducerConfig, LogProducerClient> pair = initLogProducer(); |
| 97 | + |
| 98 | + pair.second.destroyLogProducer(); |
| 99 | + } |
| 100 | + |
| 101 | + private void benchmarkMem50s() { |
| 102 | + Pair<LogProducerConfig, LogProducerClient> pair = initLogProducer(); |
| 103 | + |
| 104 | + pair.second.destroyLogProducer(); |
| 105 | + } |
| 106 | + |
| 107 | + private void benchmarkMem100s() { |
| 108 | + Pair<LogProducerConfig, LogProducerClient> pair = initLogProducer(); |
| 109 | + |
| 110 | + pair.second.destroyLogProducer(); |
| 111 | + } |
| 112 | + |
| 113 | + private void benchmarkMem200s() { |
| 114 | + Pair<LogProducerConfig, LogProducerClient> pair = initLogProducer(); |
| 115 | + |
| 116 | + pair.second.destroyLogProducer(); |
| 117 | + } |
| 118 | + |
| 119 | + private void benchmarkFile1s() { |
| 120 | + Pair<LogProducerConfig, LogProducerClient> pair = initLogProducer(); |
| 121 | + |
| 122 | + pair.second.destroyLogProducer(); |
| 123 | + } |
| 124 | + |
| 125 | + private void benchmarkFile10s() { |
| 126 | + Pair<LogProducerConfig, LogProducerClient> pair = initLogProducer(); |
| 127 | + |
| 128 | + pair.second.destroyLogProducer(); |
| 129 | + } |
| 130 | + |
| 131 | + private void benchmarkFile50s() { |
| 132 | + Pair<LogProducerConfig, LogProducerClient> pair = initLogProducer(); |
| 133 | + |
| 134 | + pair.second.destroyLogProducer(); |
| 135 | + } |
| 136 | + |
| 137 | + private void benchmarkFile100s() { |
| 138 | + Pair<LogProducerConfig, LogProducerClient> pair = initLogProducer(); |
| 139 | + |
| 140 | + pair.second.destroyLogProducer(); |
| 141 | + } |
| 142 | + |
| 143 | + private void benchmarkFile200s() { |
| 144 | + Pair<LogProducerConfig, LogProducerClient> pair = initLogProducer(); |
| 145 | + |
| 146 | + pair.second.destroyLogProducer(); |
| 147 | + } |
| 148 | +} |
0 commit comments