Skip to content

Commit

Permalink
1. modify sample for performance testing
Browse files Browse the repository at this point in the history
  • Loading branch information
qiulinmin committed Nov 24, 2017
1 parent 862b22d commit ec780c4
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 59 deletions.
138 changes: 110 additions & 28 deletions app/src/main/java/me/pqpo/log4a/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import me.pqpo.librarylog4a.Level;
import me.pqpo.librarylog4a.Log4a;
import me.pqpo.librarylog4a.Logger;
import me.pqpo.librarylog4a.appender.AbsAppender;
import me.pqpo.librarylog4a.appender.AndroidAppender;
import me.pqpo.librarylog4a.appender.Appender;
import me.pqpo.librarylog4a.appender.FileAppender;

Expand All @@ -29,7 +35,6 @@ public class MainActivity extends AppCompatActivity {
TextView tvTest;
EditText etTimes;

RadioGroup radioGroup;
boolean testing = false;

@Override
Expand All @@ -43,14 +48,17 @@ protected void onCreate(Bundle savedInstanceState) {
btnTest = findViewById(R.id.btn_test);
tvTest = findViewById(R.id.tv_test);
etTimes = findViewById(R.id.et_times);
radioGroup = findViewById(R.id.rg_log);

btnWrite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (testing) {
Toast.makeText(getApplicationContext(), "testing", Toast.LENGTH_SHORT).show();
return;
}
int threads = Integer.valueOf(etThread.getText().toString());
if (threads > 200) {
Toast.makeText(getApplicationContext(), "Do not exceed 200 threads", Toast.LENGTH_SHORT).show();
if (threads > 500) {
Toast.makeText(getApplicationContext(), "Do not exceed 500 threads", Toast.LENGTH_SHORT).show();
return;
}
final String str = etContent.getText().toString();
Expand All @@ -71,30 +79,10 @@ public void run() {
@Override
public void onClick(View view) {
if (!testing) {
tvTest.setText("start testing");
tvTest.setText("start testing\n");
testing = true;
int times = Integer.valueOf(etTimes.getText().toString());
if (radioGroup.getCheckedRadioButtonId() == R.id.rb_log4a) {
long currentTimeMillis = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
Log4a.i(TAG, "log-" + i);
}
tvTest.setText(String.format(Locale.getDefault(),
"Log4a prints %d logs spend: %d ms\nlog file path: %s",
times,
System.currentTimeMillis() - currentTimeMillis,
getLogPath()));
} else if (radioGroup.getCheckedRadioButtonId() == R.id.rb_android_log) {
long currentTimeMillis = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
Log.i(TAG, "log-" + i);
}
tvTest.setText(String.format(Locale.getDefault(),
"Log prints %d logs spend: %d ms\nlog file path: %s",
times,
System.currentTimeMillis() - currentTimeMillis,
getLogPath()));
}
performanceTest(times);
testing = false;
} else {
Toast.makeText(getApplicationContext(), "testing", Toast.LENGTH_SHORT).show();
Expand All @@ -104,6 +92,100 @@ public void onClick(View view) {

}

private void performanceTest(int times) {
tvTest.append(String.format(Locale.getDefault(),
"## prints %d logs:\n", times));
Log4a.release();
Logger logger = new Logger.Builder().create();
Log4a.setLogger(logger);

logger.addAppender(createLog4aFileAppender());
doPerformanceTest("log4a" ,times);
// logger.flush();
Log4a.release();

Log4a.setLogger(logger);
logger.addAppender(createAndroidLogAppender());
doPerformanceTest("android log" ,times);
Log4a.release();

Log4a.setLogger(logger);
List<String> buffer = new ArrayList<>();
logger.addAppender(createMemAppender(buffer));
doPerformanceTest("array list log" ,times);
buffer.clear();
Log4a.release();

Log4a.setLogger(logger);
logger.addAppender(createFileAppender());
doPerformanceTest("file log" ,times);
Log4a.release();

LogInit.init(this);
tvTest.append("## end");
}

private Appender createFileAppender() {
File log = FileUtils.getLogDir(this);
File logFile = new File(log, "logFileTest.txt");
logFile.delete();
OutputStream os = null;
try {
logFile.createNewFile();
os = new FileOutputStream(logFile);
} catch (IOException e) {
e.printStackTrace();
}
final OutputStream oss = os;
return new AbsAppender() {
@Override
protected void doAppend(int logLevel, String tag, String msg) {
String logStr = String.format("%s/%s: %s\n", Level.getShortLevelName(logLevel), tag, msg);
try {
oss.write(logStr.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
};
}

private Appender createMemAppender(final List<String> buffer) {
return new AbsAppender() {
@Override
protected void doAppend(int logLevel, String tag, String msg) {
buffer.add(String.format("%s/%s: %s\n", Level.getShortLevelName(logLevel), tag, msg));
}
};
}

private Appender createLog4aFileAppender() {
File log = FileUtils.getLogDir(this);
File cacheFile = new File(log, "test.logCache");
File logFile = new File(log, "log4aTest.txt");
cacheFile.delete();
logFile.delete();
FileAppender.Builder fileBuild = new FileAppender.Builder(this)
.setLogFilePath(logFile.getAbsolutePath())
.setBufferFilePath(cacheFile.getAbsolutePath());
return fileBuild.create();
}

private Appender createAndroidLogAppender() {
return new AndroidAppender.Builder().create();
}

private void doPerformanceTest(String testName, int times) {
long currentTimeMillis = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
Log4a.i(TAG, "log-" + i);
}
tvTest.append(String.format(Locale.getDefault(),
"* %s spend: %d ms\n",
testName,
System.currentTimeMillis() - currentTimeMillis));
}

public String getLogPath() {
String logPath = "";
Logger logger = Log4a.getLogger();
Expand Down
60 changes: 32 additions & 28 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
android:orientation="vertical"
tools:context="me.pqpo.log4a.MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="Multi-threaded Testing"
android:padding="10dp"
android:textColor="@android:color/black"
android:textSize="20dp"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down Expand Up @@ -68,6 +78,7 @@
android:layout_height="wrap_content"
android:text="Performance Testing"
android:padding="10dp"
android:layout_marginTop="20dp"
android:textColor="@android:color/black"
android:textSize="20dp"/>

Expand All @@ -78,39 +89,21 @@
android:gravity="center_vertical"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="log times for test:"/>

<EditText
android:id="@+id/et_times"
android:layout_width="100dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
android:text="10000"
android:inputType="number"/>

<RadioGroup
android:id="@+id/rg_log"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:orientation="horizontal">

<RadioButton
android:id="@+id/rb_log4a"
android:layout_width="0dp"
android:layout_weight="1"
android:checked="true"
android:layout_height="wrap_content"
android:text="Log4a"/>

<RadioButton
android:id="@+id/rb_android_log"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Log"/>

</RadioGroup>

</LinearLayout>

<Button
Expand All @@ -120,11 +113,22 @@
android:text="test"/>

<TextView
android:id="@+id/tv_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Hello World!"
android:textSize="18sp"
android:text="result:"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/tv_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="log4a"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private void appendInner(int logLevel, String tag, String msg) {
}
}

abstract void doAppend(int logLevel, String tag, String msg);
protected abstract void doAppend(int logLevel, String tag, String msg);

@Override
public void flush() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected AndroidAppender(Builder builder) {
}

@Override
void doAppend(int logLevel, String tag, String msg) {
protected void doAppend(int logLevel, String tag, String msg) {
android.util.Log.println(logLevel, tag, msg);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void setFormatter(Formatter formatter) {
}

@Override
void doAppend(int logLevel, String tag, String msg) {
protected void doAppend(int logLevel, String tag, String msg) {
logBuffer.write(formatter.format(logLevel, tag, msg));
}

Expand Down

0 comments on commit ec780c4

Please sign in to comment.