Skip to content

Commit bac5f09

Browse files
author
gzbleu
committed
feat: add benchmark page
1 parent f4936ab commit bac5f09

File tree

8 files changed

+247
-200
lines changed

8 files changed

+247
-200
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
<activity android:name=".example.example.producer.ProducerWithDynamicConfig" />
3737
<activity android:name=".example.example.producer.ProducerWithMultiClients" />
3838
<activity android:name=".example.example.producer.ProducerWithDestroy" />
39-
<activity android:name=".example.example.producer.ProducerWebTracking" />
4039

4140
<activity android:name=".example.example.crash.CrashExampleActivity" />
4241
<activity android:name=".example.example.network.NetworkExample" />
@@ -48,6 +47,9 @@
4847

4948
<!-- instrumentation -->
5049
<activity android:name=".example.example.instrumentation.WebViewInstrumentationActivity" />
50+
51+
<!-- benchmark -->
52+
<activity android:name=".example.example.benchmark.BenchmarkActivity"/>
5153
</application>
5254

5355
</manifest>

app/src/main/java/com/aliyun/sls/android/producer/example/MainActivity.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
import com.aliyun.sls.android.producer.R;
1313
import com.aliyun.sls.android.producer.example.example.TraceDemoActivity;
1414
import com.aliyun.sls.android.producer.example.example.TraceDemoKotlinActivity;
15+
import com.aliyun.sls.android.producer.example.example.benchmark.BenchmarkActivity;
1516
import com.aliyun.sls.android.producer.example.example.crash.CrashExampleActivity;
1617
import com.aliyun.sls.android.producer.example.example.instrumentation.WebViewInstrumentationActivity;
1718
import com.aliyun.sls.android.producer.example.example.network.NetworkExample;
1819
import com.aliyun.sls.android.producer.example.example.producer.ProducerExample;
19-
import com.aliyun.sls.android.producer.example.example.producer.ProducerWebTracking;
2020
import com.aliyun.sls.android.producer.example.example.producer.ProducerWithDestroy;
2121
import com.aliyun.sls.android.producer.example.example.producer.ProducerWithDynamicConfig;
2222
import com.aliyun.sls.android.producer.example.example.producer.ProducerWithImmediately;
@@ -40,12 +40,13 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
4040
findViewById(R.id.main_producer_multi_clients).setOnClickListener(this);
4141
findViewById(R.id.main_producer_immediately).setOnClickListener(this);
4242
findViewById(R.id.main_producer_destroy).setOnClickListener(this);
43-
findViewById(R.id.main_producer_webtracking).setOnClickListener(this);
4443
findViewById(R.id.main_apm_crash).setOnClickListener(this);
4544
findViewById(R.id.main_trace).setOnClickListener(this);
4645
findViewById(R.id.main_network_diagnosis).setOnClickListener(this);
4746
findViewById(R.id.main_trace_demo).setOnClickListener(this);
4847
findViewById(R.id.main_webview).setOnClickListener(this);
48+
findViewById(R.id.main_webview).setOnClickListener(this);
49+
findViewById(R.id.main_benchmark).setOnClickListener(this);
4950

5051
findViewById(R.id.main_kotlin_trace).setOnClickListener(this);
5152

@@ -71,9 +72,6 @@ public void onClick(View v) {
7172
} else if (R.id.main_producer_destroy == v.getId()) {
7273
// 销毁LogProducerClient
7374
startActivity(ProducerWithDestroy.class);
74-
} else if (R.id.main_producer_webtracking == v.getId()) {
75-
// 销毁LogProducerClient
76-
startActivity(ProducerWebTracking.class);
7775
} else if (R.id.main_apm_crash == v.getId()) {
7876
// 崩溃监控
7977
startActivity(CrashExampleActivity.class);
@@ -92,6 +90,9 @@ public void onClick(View v) {
9290
} else if (R.id.main_kotlin_trace == v.getId()) {
9391
// Trace Demo
9492
startActivity(TraceDemoKotlinActivity.class);
93+
} else if (R.id.main_benchmark == v.getId()) {
94+
// Trace Demo
95+
startActivity(BenchmarkActivity.class);
9596
}
9697
}
9798

app/src/main/java/com/aliyun/sls/android/producer/example/SLSDemoApplication.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public void onCreate() {
5757
PreferenceUtils.overrideConfig(this);
5858
}
5959

60+
//if (true) {
61+
// return;
62+
//}
63+
6064
initOTel();
6165

6266
Credentials credentials = new Credentials();
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
}

app/src/main/java/com/aliyun/sls/android/producer/example/example/producer/LogUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
* @author gordon
1010
* @date 2022/6/27
1111
*/
12-
class LogUtils {
13-
static Log createLog() {
12+
public class LogUtils {
13+
public static Log createLog() {
1414
Log log = new Log();
1515
// multi-type input content
1616
log.putContent("1int", 11);

app/src/main/java/com/aliyun/sls/android/producer/example/example/producer/ProducerWebTracking.java

Lines changed: 0 additions & 148 deletions
This file was deleted.

0 commit comments

Comments
 (0)