summaryrefslogtreecommitdiffstats
path: root/tests/FrameworkPerf/src/com/android/frameworkperf/TestService.java
blob: 5f4f00626059d9d02ded80e60a0dee9f1f3ee11e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.frameworkperf;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.FileUtils;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.Process;
import android.os.RemoteException;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.Xml;
import android.view.LayoutInflater;

public class TestService extends Service {
    static final String TAG = "Perf";

    final static Op[] mOpPairs = new Op[] {
            new MethodCallOp(), new NoOp(),
            new MethodCallOp(), new CpuOp(),
            new MethodCallOp(), new SchedulerOp(),
            new MethodCallOp(), new GcOp(),
            new MethodCallOp(), new CreateFileOp(),
            new MethodCallOp(), new CreateWriteFileOp(),
            new MethodCallOp(), new CreateWriteSyncFileOp(),
            new MethodCallOp(), new WriteFileOp(),
            new MethodCallOp(), new ReadFileOp(),
            new SchedulerOp(), new SchedulerOp(),
            new GcOp(), new NoOp(),
            new ObjectGcOp(), new NoOp(),
            new FinalizingGcOp(), new NoOp(),
            new PaintGcOp(), new NoOp(),
            new IpcOp(), new NoOp(),
            new IpcOp(), new CpuOp(),
            new IpcOp(), new SchedulerOp(),
            new IpcOp(), new GcOp(),
            new IpcOp(), new CreateFileOp(),
            new IpcOp(), new CreateWriteFileOp(),
            new IpcOp(), new CreateWriteSyncFileOp(),
            new IpcOp(), new WriteFileOp(),
            new IpcOp(), new ReadFileOp(),
            new CreateFileOp(), new NoOp(),
            new CreateWriteFileOp(), new NoOp(),
            new CreateWriteSyncFileOp(), new NoOp(),
            new WriteFileOp(), new NoOp(),
            new ReadFileOp(), new NoOp(),
            new WriteFileOp(), new CreateWriteFileOp(),
            new ReadFileOp(), new CreateWriteFileOp(),
            new WriteFileOp(), new CreateWriteSyncFileOp(),
            new ReadFileOp(), new CreateWriteSyncFileOp(),
            new WriteFileOp(), new WriteFileOp(),
            new WriteFileOp(), new ReadFileOp(),
            new ReadFileOp(), new WriteFileOp(),
            new ReadFileOp(), new ReadFileOp(),
            new OpenXmlResOp(), new NoOp(),
            new ReadXmlAttrsOp(), new NoOp(),
            new ParseXmlResOp(), new NoOp(),
            new ParseLargeXmlResOp(), new NoOp(),
            new LayoutInflaterOp(), new NoOp(),
            new LayoutInflaterLargeOp(), new NoOp(),
            new LayoutInflaterViewOp(), new NoOp(),
            new LayoutInflaterButtonOp(), new NoOp(),
            new LayoutInflaterImageButtonOp(), new NoOp(),
            new CreateBitmapOp(), new NoOp(),
            new CreateRecycleBitmapOp(), new NoOp(),
            new LoadSmallBitmapOp(), new NoOp(),
            new LoadRecycleSmallBitmapOp(), new NoOp(),
            new LoadLargeBitmapOp(), new NoOp(),
            new LoadRecycleLargeBitmapOp(), new NoOp(),
            new LoadSmallScaledBitmapOp(), new NoOp(),
            new LoadLargeScaledBitmapOp(), new NoOp(),
    };

    final static Op[] mAvailOps = new Op[] {
            null,
            new NoOp(),
            new CpuOp(),
            new SchedulerOp(),
            new MethodCallOp(),
            new GcOp(),
            new ObjectGcOp(),
            new FinalizingGcOp(),
            new PaintGcOp(),
            new IpcOp(),
            new CreateFileOp(),
            new CreateWriteFileOp(),
            new CreateWriteSyncFileOp(),
            new WriteFileOp(),
            new ReadFileOp(),
            new OpenXmlResOp(),
            new ReadXmlAttrsOp(),
            new ParseXmlResOp(),
            new ParseLargeXmlResOp(),
            new LayoutInflaterOp(),
            new LayoutInflaterLargeOp(),
            new LayoutInflaterViewOp(),
            new LayoutInflaterButtonOp(),
            new LayoutInflaterImageButtonOp(),
            new CreateBitmapOp(),
            new CreateRecycleBitmapOp(),
            new LoadSmallBitmapOp(),
            new LoadRecycleSmallBitmapOp(),
            new LoadLargeBitmapOp(),
            new LoadRecycleLargeBitmapOp(),
            new LoadSmallScaledBitmapOp(),
            new LoadLargeScaledBitmapOp(),
    };

