效能測試之一----I/O讀寫速度(1)

來源:互聯網
上載者:User

標籤:

公司做的智能硬體,有些地方需要和硬體晶片互動,不多說,直接上代碼:

import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import android.os.AsyncTask;import android.os.Bundle;import android.os.Environment;import android.os.SystemClock;import android.support.v7.app.ActionBarActivity;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.TextView;public class MainActivity extends ActionBarActivity {    private TextView mWriteTime;    private Button mBtnTest;    private static final String    TAG  = "Ilejia::TestSDCard";    private TestAsyncTask mTestTask = null;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mWriteTime = (TextView)findViewById(R.id.write_time);        mBtnTest = (Button) findViewById(R.id.btn_test);        mBtnTest.setOnClickListener(new View.OnClickListener() {                        @Override            public void onClick(View v) {                if (mTestTask != null && mTestTask.getStatus() != AsyncTask.Status.FINISHED) {                    return;                }                                mTestTask = new TestAsyncTask();                mTestTask.execute((Void[])null);            }        });    }        private class TestAsyncTask extends AsyncTask<Void, Void, Long> {        @Override        protected void onPreExecute() {            mBtnTest.setEnabled(false);            mWriteTime.setText("Testing...");        }        @Override        protected Long doInBackground(Void... params) {            return runTest();        }                protected void onPostExecute(Long result) {            mBtnTest.setEnabled(true);            if (result > 0) {                mWriteTime.setText("寫100M資料耗時" + result + "ms");            }        };    } long runTest() {        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "Ilejia");        File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "test.dat");        if (!mediaStorageDir.exists()) {            if (!mediaStorageDir.mkdirs()) {                Log.e(TAG , "failed to create directory");                return -1;            }        }//        if (mediaFile.exists()) {//            mediaFile.delete();//        }        Log.i(TAG , "preparing data");                StringBuilder s =  new StringBuilder();        s.append("this is a test string writing to file.");        for(int i = 0; i < 1000; i++){            s.append("0123456789");        }        Log.i(TAG , "write data");        long startTime = SystemClock.uptimeMillis();        FileOutputStream stream = null;        try {            stream = new FileOutputStream(mediaFile);            byte[] buf = s.toString().getBytes();            for(int i = 0; i < 10000; ++i){                  stream.write(buf);                        }            stream.close();          } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } finally {            if (stream != null) {                try {                    stream.close();                } catch (Exception e) {                    e.printStackTrace();                }            }        }        Log.i(TAG , "write data successful");        return SystemClock.uptimeMillis() - startTime;    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

 

效能測試之一----I/O讀寫速度(1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.