001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client.ui;
017:
018: import com.google.gwt.user.client.Timer;
019: import com.google.gwt.user.client.Window;
020:
021: /**
022: * TODO: document me.
023: */
024: public class TreeProfile extends WidgetProfile {
025:
026: // Using 10*10,20*10,20*20,40*20, 40*40, 80*40,80*80 to get answers. 40*20 and
027: // 80*40
028: // breaks the square pattern because the closest squares are a bit too far off
029: public void testTiming() throws Exception {
030:
031: int[] flatTree = { 50 };
032: timing(flatTree);
033:
034: flatTree[0] = 100;
035: timing(flatTree);
036: flatTree[0] = 200;
037: timing(flatTree);
038: flatTree[0] = 400;
039: timing(flatTree);
040: flatTree[0] = 800;
041: timing(flatTree);
042:
043: int[] bushy = { 2, 2, 2, 2 };
044: timing(bushy);
045: int[] bushy2 = { 2, 2, 2, 2, 2 };
046: timing(bushy2);
047: int[] bushy3 = { 2, 2, 2, 2, 2, 2 };
048: timing(bushy3);
049: int[] bushy4 = { 2, 2, 2, 2, 2, 2, 2 };
050: timing(bushy4);
051: int[] bushy5 = { 2, 2, 2, 2, 2, 2, 2, 2 };
052: timing(bushy5);
053: int[] bushy6 = { 2, 2, 2, 2, 2, 2, 2, 2, 2 };
054: timing(bushy6);
055: int[] bushy7 = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
056: timing(bushy7);
057: throw new Exception("Finished Profile");
058: }
059:
060: public void tastMemory() throws Exception {
061: int[] flatTree = { 100 };
062: for (int i = 0; i < 30; i++) {
063: timing(flatTree);
064: Window.alert("forcing event pump");
065: }
066: throw new Exception("Finished Profile");
067: }
068:
069: static int run = 0;
070:
071: public void timing(final int[] branches) {
072: Timer t = new Timer() {
073: public void run() {
074: ++run;
075: }
076: };
077: createTree(branches);
078: }
079:
080: Tree createTree(int[] branches) {
081: String branchName = "[";
082: for (int i = 0; i < branches.length; i++) {
083: if (i != 0) {
084: branchName += ", ";
085: }
086: branchName += branches[i];
087: }
088: branchName = "Created tree with branches " + branchName + "]";
089: resetTimer();
090: Tree t = new Tree();
091: RootPanel.get().add(t);
092: for (int i = 0; i < branches[0]; i++) {
093: TreeItem item = new TreeItem(branches[0] + "-" + i);
094: t.addItem(item);
095: if (branches.length > 1) {
096: createTreeItem(item, branches, 1);
097: }
098: }
099: timing(branchName);
100: return t;
101: }
102:
103: void createTreeItem(TreeItem branch, int[] branches, int marker) {
104: for (int i = 0; i < branches[marker]; i++) {
105: TreeItem child = new TreeItem();
106: child.setText(branches[marker] + "-" + i);
107: branch.addItem(child);
108: if (marker + 1 < branches.length) {
109: createTreeItem(child, branches, marker + 1);
110: }
111: }
112: }
113:
114: Tree createCheckBoxTree(int[] branches) {
115: String branchName = "[";
116: for (int i = 0; i < branches.length; i++) {
117: if (i != 0) {
118: branchName += ", ";
119: }
120: branchName += branches[i];
121: }
122: branchName = "Created checkbox tree with branches "
123: + branchName + "]";
124: resetTimer();
125: Tree t = new Tree();
126: RootPanel.get().add(t);
127: for (int i = 0; i < branches[0]; i++) {
128: TreeItem item = new TreeItem(branches[0] + "-" + i);
129: t.addItem(item);
130: if (branches.length > 1) {
131: createCheckBoxTreeItem(item, branches, 1);
132: }
133: }
134: timing(branchName);
135: return t;
136: }
137:
138: void createCheckBoxTreeItem(TreeItem branch, int[] branches,
139: int marker) {
140: for (int i = 0; i < branches[marker]; i++) {
141: TreeItem child = new TreeItem(new CheckBox(branches[marker]
142: + "-" + i));
143: branch.addItem(child);
144: if (marker + 1 < branches.length) {
145: createLabelTreeItem(child, branches, marker + 1);
146: }
147: }
148: }
149:
150: Tree createLabelTree(int[] branches) {
151: String branchName = "[";
152: for (int i = 0; i < branches.length; i++) {
153: if (i != 0) {
154: branchName += ", ";
155: }
156: branchName += branches[i];
157: }
158: branchName = "Created label tree with branches " + branchName
159: + "]";
160: resetTimer();
161: Tree t = new Tree();
162: RootPanel.get().add(t);
163: for (int i = 0; i < branches[0]; i++) {
164: TreeItem item = new TreeItem(new Label(branches[0] + "-"
165: + i));
166: t.addItem(item);
167: if (branches.length > 1) {
168:
169: createLabelTreeItem(item, branches, 1);
170: }
171: }
172: timing(branchName);
173: return t;
174: }
175:
176: void createLabelTreeItem(TreeItem branch, int[] branches, int marker) {
177: for (int i = 0; i < branches[marker]; i++) {
178: TreeItem child = new TreeItem(new Label(branches[marker]
179: + "-" + i));
180: branch.addItem(child);
181: if (marker + 1 < branches.length) {
182: createLabelTreeItem(child, branches, marker + 1);
183: }
184: }
185: }
186: }
|