    static final int CMD_START_TEST = 1;
    static final int CMD_TERMINATE = 2;

    static final int MSG_REALLY_START = 1000;
    static final int MSG_REALLY_TERMINATE = 1001;

    static final int RES_TEST_FINISHED = 1;
    static final int RES_TERMINATED = 2;

    final Handler mHandler = new Handler() {
        @Override public void handleMessage(Message msg) {
            switch (msg.what) {
                case CMD_START_TEST: {
                    // Give a little time for things to settle down.
                    Message newMsg = Message.obtain(null, MSG_REALLY_START);
                    newMsg.obj = msg.obj;
                    newMsg.replyTo = msg.replyTo;
                    sendMessageDelayed(newMsg, 500);
                } break;
                case MSG_REALLY_START: {
                    Bundle bundle = (Bundle)msg.obj;
                    bundle.setClassLoader(getClassLoader());
                    final TestArgs args = (TestArgs)bundle.getParcelable("args");
                    final Messenger replyTo = msg.replyTo;
                    mRunner.run(this, args, new Runnable() {
                        @Override public void run() {
                            if (replyTo != null) {
                                Message msg = Message.obtain(null, RES_TEST_FINISHED);
                                Bundle bundle = new Bundle();
                                bundle.putParcelable("res", new RunResult(mRunner));
                                msg.obj = bundle;
                                try {
                                    replyTo.send(msg);
                                } catch (RemoteException e) {
                                }
                            }
                        }
                    });
                } break;
                case CMD_TERMINATE: {
                    // Give a little time for things to settle down.
                    Message newMsg = Message.obtain(null, MSG_REALLY_TERMINATE);
                    newMsg.obj = msg.obj;
                    newMsg.replyTo = msg.replyTo;
                    sendMessageDelayed(newMsg, 50);
                } break;
                case MSG_REALLY_TERMINATE: {
                    if (msg.replyTo != null) {
                        Message reply = Message.obtain(null, RES_TERMINATED);
                        try {
                            msg.replyTo.send(reply);
                        } catch (RemoteException e) {
                        }
                    }
                    terminate();
                } break;
            }
        }
    };

    final TestRunner mRunner = new TestRunner();

    @Override
    public IBinder onBind(Intent intent) {
        return (new Messenger(mHandler)).getBinder();
    }

    void terminate() {
        Runtime.getRuntime().exit(0);
    }

    enum BackgroundMode {
        NOTHING,
        CPU,
        SCHEDULER
    };

    public class TestRunner {
        Handler mHandler;
        long mMaxRunTime;
        long mMaxOps;
        Op mForegroundOp;
        Op mBackgroundOp;
        Runnable mDoneCallback;

        RunnerThread mBackgroundThread;
        RunnerThread mForegroundThread;
        long mStartTime;

        boolean mBackgroundRunning;
        boolean mForegroundRunning;

        long mBackgroundEndTime;
        long mBackgroundOps;
        long mForegroundEndTime;
        long mForegroundOps;

        public TestRunner() {
        }

        public String getForegroundName() {
            return mForegroundOp.getName();
        }

        public String getBackgroundName() {
            return mBackgroundOp.getName();
        }

        public String getName() {
            String fgName = mForegroundOp.getName();
            String bgName = mBackgroundOp.getName();
            StringBuilder res = new StringBuilder();
            if (fgName != null) {
                res.append(fgName);
                res.append("Fg");
            }
            if (bgName != null) {
                res.append(bgName);
                res.append("Bg");
            }
            return res.toString();
        }

