需要實(shí)現(xiàn)提高CPU占用率的功能也糊,用來(lái)做一個(gè)壓力測(cè)試葫盼。
但是找了一下午發(fā)現(xiàn)沒(méi)有相關(guān)工具,只有monkey這種測(cè)試穩(wěn)定性的測(cè)試工具叠艳,以及內(nèi)存壓力測(cè)試工具。有關(guān)CPU的測(cè)試工具只能顯示CPU的占用率易阳。但是現(xiàn)實(shí)CPU占用率的話附较,Android Studio本身就帶有顯示功能。
后面發(fā)現(xiàn)一種思路潦俺,就是把一些耗時(shí)的代碼寫進(jìn)app里面去拒课,從而達(dá)到提升CPU占用率的目的。
首先我想新建一個(gè)線程事示,使用sleep達(dá)到占用CPU的目的早像,但是這樣程序會(huì)崩潰,雖然提升了CPU的占用率但是內(nèi)存溢出了(猜測(cè)可能是因?yàn)閟leep時(shí)間太長(zhǎng))肖爵。
然后扎酷,我在線程內(nèi)寫了在for循環(huán)中對(duì)float進(jìn)行運(yùn)算操作,從而增加CPU的占用率遏匆。但是占用率不高法挨。后面想到更新UI是較為耗時(shí)的操作谁榜,因此,在界面增加了2個(gè)TextView控件凡纳,新建兩個(gè)Handler窃植,在循環(huán)內(nèi)對(duì)其控制更新。然后就基本穩(wěn)定在CPU百分之40左右的占用率荐糜。
最后具體使用就在實(shí)際APP中添加一個(gè)Button控件巷怜,點(diǎn)擊即開(kāi)啟線程,從而提高CPU占用率暴氏。
后面實(shí)際使用的時(shí)候發(fā)現(xiàn)了問(wèn)題延塑,加入到app中會(huì)崩掉,卡死答渔。于是又改變了思路关带,刪掉了更新UI的代碼。新的代碼在末尾沼撕。之前的代碼還是寫的太復(fù)雜了宋雏。新的代碼比較簡(jiǎn)潔。主要是用到了一個(gè)while來(lái)實(shí)現(xiàn)增加CPU占用率务豺。
MainActivity文件如下:
packagecom.demo.sleep;
importandroid.os.Handler;
importandroid.os.Message;
importandroid.support.v7.app.AppCompatActivity;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.widget.TextView;
public classMainActivityextendsAppCompatActivity {
private static finalStringTAG="MainActivity";
privateTextViewmTextView;
privateTextViewmText;
privateHandlermHandler;
privateHandlermTextHandler;
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView= (TextView)findViewById(R.id.text_view);
mText= (TextView)findViewById(R.id.text_id);
mHandler=newHandler(){
@Override
public voidhandleMessage(Message msg){
String s = (String) msg.obj;
mTextView.setText(s);
}
};
mTextHandler=newHandler(){
@Override
public voidhandleMessage(Message msg){
String s = (String) msg.obj;
mText.setText(s);
}
};
newThread(newRunnable() {
@Override
public voidrun() {
for(inti =0; i<10000;i++) {
floatsum =2.1f;
for(intj =0;j<1000;j++){
floatb =2.5f;
floatc =3.0f;
doubled = (double) (b*c +b-c);
doublef = (double) (b*b*b+c*c-b-c);
for(intaddnum? =0;addnum <3000;addnum++) {
String s = addnum+"";
Message msg =newMessage();
msg.obj= s;
mHandler.sendMessage(msg);
}
for(intaddnum? =0;addnum <3000;addnum++) {
String s = addnum+"";
Message msg =newMessage();
msg.obj= s;
mTextHandler.sendMessage(msg);
}
Log.d(TAG,"d is"+d);
newThread(newRunnable() {
@Override
public voidrun() {
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}).start();
sum = sum* b;
sum = sum *c;
sum = sum/b;
for(intaddnum? =0;addnum <3000;addnum++) {
String s = addnum+"";
Message msg =newMessage();
msg.obj= s;
mHandler.sendMessage(msg);
}
for(intaddnum? =0;addnum <3000;addnum++) {
String s = addnum+"";
Message msg =newMessage();
msg.obj= s;
mTextHandler.sendMessage(msg);
}
Log.d(TAG,"sum is "+sum);
newThread(newRunnable() {
@Override
public voidrun() {
try{
for(intaddnum? =0;addnum <3000;addnum++) {
String s = addnum+"";
Message msg =newMessage();
msg.obj= s;
mTextHandler.sendMessage(msg);
}
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}).start();
for(intk =0; k<1000;k++){
floate =2.0f;
floatl =3.0f;
l = (float) (e*f/sum+l-e);
String s = k+"";
Message msg =newMessage();
msg.obj= s;
mHandler.sendMessage(msg);
sum = sum/e;
String s1 = k+"";
Message msg1 =newMessage();
msg.obj= s1;
mTextHandler.sendMessage(msg1);
}
for(intq =0; q<1000;q++){
floatw =2.0f;
floatt =3.0f;
floatr = (float) (w*f/sum);
sum =? sum+t/w +r +r*w +t*w;
newThread(newRunnable() {
@Override
public voidrun() {
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}).start();
}
newThread(newRunnable() {
@Override
public voidrun() {
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}).start();
}
}
}
}).start();
}
}
xml文件如下:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.demo.sleep.MainActivity">
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
android:id="@+id/text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
public classMainActivityextendsAppCompatActivity {
private Button mCPUButton;
private boolean bTestThread=true;
private boolean bFlag=true;
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCPUButton= (Button)findViewById(R.id.cup_button);
mCPUButton.setOnClickListener(newView.OnClickListener() {
@Override
public voidonClick(View view) {
if(bFlag){
bFlag=false;
mCPUButton.setText("停止");
for(inti =0; i<100; i++)
cpuTest();
}else{
bFlag=true;
mCPUButton.setText("增加CPU壓力");
bTestThread=false;
}
}
});
}
private voidcpuTest(){
newThread(newRunnable() {
public voidrun(){
while(bTestThread)
{
}
}
}).start();
}
}
參考鏈接
http://www.voidcn.com/article/p-bpnqjlyi-ck.html
http://blog.csdn.net/xiaobai20131118/article/details/50698448
http://www.itwendao.com/article/detail/343816.html