2323import android .graphics .Bitmap ;
2424import android .graphics .BitmapFactory ;
2525import android .os .Bundle ;
26+ import android .os .FileUtils ;
2627import android .os .Handler ;
2728import android .os .Looper ;
2829import android .os .PowerManager ;
3536import android .view .WindowManager ;
3637import android .widget .TextView ;
3738
39+ import java .io .File ;
40+ import java .io .FileNotFoundException ;
41+ import java .io .FileOutputStream ;
42+ import java .io .IOException ;
43+ import java .io .RandomAccessFile ;
3844import java .util .ArrayList ;
3945
4046/**
4147 * So you thought sync used up your battery life.
4248 */
4349public class FrameworkPerfActivity extends Activity {
50+ static final String TAG = "Perf" ;
51+
4452 final Handler mHandler = new Handler ();
4553
4654 TextView mLog ;
@@ -56,12 +64,35 @@ public class FrameworkPerfActivity extends Activity {
5664 new MethodCallOp (), new CpuOp (),
5765 new MethodCallOp (), new SchedulerOp (),
5866 new MethodCallOp (), new GcOp (),
67+ new MethodCallOp (), new CreateFileOp (),
68+ new MethodCallOp (), new CreateWriteFileOp (),
69+ new MethodCallOp (), new CreateWriteSyncFileOp (),
70+ new MethodCallOp (), new WriteFileOp (),
71+ new MethodCallOp (), new ReadFileOp (),
5972 new SchedulerOp (), new SchedulerOp (),
6073 new GcOp (), new NoOp (),
6174 new IpcOp (), new NoOp (),
6275 new IpcOp (), new CpuOp (),
6376 new IpcOp (), new SchedulerOp (),
6477 new IpcOp (), new GcOp (),
78+ new IpcOp (), new CreateFileOp (),
79+ new IpcOp (), new CreateWriteFileOp (),
80+ new IpcOp (), new CreateWriteSyncFileOp (),
81+ new IpcOp (), new WriteFileOp (),
82+ new IpcOp (), new ReadFileOp (),
83+ new CreateFileOp (), new NoOp (),
84+ new CreateWriteFileOp (), new NoOp (),
85+ new CreateWriteSyncFileOp (), new NoOp (),
86+ new WriteFileOp (), new NoOp (),
87+ new ReadFileOp (), new NoOp (),
88+ new WriteFileOp (), new CreateWriteFileOp (),
89+ new ReadFileOp (), new CreateWriteFileOp (),
90+ new WriteFileOp (), new CreateWriteSyncFileOp (),
91+ new ReadFileOp (), new CreateWriteSyncFileOp (),
92+ new WriteFileOp (), new WriteFileOp (),
93+ new WriteFileOp (), new ReadFileOp (),
94+ new ReadFileOp (), new WriteFileOp (),
95+ new ReadFileOp (), new ReadFileOp (),
6596 new ParseXmlResOp (), new NoOp (),
6697 new ParseLargeXmlResOp (), new NoOp (),
6798 new LayoutInflaterOp (), new NoOp (),
@@ -204,7 +235,7 @@ void stopRunning() {
204235 float bgMsPerOp = result .getBgMsPerOp ();
205236 String fgMsPerOpStr = fgMsPerOp != 0 ? Float .toString (fgMsPerOp ) : "" ;
206237 String bgMsPerOpStr = bgMsPerOp != 0 ? Float .toString (bgMsPerOp ) : "" ;
207- Log .i ("Perf" , "\t " + result .name + "\t " + result .fgOps
238+ Log .i (TAG , "\t " + result .name + "\t " + result .fgOps
208239 + "\t " + result .getFgMsPerOp () + "\t " + result .fgTime
209240 + "\t " + result .fgLongName + "\t " + result .bgOps
210241 + "\t " + result .getBgMsPerOp () + "\t " + result .bgTime
@@ -229,7 +260,7 @@ void updateWakeLock() {
229260
230261 void log (String s ) {
231262 mLog .setText (mLog .getText () + "\n " + s );
232- Log .i ("Perf" , s );
263+ Log .i (TAG , s );
233264 }
234265
235266 enum BackgroundMode {
@@ -322,8 +353,8 @@ public void run(Handler handler, Op foreground, Op background, Runnable doneCall
322353 }
323354 }, Process .THREAD_PRIORITY_FOREGROUND );
324355
325- mForegroundOp .onInit (FrameworkPerfActivity .this );
326- mBackgroundOp .onInit (FrameworkPerfActivity .this );
356+ mForegroundOp .onInit (FrameworkPerfActivity .this , true );
357+ mBackgroundOp .onInit (FrameworkPerfActivity .this , false );
327358
328359 synchronized (this ) {
329360 mStartTime = SystemClock .uptimeMillis ();
@@ -380,6 +411,8 @@ private void threadFinished(boolean foreground) {
380411 if (!mBackgroundRunning && !mForegroundRunning ) {
381412 mHandler .post (new Runnable () {
382413 @ Override public void run () {
414+ mForegroundOp .onTerm (FrameworkPerfActivity .this );
415+ mBackgroundOp .onTerm (FrameworkPerfActivity .this );
383416 if (mDoneCallback != null ) {
384417 mDoneCallback .run ();
385418 }
@@ -423,11 +456,14 @@ public String getLongName() {
423456 return mLongName ;
424457 }
425458
426- void onInit (Context context ) {
459+ void onInit (Context context , boolean foreground ) {
427460 }
428461
429462 abstract boolean onRun ();
430463
464+ void onTerm (Context context ) {
465+ }
466+
431467 int getOpsPerRun () {
432468 return 1 ;
433469 }
@@ -510,7 +546,7 @@ static class IpcOp extends Op {
510546 super ("Ipc" , "IPC to system process" );
511547 }
512548
513- void onInit (Context context ) {
549+ void onInit (Context context , boolean foreground ) {
514550 mPm = context .getPackageManager ();
515551 mProcessName = context .getApplicationInfo ().processName ;
516552 }
@@ -535,7 +571,7 @@ static class ParseXmlResOp extends Op {
535571 super ("ParseXmlRes" , "Parse compiled XML resource" );
536572 }
537573
538- void onInit (Context context ) {
574+ void onInit (Context context , boolean foreground ) {
539575 mContext = context ;
540576 }
541577
@@ -553,7 +589,7 @@ static class ParseLargeXmlResOp extends Op {
553589 super ("ParseLargeXmlRes" , "Parse large XML resource" );
554590 }
555591
556- void onInit (Context context ) {
592+ void onInit (Context context , boolean foreground ) {
557593 mContext = context ;
558594 }
559595
@@ -571,7 +607,7 @@ static class LayoutInflaterOp extends Op {
571607 super ("LayoutInflaterOp" , "Inflate layout resource" );
572608 }
573609
574- void onInit (Context context ) {
610+ void onInit (Context context , boolean foreground ) {
575611 mContext = context ;
576612 }
577613
@@ -593,7 +629,7 @@ static class LayoutInflaterLargeOp extends Op {
593629 super ("LayoutInflaterLargeOp" , "Inflate large layout resource" );
594630 }
595631
596- void onInit (Context context ) {
632+ void onInit (Context context , boolean foreground ) {
597633 mContext = context ;
598634 }
599635
@@ -615,7 +651,7 @@ static class LoadSmallBitmapOp extends Op {
615651 super ("LoadSmallBitmap" , "Load small raw bitmap" );
616652 }
617653
618- void onInit (Context context ) {
654+ void onInit (Context context , boolean foreground ) {
619655 mContext = context ;
620656 }
621657
@@ -636,7 +672,7 @@ static class LoadLargeBitmapOp extends Op {
636672 super ("LoadLargeBitmap" , "Load large raw bitmap" );
637673 }
638674
639- void onInit (Context context ) {
675+ void onInit (Context context , boolean foreground ) {
640676 mContext = context ;
641677 }
642678
@@ -657,7 +693,7 @@ static class LoadSmallScaledBitmapOp extends Op {
657693 super ("LoadSmallScaledBitmap" , "Load small raw bitmap that is scaled for density" );
658694 }
659695
660- void onInit (Context context ) {
696+ void onInit (Context context , boolean foreground ) {
661697 mContext = context ;
662698 }
663699
@@ -678,7 +714,7 @@ static class LoadLargeScaledBitmapOp extends Op {
678714 super ("LoadLargeScaledBitmap" , "Load large raw bitmap that is scaled for density" );
679715 }
680716
681- void onInit (Context context ) {
717+ void onInit (Context context , boolean foreground ) {
682718 mContext = context ;
683719 }
684720
@@ -691,4 +727,169 @@ boolean onRun() {
691727 return true ;
692728 }
693729 }
730+
731+ static class CreateFileOp extends Op {
732+ File mFile ;
733+
734+ CreateFileOp () {
735+ super ("CreateFile" , "Create and delete a file" );
736+ }
737+
738+ void onInit (Context context , boolean foreground ) {
739+ mFile = context .getFileStreamPath (foreground ? "test-fg.file" : "test-bg.file" );
740+ mFile .delete ();
741+ }
742+
743+ boolean onRun () {
744+ try {
745+ mFile .createNewFile ();
746+ } catch (IOException e ) {
747+ Log .w (TAG , "Failure creating " + mFile , e );
748+ }
749+ mFile .delete ();
750+ return true ;
751+ }
752+ }
753+
754+ static class CreateWriteFileOp extends Op {
755+ File mFile ;
756+
757+ CreateWriteFileOp () {
758+ super ("CreateWriteFile" , "Create, write, and delete a file" );
759+ }
760+
761+ void onInit (Context context , boolean foreground ) {
762+ mFile = context .getFileStreamPath (foreground ? "test-fg.file" : "test-bg.file" );
763+ mFile .delete ();
764+ }
765+
766+ boolean onRun () {
767+ try {
768+ FileOutputStream fos = new FileOutputStream (mFile );
769+ fos .write (1 );
770+ fos .close ();
771+ } catch (IOException e ) {
772+ Log .w (TAG , "Failure creating " + mFile , e );
773+ }
774+ mFile .delete ();
775+ return true ;
776+ }
777+ }
778+
779+ static class CreateWriteSyncFileOp extends Op {
780+ File mFile ;
781+
782+ CreateWriteSyncFileOp () {
783+ super ("CreateWriteSyncFile" , "Create, write, sync, and delete a file" );
784+ }
785+
786+ void onInit (Context context , boolean foreground ) {
787+ mFile = context .getFileStreamPath (foreground ? "test-fg.file" : "test-bg.file" );
788+ mFile .delete ();
789+ }
790+
791+ boolean onRun () {
792+ try {
793+ FileOutputStream fos = new FileOutputStream (mFile );
794+ fos .write (1 );
795+ fos .flush ();
796+ FileUtils .sync (fos );
797+ fos .close ();
798+ } catch (IOException e ) {
799+ Log .w (TAG , "Failure creating " + mFile , e );
800+ }
801+ mFile .delete ();
802+ return true ;
803+ }
804+ }
805+
806+ static class WriteFileOp extends Op {
807+ File mFile ;
808+ RandomAccessFile mRAF ;
809+ byte [] mBuffer ;
810+
811+ WriteFileOp () {
812+ super ("WriteFile" , "Truncate and write a 64k file" );
813+ }
814+
815+ void onInit (Context context , boolean foreground ) {
816+ mBuffer = new byte [1024 *64 ];
817+ for (int i =0 ; i <mBuffer .length ; i ++) {
818+ mBuffer [i ] = (byte )i ;
819+ }
820+ mFile = context .getFileStreamPath (foreground ? "test-fg.file" : "test-bg.file" );
821+ mFile .delete ();
822+ try {
823+ mRAF = new RandomAccessFile (mFile , "rw" );
824+ } catch (FileNotFoundException e ) {
825+ Log .w (TAG , "Failure creating " + mFile , e );
826+ }
827+ }
828+
829+ boolean onRun () {
830+ try {
831+ mRAF .seek (0 );
832+ mRAF .setLength (0 );
833+ mRAF .write (mBuffer );
834+ } catch (IOException e ) {
835+ Log .w (TAG , "Failure writing " + mFile , e );
836+ }
837+ return true ;
838+ }
839+
840+ void onTerm (Context context ) {
841+ try {
842+ mRAF .close ();
843+ } catch (IOException e ) {
844+ Log .w (TAG , "Failure closing " + mFile , e );
845+ }
846+ mFile .delete ();
847+ }
848+ }
849+
850+ static class ReadFileOp extends Op {
851+ File mFile ;
852+ RandomAccessFile mRAF ;
853+ byte [] mBuffer ;
854+
855+ ReadFileOp () {
856+ super ("ReadFile" , "Seek and read a 64k file" );
857+ }
858+
859+ void onInit (Context context , boolean foreground ) {
860+ mBuffer = new byte [1024 *64 ];
861+ for (int i =0 ; i <mBuffer .length ; i ++) {
862+ mBuffer [i ] = (byte )i ;
863+ }
864+ mFile = context .getFileStreamPath (foreground ? "test-fg.file" : "test-bg.file" );
865+ mFile .delete ();
866+ try {
867+ mRAF = new RandomAccessFile (mFile , "rw" );
868+ mRAF .seek (0 );
869+ mRAF .setLength (0 );
870+ mRAF .write (mBuffer );
871+ } catch (IOException e ) {
872+ Log .w (TAG , "Failure creating " + mFile , e );
873+ }
874+ }
875+
876+ boolean onRun () {
877+ try {
878+ mRAF .seek (0 );
879+ mRAF .read (mBuffer );
880+ } catch (IOException e ) {
881+ Log .w (TAG , "Failure reading " + mFile , e );
882+ }
883+ return true ;
884+ }
885+
886+ void onTerm (Context context ) {
887+ try {
888+ mRAF .close ();
889+ } catch (IOException e ) {
890+ Log .w (TAG , "Failure closing " + mFile , e );
891+ }
892+ mFile .delete ();
893+ }
894+ }
694895}
0 commit comments