        public String getForegroundLongName() {
            return mForegroundOp.getLongName();
        }

        public String getBackgroundLongName() {
            return mBackgroundOp.getLongName();
        }

        public void run(Handler handler, TestArgs args, Runnable doneCallback) {
            mHandler = handler;
            mMaxRunTime = args.maxTime;
            mMaxOps = args.maxOps;
            if (args.combOp >= 0) {
                mForegroundOp = mOpPairs[args.combOp];
                mBackgroundOp = mOpPairs[args.combOp+1];
            } else {
                mForegroundOp = mAvailOps[args.fgOp];
                mBackgroundOp = mAvailOps[args.bgOp];
            }
            mDoneCallback = doneCallback;
            mBackgroundThread = new RunnerThread("background", new Runnable() {
                @Override public void run() {
                    boolean running;
                    int ops = 0;
                    do {
                        running = mBackgroundOp.onRun();
                        ops++;
                    } while (evalRepeat(running, true) && running);
                    mBackgroundEndTime = SystemClock.uptimeMillis();
                    mBackgroundOps = ops * mBackgroundOp.getOpsPerRun();
                    threadFinished(false);
                }
            }, Process.THREAD_PRIORITY_BACKGROUND);
            mForegroundThread = new RunnerThread("foreground", new Runnable() {
                @Override public void run() {
                    boolean running;
                    int ops = 0;
                    do {
                        running = mForegroundOp.onRun();
                        ops++;
                    } while (evalRepeat(true, running) && running);
                    mForegroundEndTime = SystemClock.uptimeMillis();
                    mForegroundOps = ops * mForegroundOp.getOpsPerRun();
                    threadFinished(true);
                }
            }, Process.THREAD_PRIORITY_FOREGROUND);

            mForegroundOp.onInit(TestService.this, true);
            mBackgroundOp.onInit(TestService.this, false);

            synchronized (this) {
                mStartTime = SystemClock.uptimeMillis();
                mBackgroundRunning = true;
                mForegroundRunning = true;
            }

            mBackgroundThread.start();
            mForegroundThread.start();
        }

        public long getForegroundTime() {
            return mForegroundEndTime-mStartTime;
        }

        public long getForegroundOps() {
            return mForegroundOps;
        }

        public long getBackgroundTime() {
            return mBackgroundEndTime-mStartTime;
        }

        public long getBackgroundOps() {
            return mBackgroundOps;
        }

        private boolean evalRepeat(boolean bgRunning, boolean fgRunning) {
            synchronized (this) {
                if (!bgRunning) {
                    mBackgroundRunning = false;
                }
                if (!fgRunning) {
                    mForegroundRunning = false;
                }
                if (!mBackgroundRunning && !mForegroundRunning) {
                    return false;
                }
                if (mMaxOps > 0) {
                    // iteration-limited case
                    if (mForegroundOps >= mMaxOps) {
                        return false;
                    }
                    mForegroundOps++;
                } else {
                    // time-limited case
                    long now = SystemClock.uptimeMillis();
                    if (now > (mStartTime+mMaxRunTime)) {
                        return false;
                    }
                }
                return true;
            }
        }

        private void threadFinished(boolean foreground) {
            synchronized (this) {
                if (foreground) {
                    mForegroundRunning = false;
                } else {
                    mBackgroundRunning = false;
                }
                if (!mBackgroundRunning && !mForegroundRunning) {
                    mHandler.post(new Runnable() {
                        @Override public void run() {
                            mForegroundOp.onTerm(TestService.this);
                            mBackgroundOp.onTerm(TestService.this);
                            if (mDoneCallback != null) {
                                mDoneCallback.run();
                            }
                        }
                    });
                }
            }
        }
    }

    class RunnerThread extends Thread {
        private final Runnable mOp;
        private final int mPriority;

        RunnerThread(String name, Runnable op, int priority) {
            super(name);
            mOp = op;
            mPriority = priority;
        }

        public void run() {
            Process.setThreadPriority(mPriority);
            mOp.run();
        }
    }

    static public abstract class Op {
        final String mName;
        final String mLongName;

