Add support for uploading SunSpider results to 0xBenchWeb.
[0xbench:0xbench.git] / src / org / zeroxlab / benchmark / CaseJavascript.java
1 /*
2  * Copyright (C) 2011 0xlab - http://0xlab.org/
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * Authored by Julian Chu <walkingice@0xlab.org>
17  */
18
19 package org.zeroxlab.benchmark;
20
21 import java.util.ArrayList;
22
23 import android.util.Log;
24
25 import android.os.SystemClock;
26
27 import android.app.Activity;
28 import android.content.Context;
29 import android.content.BroadcastReceiver;
30 import android.content.Intent;
31 import android.os.Bundle;
32
33 import java.lang.Float;
34
35 public class CaseJavascript extends Case {
36
37     public static String SUNSPIDER_RESULT = "SUNSPIDER_RESULT";
38     public static String SUNSPIDER_FORMATTED_RESULT = "SUNSPIDER_FORMATTED_RESULT";
39     public static String SUNSPIDER_TOTAL  = "SUNSPIDER_TOTAL";
40
41     public static int sRepeat = 1;
42     public static int sRound  = 1;
43
44     private double mTotal = 0.0;
45
46     protected String[] mJSResults;
47         protected String mFormattedResult;
48     CaseJavascript() {
49         super("CaseJavascript", "org.zeroxlab.benchmark.TesterJavascript", sRepeat, sRound);
50         mType = "msec-js";
51         mTags = new String[]{new String("javascript")};
52     }
53
54     public String getTitle() {
55         return "SunSpider";
56     }
57
58     public String getDescription() {
59         return "This benchmark tests the core JavaScript language only, not the DOM or other browser APIs. It is designed to compare different versions of the same browser, and different browsers to each other.";
60     }
61
62     @Override
63     public void clear() {
64         super.clear();
65         mJSResults = new String[sRepeat];
66     }
67
68     @Override
69     public void reset() {
70         super.reset();
71         mJSResults = new String[sRepeat];
72     }
73
74     @Override
75     public String getResultOutput() {
76         String result = "\n";
77         for (int i = 0; i < mJSResults.length; i++) {
78             result += mJSResults[i];
79             result += "\n";
80         }
81         return result;
82     }
83
84     @Override
85     public ArrayList<Scenario> getScenarios () {
86
87         ArrayList<Scenario> scenarios = new ArrayList<Scenario>();
88                 String results[] = mFormattedResult.split("\n");
89                 for (String result: results) {
90                         String name_time[] = result.split("\t");
91                         String title = getTitle() + ":" + name_time[0];
92                         Log.i(TAG, "JS title: " + title);
93                         Log.i(TAG, "JS time: " + name_time[1]);
94
95             Scenario s = new Scenario(title, mType, mTags);
96                         s.mResults.add( Double.parseDouble(name_time[1]) );
97             scenarios.add(s);
98                 }
99
100         return scenarios;
101     }
102
103     @Override
104     protected boolean saveResult(Intent intent, int index) {
105         String result = intent.getStringExtra(SUNSPIDER_RESULT);
106         mTotal = intent.getDoubleExtra(SUNSPIDER_TOTAL, 0.0);
107
108         if (result == null) {
109             Log.e(TAG, "Weird! cannot find SunSpiderInfo");
110             return false;
111         } else {
112             mJSResults[index] = result;
113         }
114
115         String formatted_result = intent.getStringExtra(SUNSPIDER_FORMATTED_RESULT);
116         if (result == null) {
117             Log.e(TAG, "Weird! cannot find SunSpiderInfo for formatted");
118             return false;
119         } else {
120                         mFormattedResult = formatted_result;
121                 }
122
123         return true;
124     }
125 }