        public Op(String name, String longName) {
            mName = name;
            mLongName = longName;
        }

        public String getName() {
            return mName;
        }

        public String getLongName() {
            return mLongName;
        }

        void onInit(Context context, boolean foreground) {
        }

        abstract boolean onRun();

        void onTerm(Context context) {
        }

        int getOpsPerRun() {
            return 1;
        }
    }

    static class NoOp extends Op {
        NoOp() {
            super(null, "Nothing");
        }

        boolean onRun() {
            return false;
        }

        int getOpsPerRun() {
            return 0;
        }
    }

    static class CpuOp extends Op {
        CpuOp() {
            super("CPU", "Consume CPU");
        }

        boolean onRun() {
            return true;
        }
    }

    static class SchedulerOp extends Op {
        SchedulerOp() {
            super("Sched", "Change scheduler group");
        }

        boolean onRun() {
            Process.setThreadPriority(Process.THREAD_PRIORITY_FOREGROUND);
            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
            return true;
        }
    }

    static class GcOp extends Op {
        GcOp() {
            super("Gc", "Run garbage collector");
        }

        boolean onRun() {
            byte[] stuff = new byte[1024*1024];
            return true;
        }
    }

    static class ObjectGcOp extends Op {
        ObjectGcOp() {
            super("ObjectGc", "Run garbage collector with simple objects");
        }

        boolean onRun() {
            Object obj = new Object();
            return true;
        }
    }

    static class FinalizingGcOp extends Op {
        class Finalizable {
            Finalizable() {}
            @Override
            protected void finalize() throws Throwable {
                super.finalize();
            }
        }

        FinalizingGcOp() {
            super("FinalizingGc", "Run garbage collector with finalizable objects");
        }

        boolean onRun() {
            Finalizable obj = new Finalizable();
            return true;
        }
    }

    static class PaintGcOp extends Op {
        PaintGcOp() {
            super("PaintGc", "Run garbage collector with Paint objects");
        }

        boolean onRun() {
            Paint p = new Paint();
            return true;
        }
    }

    static class MethodCallOp extends Op {
        MethodCallOp() {
            super("MethodCall", "Method call");
        }

        boolean onRun() {
            final int N = getOpsPerRun();
            for (int i=0; i<N; i++) {
                someFunc(i);
            }
            return true;
        }

        int someFunc(int foo) {
            return 0;
        }

        int getOpsPerRun() {
            return 500;
        }
    }

    static class IpcOp extends Op {
        PackageManager mPm;
        String mProcessName;

        IpcOp() {
            super("Ipc", "IPC to system process");
        }

        void onInit(Context context, boolean foreground) {
            mPm = context.getPackageManager();
            mProcessName = context.getApplicationInfo().processName;
        }

        boolean onRun() {
            final int N = getOpsPerRun();
            for (int i=0; i<N; i++) {
                mPm.queryContentProviders(mProcessName, Process.myUid(), 0);
            }
            return true;
        }

        int getOpsPerRun() {
            return 100;
        }
    }

    static class OpenXmlResOp extends Op {
        Context mContext;

        OpenXmlResOp() {
            super("OpenXmlRes", "Open (and close) an XML resource");
        }

        void onInit(Context context, boolean foreground) {
            mContext = context;
        }

        boolean onRun() {
            XmlResourceParser parser = mContext.getResources().getLayout(R.xml.simple);
            parser.close();
            return true;
        }
    }

    static class ReadXmlAttrsOp extends Op {
        Context mContext;
        XmlResourceParser mParser;
        AttributeSet mAttrs;

        ReadXmlAttrsOp() {
            super("ReadXmlAttrs", "Read attributes from an XML tag");
        }

        void onInit(Context context, boolean foreground) {
            mContext = context;
            mParser = mContext.getResources().getLayout(R.xml.simple);
            mAttrs = Xml.asAttributeSet(mParser);

            int eventType;
            try {
                // Find the first <item> tag.
                eventType = mParser.getEventType();
                String tagName;
                do {
                    if (eventType == XmlPullParser.START_TAG) {
                        tagName = mParser.getName();
                        if (tagName.equals("item")) {
                            break;
                        }
                    }
                    eventType = mParser.next();
                } while (eventType != XmlPullParser.END_DOCUMENT);
            } catch (XmlPullParserException e) {
                throw new RuntimeException("I died", e);
            } catch (IOException e) {
                throw new RuntimeException("I died", e);
            }
        }

        void onTerm(Context context) {
            mParser.close();
        }

        boolean onRun() {
            TypedArray a = mContext.obtainStyledAttributes(mAttrs,
                    com.android.internal.R.styleable.MenuItem);
            a.recycle();
            return true;
        }
    }

    static class ParseXmlResOp extends Op {
        Context mContext;

        ParseXmlResOp() {
            super("ParseXmlRes", "Parse compiled XML resource");
        }

        void onInit(Context context, boolean foreground) {
            mContext = context;
        }

        boolean onRun() {
            SimpleInflater inf = new SimpleInflater(mContext);
            inf.inflate(R.xml.simple);
            return true;
        }
    }

    static class ParseLargeXmlResOp extends Op {
        Context mContext;

        ParseLargeXmlResOp() {
            super("ParseLargeXmlRes", "Parse large XML resource");
        }

        void onInit(Context context, boolean foreground) {
            mContext = context;
        }

        boolean onRun() {
            SimpleInflater inf = new SimpleInflater(mContext);
            inf.inflate(R.xml.simple_large);
            return true;
        }
    }

    static class LayoutInflaterOp extends Op {
        Context mContext;

        LayoutInflaterOp() {
            super("LayoutInflater", "Inflate layout resource");
        }

        void onInit(Context context, boolean foreground) {
            mContext = context;
        }

        boolean onRun() {
            if (Looper.myLooper() == null) {
                Looper.prepare();
            }
            LayoutInflater inf = (LayoutInflater)mContext.getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            inf.inflate(R.layout.small_layout, null);
            return true;
        }
    }

    static class LayoutInflaterLargeOp extends Op {
        Context mContext;

        LayoutInflaterLargeOp() {
            super("LayoutInflaterLarge", "Inflate large layout resource");
        }

        void onInit(Context context, boolean foreground) {
            mContext = context;
        }

        boolean onRun() {
            if (Looper.myLooper() == null) {
                Looper.prepare();
            }
            LayoutInflater inf = (LayoutInflater)mContext.getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            inf.inflate(R.layout.large_layout, null);
            return true;
        }
    }

    static class LayoutInflaterViewOp extends Op {
        Context mContext;

        LayoutInflaterViewOp() {
            super("LayoutInflaterView", "Inflate layout with 50 View objects");
        }

        void onInit(Context context, boolean foreground) {
            mContext = context;
        }

        boolean onRun() {
            if (Looper.myLooper() == null) {
                Looper.prepare();
            }
            LayoutInflater inf = (LayoutInflater)mContext.getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            inf.inflate(R.layout.view_layout, null);
            return true;
        }
    }

    static class LayoutInflaterButtonOp extends Op {
        Context mContext;

        LayoutInflaterButtonOp() {
            super("LayoutInflaterButton", "Inflate layout with 50 Button objects");
        }

        void onInit(Context context, boolean foreground) {
            mContext = context;
        }

        boolean onRun() {
            if (Looper.myLooper() == null) {
                Looper.prepare();
            }
            LayoutInflater inf = (LayoutInflater)mContext.getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            inf.inflate(R.layout.button_layout, null);
            return true;
        }
    }

    static class LayoutInflaterImageButtonOp extends Op {
        Context mContext;

        LayoutInflaterImageButtonOp() {
            super("LayoutInflaterImageButton", "Inflate layout with 50 ImageButton objects");
        }

        void onInit(Context context, boolean foreground) {
            mContext = context;
        }

        boolean onRun() {
            if (Looper.myLooper() == null) {
                Looper.prepare();
            }
            LayoutInflater inf = (LayoutInflater)mContext.getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            inf.inflate(R.layout.image_button_layout, null);
            return true;
        }
    }

    static class CreateBitmapOp extends Op {
        Context mContext;

        CreateBitmapOp() {
            super("CreateBitmap", "Create a Bitmap");
        }

        void onInit(Context context, boolean foreground) {
            mContext = context;
        }

        boolean onRun() {
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inScreenDensity = DisplayMetrics.DENSITY_DEVICE;
            Bitmap bm = Bitmap.createBitmap(16, 16, Bitmap.Config.ARGB_8888);
            return true;
        }
    }

    static class CreateRecycleBitmapOp extends Op {
        Context mContext;

        CreateRecycleBitmapOp() {
            super("CreateRecycleBitmap", "Create and recycle a Bitmap");
        }

        void onInit(Context context, boolean foreground) {
            mContext = context;
        }

        boolean onRun() {
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inScreenDensity = DisplayMetrics.DENSITY_DEVICE;
            Bitmap bm = Bitmap.createBitmap(16, 16, Bitmap.Config.ARGB_8888);
            bm.recycle();
            return true;
        }
    }

    static class LoadSmallBitmapOp extends Op {
        Context mContext;

        LoadSmallBitmapOp() {
            super("LoadSmallBitmap", "Load small raw bitmap");
        }

        void onInit(Context context, boolean foreground) {
            mContext = context;
        }

        boolean onRun() {
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inScreenDensity = DisplayMetrics.DENSITY_DEVICE;
            Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(),
                    R.drawable.stat_sample, opts);
            return true;
        }
    }

    static class LoadRecycleSmallBitmapOp extends Op {
        Context mContext;

        LoadRecycleSmallBitmapOp() {
            super("LoadRecycleSmallBitmap", "Load and recycle small raw bitmap");
        }

        void onInit(Context context, boolean foreground) {
            mContext = context;
        }

        boolean onRun() {
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inScreenDensity = DisplayMetrics.DENSITY_DEVICE;
            Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(),
                    R.drawable.stat_sample, opts);
            bm.recycle();
            return true;
        }
    }

    static class LoadLargeBitmapOp extends Op {
        Context mContext;

        LoadLargeBitmapOp() {
            super("LoadLargeBitmap", "Load large raw bitmap");
        }

        void onInit(Context context, boolean foreground) {
            mContext = context;
        }

        boolean onRun() {
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inScreenDensity = DisplayMetrics.DENSITY_DEVICE;
            Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(),
                    R.drawable.wallpaper_goldengate, opts);
            return true;
        }
    }

    static class LoadRecycleLargeBitmapOp extends Op {
        Context mContext;

        LoadRecycleLargeBitmapOp() {
            super("LoadRecycleLargeBitmap", "Load and recycle large raw bitmap");
        }

        void onInit(Context context, boolean foreground) {
            mContext = context;
        }

        boolean onRun() {
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inScreenDensity = DisplayMetrics.DENSITY_DEVICE;
            Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(),
                    R.drawable.wallpaper_goldengate, opts);
            bm.recycle();
            return true;
        }
    }

    static class LoadSmallScaledBitmapOp extends Op {
        Context mContext;

        LoadSmallScaledBitmapOp() {
            super("LoadSmallScaledBitmap", "Load small raw bitmap that is scaled for density");
        }

        void onInit(Context context, boolean foreground) {
            mContext = context;
        }

        boolean onRun() {
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inScreenDensity = DisplayMetrics.DENSITY_DEVICE;
            Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(),
                    R.drawable.stat_sample_scale, opts);
            return true;
        }
    }

    static class LoadLargeScaledBitmapOp extends Op {
        Context mContext;

        LoadLargeScaledBitmapOp() {
            super("LoadLargeScaledBitmap", "Load large raw bitmap that is scaled for density");
        }

        void onInit(Context context, boolean foreground) {
            mContext = context;
        }

        boolean onRun() {
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inScreenDensity = DisplayMetrics.DENSITY_DEVICE;
            Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(),
                    R.drawable.wallpaper_goldengate_scale, opts);
            return true;
        }
    }

    static class CreateFileOp extends Op {
        File mFile;

        CreateFileOp() {
            super("CreateFile", "Create and delete a file");
        }

        void onInit(Context context, boolean foreground) {
            mFile = context.getFileStreamPath(foreground ? "test-fg.file" : "test-bg.file");
            mFile.delete();
        }

        boolean onRun() {
            try {
                mFile.createNewFile();
            } catch (IOException e) {
                Log.w(TAG, "Failure creating " + mFile, e);
            }
            mFile.delete();
            return true;
        }
    }

    static class CreateWriteFileOp extends Op {
        File mFile;

        CreateWriteFileOp() {
            super("CreateWriteFile", "Create, write, and delete a file");
        }

        void onInit(Context context, boolean foreground) {
            mFile = context.getFileStreamPath(foreground ? "test-fg.file" : "test-bg.file");
            mFile.delete();
        }

        boolean onRun() {
            try {
                FileOutputStream fos = new FileOutputStream(mFile);
                fos.write(1);
                fos.close();
            } catch (IOException e) {
                Log.w(TAG, "Failure creating " + mFile, e);
            }
            mFile.delete();
            return true;
        }
    }

    static class CreateWriteSyncFileOp extends Op {
        File mFile;

        CreateWriteSyncFileOp() {
            super("CreateWriteSyncFile", "Create, write, sync, and delete a file");
        }

        void onInit(Context context, boolean foreground) {
            mFile = context.getFileStreamPath(foreground ? "test-fg.file" : "test-bg.file");
            mFile.delete();
        }

        boolean onRun() {
            try {
                FileOutputStream fos = new FileOutputStream(mFile);
                fos.write(1);
                fos.flush();
                FileUtils.sync(fos);
                fos.close();
            } catch (IOException e) {
                Log.w(TAG, "Failure creating " + mFile, e);
            }
            mFile.delete();
            return true;
        }
    }

    static class WriteFileOp extends Op {
        File mFile;
        RandomAccessFile mRAF;
        byte[] mBuffer;

        WriteFileOp() {
            super("WriteFile", "Truncate and write a 64k file");
        }

        void onInit(Context context, boolean foreground) {
            mBuffer = new byte[1024*64];
            for (int i=0; i<mBuffer.length; i++) {
                mBuffer[i] = (byte)i;
            }
            mFile = context.getFileStreamPath(foreground ? "test-fg.file" : "test-bg.file");
            mFile.delete();
            try {
                mRAF = new RandomAccessFile(mFile, "rw");
            } catch (FileNotFoundException e) {
                Log.w(TAG, "Failure creating " + mFile, e);
            }
        }

        boolean onRun() {
            try {
                mRAF.seek(0);
                mRAF.setLength(0);
                mRAF.write(mBuffer);
            } catch (IOException e) {
                Log.w(TAG, "Failure writing " + mFile, e);
            }
            return true;
        }

        void onTerm(Context context) {
            try {
                mRAF.close();
            } catch (IOException e) {
                Log.w(TAG, "Failure closing " + mFile, e);
            }
            mFile.delete();
        }
    }

    static class ReadFileOp extends Op {
        File mFile;
        RandomAccessFile mRAF;
        byte[] mBuffer;

        ReadFileOp() {
            super("ReadFile", "Seek and read a 64k file");
        }

        void onInit(Context context, boolean foreground) {
            mBuffer = new byte[1024*64];
            for (int i=0; i<mBuffer.length; i++) {
                mBuffer[i] = (byte)i;
            }
            mFile = context.getFileStreamPath(foreground ? "test-fg.file" : "test-bg.file");
            mFile.delete();
            try {
                mRAF = new RandomAccessFile(mFile, "rw");
                mRAF.seek(0);
                mRAF.setLength(0);
                mRAF.write(mBuffer);
            } catch (IOException e) {
                Log.w(TAG, "Failure creating " + mFile, e);
            }
        }

        boolean onRun() {
            try {
                mRAF.seek(0);
                mRAF.read(mBuffer);
            } catch (IOException e) {
                Log.w(TAG, "Failure reading " + mFile, e);
            }
            return true;
        }

        void onTerm(Context context) {
            try {
                mRAF.close();
            } catch (IOException e) {
                Log.w(TAG, "Failure closing " + mFile, e);
            }
            mFile.delete();
        }
    }
}