Revision: 7357
Author: rj...@google.com
Date: Tue Jan  5 10:30:31 2010
Log: Introduces Microbenchmarks app in references. At the moment there
is only one benchmark, comparing widget creation strategies.

Review by jaimeyap, tbroyer, jgw, jamesr

http://gwt-code-reviews.appspot.com/127801
http://code.google.com/p/google-web-toolkit/source/detail?r=7357

Added:
 /trunk/reference/Microbenchmarks
 /trunk/reference/Microbenchmarks/src
 /trunk/reference/Microbenchmarks/src/com
 /trunk/reference/Microbenchmarks/src/com/google
 /trunk/reference/Microbenchmarks/src/com/google/gwt
 /trunk/reference/Microbenchmarks/src/com/google/gwt/reference
/trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/Microbenchmarks.gwt.xml /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/EmptyBinder.java /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/EmptyBinder.ui.xml /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/Microbenchmark.java /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/Microbenchmarks.java /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/Microbenchmarks.ui.xml /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/TestCursorDomCrawl.java /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/TestDom.java /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/TestDomBinder.java /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/TestDomBinder.ui.xml /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/TestFlows.java /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/TestRealisticDomCrawl.java /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/TestWidget.java /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/TestWidgetBinder.java /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/TestWidgetBinder.ui.xml /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/WidgetCreation.java
 /trunk/reference/Microbenchmarks/war
 /trunk/reference/Microbenchmarks/war/Microbenchmarks.html
 /trunk/reference/Microbenchmarks/war/WEB-INF
 /trunk/reference/Microbenchmarks/war/WEB-INF/web.xml
Deleted:
 /trunk/samples/mail/war/WEB-INF/classes/marker

=======================================
--- /dev/null
+++ /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/Microbenchmarks.gwt.xml Tue Jan 5 10:30:31 2010
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module rename-to='microbenchmarks'>
+  <!-- Inherit the core Web Toolkit stuff.                        -->
+  <inherits name='com.google.gwt.user.User'/>
+
+  <!-- Inherit the default GWT style sheet.  You can change       -->
+  <!-- the theme of your GWT application by uncommenting          -->
+  <!-- any one of the following lines.                            -->
+  <inherits name='com.google.gwt.user.theme.standard.Standard'/>
+  <!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->
+  <!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/>     -->
+
+  <!-- Other module inherits                                      -->
+
+  <!-- Specify the app entry point class.                         -->
+ <entry-point class='com.google.gwt.reference.microbenchmark.client.Microbenchmarks'/>
+
+  <!-- Specify the paths for translatable code                    -->
+  <source path='client'/>
+
+</module>
=======================================
--- /dev/null
+++ /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/EmptyBinder.java Tue Jan 5 10:30:31 2010
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * 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.google.gwt.reference.microbenchmark.client;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.uibinder.client.UiBinder;
+import com.google.gwt.user.client.ui.Composite;
+import com.google.gwt.user.client.ui.Widget;
+
+/**
+ * Creates an empty HTMLPanel via UiBinder.
+ */
+public class EmptyBinder extends Composite {
+
+  public static class Maker extends WidgetCreation.Maker {
+    Maker() {
+      super("HTMLPanel UiBinder");
+    }
+    public Widget make() {
+      return new EmptyBinder();
+    }
+  }
+  interface Binder extends UiBinder<Widget, EmptyBinder> {}
+
+  private static final Binder BINDER = GWT.create(Binder.class);
+
+  private EmptyBinder() {
+    initWidget(BINDER.createAndBindUi(this));
+  }
+}
=======================================
--- /dev/null
+++ /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/EmptyBinder.ui.xml Tue Jan 5 10:30:31 2010
@@ -0,0 +1,4 @@
+<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
+    xmlns:gwt='urn:import:com.google.gwt.user.client.ui'>
+    <gwt:HTMLPanel/>
+</ui:UiBinder>
=======================================
--- /dev/null
+++ /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/Microbenchmark.java Tue Jan 5 10:30:31 2010
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * 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.google.gwt.reference.microbenchmark.client;
+
+import com.google.gwt.user.client.ui.Widget;
+
+/**
+ * Implemented by the Microbenchmarks run from {...@link Microbenchmarks}.
+ */
+public interface Microbenchmark {
+  /**
+   * @return The user visible name of this benchmark
+   */
+  String getName();
+
+  /**
+   * @return The widget to display for this benchmark
+   */
+  Widget getWidget();
+
+  /**
+   * Run the benchmark.
+   */
+  void run();
+}
=======================================
--- /dev/null
+++ /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/Microbenchmarks.java Tue Jan 5 10:30:31 2010
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * 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.google.gwt.reference.microbenchmark.client;
+
+import com.google.gwt.core.client.EntryPoint;
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.event.dom.client.ChangeEvent;
+import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.uibinder.client.UiBinder;
+import com.google.gwt.uibinder.client.UiField;
+import com.google.gwt.uibinder.client.UiHandler;
+import com.google.gwt.user.client.Command;
+import com.google.gwt.user.client.DeferredCommand;
+import com.google.gwt.user.client.ui.Button;
+import com.google.gwt.user.client.ui.DeckPanel;
+import com.google.gwt.user.client.ui.ListBox;
+import com.google.gwt.user.client.ui.RootPanel;
+import com.google.gwt.user.client.ui.UIObject;
+import com.google.gwt.user.client.ui.Widget;
+
+/**
+ * Offers up a selection of {...@link Microbenchmark} implementations to run.
+ */
+public class Microbenchmarks implements EntryPoint {
+
+  interface Binder extends UiBinder<Widget, Microbenchmarks> {}
+  private static final Binder BINDER = GWT.create(Binder.class);
+
+  // Add entries for new benchmarks here.
+  private final Microbenchmark[] benchmarks = {
+      new WidgetCreation()
+  };
+
+  @UiField ListBox listBox;
+  @UiField DeckPanel deck;
+  @UiField Button button;
+  @UiField Element running;
+
+  @UiHandler("listBox")
+  public void onChange(@SuppressWarnings("unused") ChangeEvent ignored) {
+    int index = listBox.getSelectedIndex();
+    deck.showWidget(index);
+  }
+
+  @UiHandler("button")
+  public void onClick(@SuppressWarnings("unused") ClickEvent ignored) {
+    final int index = listBox.getSelectedIndex();
+    UIObject.setVisible(running, true);
+    button.setEnabled(false);
+    DeferredCommand.addCommand(new Command() {
+      public void execute() {
+        benchmarks[index].run();
+        UIObject.setVisible(running, false);
+        button.setEnabled(true);
+      }
+    });
+  }
+
+  public void onModuleLoad() {
+    Widget root = BINDER.createAndBindUi(this);
+
+    for (Microbenchmark benchmark : benchmarks) {
+      listBox.addItem(benchmark.getName());
+      deck.add(benchmark.getWidget());
+    }
+
+    deck.showWidget(0);
+    RootPanel.get().add(root);
+  }
+}
=======================================
--- /dev/null
+++ /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/Microbenchmarks.ui.xml Tue Jan 5 10:30:31 2010
@@ -0,0 +1,16 @@
+<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
+  xmlns:gwt='urn:import:com.google.gwt.user.client.ui'>
+
+  <gwt:HTMLPanel>
+    <div style='margin-left:1em; margin-top:1em;'>
+      Select Benchmark: <gwt:ListBox ui:field='listBox'/>
+      <gwt:Button ui:field='button'>Run</gwt:Button>
+      <span style="display:none; color:gray; font-style:oblique"
+        ui:field='running'>Running...</span>
+    </div>
+    <br/><hr/><br/>
+    <div style='margin-left:1em;'>
+      <gwt:DeckPanel ui:field='deck'/>
+    </div>
+  </gwt:HTMLPanel>
+</ui:UiBinder>
=======================================
--- /dev/null
+++ /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/TestCursorDomCrawl.java Tue Jan 5 10:30:31 2010
@@ -0,0 +1,50 @@
+package com.google.gwt.reference.microbenchmark.client;
+
+import com.google.gwt.dom.client.DivElement;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.SpanElement;
+import com.google.gwt.user.client.ui.Widget;
+
+/**
+ * Run by {...@link WidgetCreation}, see {...@link Maker#name} for details.
+ */
+public class TestCursorDomCrawl extends Widget {
+  public static class Maker extends WidgetCreation.Maker {
+    Maker() {
+ super("Complex UI via innerHTML, no widgets, get children by idealized crawl");
+    }
+    public Widget make() {
+      return new TestCursorDomCrawl();
+    }
+  }
+
+  Element elm;
+  DivElement div1;
+  DivElement div2;
+  DivElement div3;
+  DivElement div4;
+  SpanElement span1;
+  SpanElement span2;
+
+  private TestCursorDomCrawl() {
+    Element root = TestDom.fromHtml(TestDom.HTML);
+
+    Element cursor = root;
+    div1 = (cursor = cursor.getFirstChildElement()).cast();
+    assert div1.getId().equals("div1");
+    div2 = (cursor = cursor.getFirstChildElement()).cast();
+    assert div2.getId().equals("div2");
+    span1 = (cursor = cursor.getNextSiblingElement()).cast();
+    assert span1.getId().equals("span1");
+
+    cursor = div1.getNextSiblingElement();
+    div3 = (cursor = cursor.getFirstChildElement()).cast();
+    assert div3.getId().equals("div3");
+    div4 = (cursor = cursor.getFirstChildElement()).cast();
+    assert div4.getId().equals("div4");
+    span2 = (cursor = cursor.getNextSiblingElement()).cast();
+    assert span2.getId().equals("span2");
+
+    setElement(root);
+  }
+}
=======================================
--- /dev/null
+++ /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/TestDom.java Tue Jan 5 10:30:31 2010
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * 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.google.gwt.reference.microbenchmark.client;
+
+import com.google.gwt.dom.client.DivElement;
+import com.google.gwt.dom.client.Document;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.SpanElement;
+import com.google.gwt.user.client.ui.Widget;
+
+/**
+ * Run by {...@link WidgetCreation}, see {...@link Maker#name} for details.
+ */
+public class TestDom extends Widget {
+  public static class Maker extends WidgetCreation.Maker {
+    Maker() {
+      super("Complex UI via innerHTML, no widgets, get children by id");
+    }
+    public Widget make() {
+      return new TestDom();
+    }
+  }
+
+  static final String HTML = "<div>"
+    +   "<div id='div1'>"
+    +     "<div id='div2'></div>"
+    +     "<span id='span1'></span>"
+    +   "</div>"
+    +   "<div>"
+    +     "<div id='div3'>"
+    +       "<div id='div4'></div>"
+    +       "<span id='span2'></span>"
+    +     "</div>"
+    +   "</div>"
+    + "</div>";
+
+ private static final DivElement detachedDiv = Document.get().createDivElement();
+
+  static Element fromHtml(String html) {
+    detachedDiv.setInnerHTML(html);
+    Element e = detachedDiv.getFirstChildElement();
+    e.getParentElement().removeChild(e);
+    return e;
+  }
+
+  Element root;
+  DivElement div1;
+  DivElement div2;
+  DivElement div3;
+
+  DivElement div4;
+  SpanElement span1;
+
+  SpanElement span2;
+
+  private TestDom() {
+    root = fromHtml(HTML);
+
+    Document.get().getBody().appendChild(root);
+    div1 = Document.get().getElementById("div1").cast();
+    div2 = Document.get().getElementById("div2").cast();
+    div3 = Document.get().getElementById("div3").cast();
+    div4 = Document.get().getElementById("div4").cast();
+    span1 = Document.get().getElementById("span1").cast();
+    span2 = Document.get().getElementById("span2").cast();
+
+    Document.get().getBody().removeChild(root);
+    div1.removeAttribute("id");
+    div2.removeAttribute("id");
+    div3.removeAttribute("id");
+    div4.removeAttribute("id");
+    span1.removeAttribute("id");
+    span2.removeAttribute("id");
+
+    setElement(root);
+  }
+}
=======================================
--- /dev/null
+++ /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/TestDomBinder.java Tue Jan 5 10:30:31 2010
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * 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.google.gwt.reference.microbenchmark.client;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.dom.client.DivElement;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.SpanElement;
+import com.google.gwt.uibinder.client.UiBinder;
+import com.google.gwt.uibinder.client.UiField;
+import com.google.gwt.user.client.ui.Widget;
+
+/**
+ * Run by {...@link WidgetCreation}, see {...@link Maker#name} for details.
+ */
+public class TestDomBinder extends Widget {
+
+  public static class Maker extends WidgetCreation.Maker {
+    Maker() {
+      super("Complex UI via UiBinder, no widgets");
+    }
+    public Widget make() {
+      return new TestDomBinder();
+    }
+  }
+  interface Binder extends UiBinder<Element, TestDomBinder> {}
+  @UiField DivElement div1;
+  @UiField DivElement div2;
+  @UiField DivElement div3;
+  @UiField DivElement div4;
+
+  @UiField SpanElement span1;
+  @UiField SpanElement span2;
+
+  private static final Binder BINDER = GWT.create(Binder.class);
+
+  private TestDomBinder() {
+    setElement(BINDER.createAndBindUi(this));
+  }
+}
=======================================
--- /dev/null
+++ /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/TestDomBinder.ui.xml Tue Jan 5 10:30:31 2010
@@ -0,0 +1,16 @@
+<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'>
+      <div>
+        <div ui:field='div1'>
+          <div ui:field='div2'>
+          </div>
+          <span ui:field='span1'/>
+        </div>
+        <div>
+        <div ui:field='div3'>
+          <div ui:field='div4'>
+          </div>
+          <span ui:field='span2'/>
+        </div>
+      </div>
+      </div>
+</ui:UiBinder>
=======================================
--- /dev/null
+++ /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/TestFlows.java Tue Jan 5 10:30:31 2010
@@ -0,0 +1,42 @@
+package com.google.gwt.reference.microbenchmark.client;
+
+import com.google.gwt.user.client.ui.Composite;
+import com.google.gwt.user.client.ui.FlowPanel;
+import com.google.gwt.user.client.ui.Label;
+import com.google.gwt.user.client.ui.Widget;
+
+/**
+ * Run by {...@link WidgetCreation}, see {...@link Maker#name} for details.
+ */
+public class TestFlows extends Composite {
+  public static class Maker extends WidgetCreation.Maker {
+    Maker() {
+      super("Complex UI via FlowPanels (DIVs) and Labels (SPANs)");
+    }
+    public Widget make() {
+      return new TestFlows();
+    }
+  }
+
+  private TestFlows() {
+    FlowPanel root = new FlowPanel();
+    FlowPanel div1 = new FlowPanel();
+    FlowPanel div2 = new FlowPanel();
+    FlowPanel child2 = new FlowPanel();
+    FlowPanel div3 = new FlowPanel();
+    FlowPanel div4 = new FlowPanel();
+    Label span1 = new Label();
+    Label span2 = new Label();
+
+    div1.add(div2);
+    div1.add(span1);
+    root.add(div1);
+
+    child2.add(div3);
+    div3.add(div4);
+    div3.add(span2);
+    root.add(child2);
+
+    initWidget(root);
+  }
+}
=======================================
--- /dev/null
+++ /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/TestRealisticDomCrawl.java Tue Jan 5 10:30:31 2010
@@ -0,0 +1,49 @@
+package com.google.gwt.reference.microbenchmark.client;
+
+import com.google.gwt.dom.client.DivElement;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.SpanElement;
+import com.google.gwt.user.client.ui.Widget;
+
+/**
+ * Run by {...@link WidgetCreation}, see {...@link Maker#name} for details.
+ */
+public class TestRealisticDomCrawl extends Widget {
+  public static class Maker extends WidgetCreation.Maker {
+    Maker() {
+ super("Complex UI via innerHTML, no widgets, get children by nav from root");
+    }
+    public Widget make() {
+      return new TestRealisticDomCrawl();
+    }
+  }
+
+  Element elm;
+  DivElement div1;
+  DivElement div2;
+  DivElement div3;
+  DivElement div4;
+  SpanElement span1;
+
+  SpanElement span2;
+
+  private TestRealisticDomCrawl() {
+    Element root = TestDom.fromHtml(TestDom.HTML);
+
+    div1 = root.getFirstChildElement().cast();
+    assert div1.getId().equals("div1");
+    div2 = root.getFirstChildElement().getFirstChildElement().cast();
+    assert div2.getId().equals("div2");
+ span1 = root.getFirstChildElement().getFirstChildElement().getNextSiblingElement().cast();
+    assert span1.getId().equals("span1");
+
+ div3 = root.getFirstChildElement().getNextSiblingElement().getFirstChildElement().cast();
+    assert div3.getId().equals("div3");
+ div4 = root.getFirstChildElement().getNextSiblingElement().getFirstChildElement().getFirstChildElement().cast();
+    assert div4.getId().equals("div4");
+ span2 = root.getFirstChildElement().getNextSiblingElement().getFirstChildElement().getFirstChildElement().getNextSiblingElement().cast();
+    assert span2.getId().equals("span2");
+
+    setElement(root);
+  }
+}
=======================================
--- /dev/null
+++ /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/TestWidget.java Tue Jan 5 10:30:31 2010
@@ -0,0 +1,41 @@
+package com.google.gwt.reference.microbenchmark.client;
+
+import com.google.gwt.dom.client.DivElement;
+import com.google.gwt.dom.client.SpanElement;
+import com.google.gwt.user.client.ui.Composite;
+import com.google.gwt.user.client.ui.HTMLPanel;
+import com.google.gwt.user.client.ui.Widget;
+
+/**
+ * Run by {...@link WidgetCreation}, see {...@link Maker#name} for details.
+ */
+public class TestWidget extends Composite {
+  public static class Maker extends WidgetCreation.Maker {
+    Maker() {
+      super("Complex UI via typical manual HTMLPanel usage");
+    }
+    public Widget make() {
+      return new TestWidget();
+    }
+  }
+  DivElement div1;
+  DivElement div2;
+  DivElement div3;
+  DivElement div4;
+  SpanElement span1;
+
+
+  SpanElement span2;
+
+  private TestWidget() {
+    HTMLPanel p = new HTMLPanel(TestDom.HTML);
+    initWidget(p);
+
+    div1 = p.getElementById("div1").cast();
+    div2 = p.getElementById("div2").cast();
+    div3 = p.getElementById("div3").cast();
+    div4 = p.getElementById("div4").cast();
+    span1 = p.getElementById("span1").cast();
+    span2 = p.getElementById("span2").cast();
+  }
+}
=======================================
--- /dev/null
+++ /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/TestWidgetBinder.java Tue Jan 5 10:30:31 2010
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * 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.google.gwt.reference.microbenchmark.client;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.dom.client.DivElement;
+import com.google.gwt.dom.client.SpanElement;
+import com.google.gwt.uibinder.client.UiBinder;
+import com.google.gwt.uibinder.client.UiField;
+import com.google.gwt.user.client.ui.Composite;
+import com.google.gwt.user.client.ui.Widget;
+
+/**
+ * Run by {...@link WidgetCreation}, see {...@link Maker#name} for details.
+ */
+public class TestWidgetBinder extends Composite {
+  public static class Maker extends WidgetCreation.Maker {
+    Maker() {
+      super("Complex UI with HTMLPanel via UiBinder");
+    }
+    public Widget make() {
+      return new TestWidgetBinder();
+    }
+  }
+  interface Binder extends UiBinder<Widget, TestWidgetBinder> {}
+  @UiField DivElement div1;
+  @UiField DivElement div2;
+  @UiField DivElement div3;
+  @UiField DivElement div4;
+
+  @UiField SpanElement span1;
+  @UiField SpanElement span2;
+
+  private static final Binder BINDER = GWT.create(Binder.class);
+
+  private TestWidgetBinder() {
+    initWidget(BINDER.createAndBindUi(this));
+  }
+}
=======================================
--- /dev/null
+++ /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/TestWidgetBinder.ui.xml Tue Jan 5 10:30:31 2010
@@ -0,0 +1,19 @@
+<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
+  xmlns:gwt='urn:import:com.google.gwt.user.client.ui'>
+  <gwt:HTMLPanel>
+    <div>
+      <div ui:field='div1'>
+        <div ui:field='div2'>
+        </div>
+        <span ui:field='span1' />
+      </div>
+      <div>
+        <div ui:field='div3'>
+          <div ui:field='div4'>
+          </div>
+          <span ui:field='span2' />
+        </div>
+      </div>
+    </div>
+  </gwt:HTMLPanel>
+</ui:UiBinder>
=======================================
--- /dev/null
+++ /trunk/reference/Microbenchmarks/src/com/google/gwt/reference/microbenchmark/client/WidgetCreation.java Tue Jan 5 10:30:31 2010
@@ -0,0 +1,219 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * 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.google.gwt.reference.microbenchmark.client;
+
+import com.google.gwt.core.client.Duration;
+import com.google.gwt.dom.client.Document;
+import com.google.gwt.event.dom.client.BlurEvent;
+import com.google.gwt.event.dom.client.BlurHandler;
+import com.google.gwt.i18n.client.NumberFormat;
+import com.google.gwt.user.client.Cookies;
+import com.google.gwt.user.client.Window;
+import com.google.gwt.user.client.Window.ClosingEvent;
+import com.google.gwt.user.client.Window.ClosingHandler;
+import com.google.gwt.user.client.ui.FlowPanel;
+import com.google.gwt.user.client.ui.Grid;
+import com.google.gwt.user.client.ui.HTMLPanel;
+import com.google.gwt.user.client.ui.RootPanel;
+import com.google.gwt.user.client.ui.SimplePanel;
+import com.google.gwt.user.client.ui.TextBox;
+import com.google.gwt.user.client.ui.Widget;
+
+import java.util.Date;
+
+/**
+ * Compares various widget creation strategies.
+ */
+public class WidgetCreation implements Microbenchmark {
+  static abstract class Maker {
+    final String name;
+
+    Maker(String name) {
+      this.name = name;
+    }
+
+    abstract Widget make();
+  }
+
+  private static final String COOKIE = "gwt_microb_widgetCreation";
+
+  private static final int DEFAULT_INSTANCES = 100;
+
+  public static native void log(String msg) /*-{
+    var logger = $wnd.console;
+    if(logger && logger.markTimeline) {
+      logger.markTimeline(msg);
+    }
+  }-*/;
+
+  final Grid grid;
+
+  final TextBox number;
+  final Maker[] makers = {
+      new Maker("SimplePanel") {
+        public Widget make() {
+          return new SimplePanel();
+        }
+      }, new Maker("FlowPanel") {
+        public Widget make() {
+          return new FlowPanel();
+        }
+      }, new Maker("HTMLPanel") {
+        public Widget make() {
+          return new HTMLPanel("");
+        }
+      }, new EmptyBinder.Maker(), new TestDom.Maker(),
+      new TestCursorDomCrawl.Maker(), new TestRealisticDomCrawl.Maker(),
+ new TestDomBinder.Maker(), new TestFlows.Maker(), new TestWidget.Maker(),
+      new TestWidgetBinder.Maker()};
+
+  final private FlowPanel root;
+
+  public WidgetCreation() {
+    int instances = DEFAULT_INSTANCES;
+    try {
+      instances = Integer.parseInt(Cookies.getCookie(COOKIE));
+    } catch (NumberFormatException ignored) {
+    }
+
+    number = new TextBox();
+    number.setVisibleLength(7);
+    number.setValue("" + instances);
+    number.addBlurHandler(new BlurHandler() {
+      public void onBlur(BlurEvent event) {
+        saveInstances();
+      }
+    });
+
+    Window.addWindowClosingHandler(new ClosingHandler() {
+      public void onWindowClosing(ClosingEvent event) {
+        saveInstances();
+      }
+    });
+
+    grid = new Grid(makers.length + 1, 3);
+    grid.setText(0, 0, "50%");
+    grid.setText(0, 1, "m");
+
+    int row = 1;
+    for (Maker m : makers) {
+      grid.setText(row, 0, "0");
+      grid.setText(row, 1, "0");
+      grid.setText(row, 2, m.name);
+      row++;
+    }
+
+    root = new FlowPanel();
+    HTMLPanel l = new HTMLPanel(
+        "<b>Time for creating, attaching and detaching "
+            + "<span id='number'></span> instances, in MS<br><br></b>");
+    l.addAndReplaceElement(number, "number");
+    root.add(l);
+    root.add(grid);
+  }
+
+  public String getName() {
+    return "Widget Creation Survey";
+  }
+
+  public Widget getWidget() {
+    return root;
+  }
+
+  public void run() {
+    RootPanel r = RootPanel.get();
+
+    Widget[] widgets = new Widget[getInstances()];
+
+    grid.resizeColumns(grid.getColumnCount() + 1);
+
+    int row = 1;
+    for (Maker maker : makers) {
+      log(maker.name);
+      double start = Duration.currentTimeMillis();
+
+      for (int i = 0; i < getInstances(); ++i) {
+        widgets[i] = maker.make();
+        r.add(widgets[i]);
+      }
+
+      /*
+       * Force a layout by finding the body's offsetTop. We avoid doing
+       * setTimeout(0), which would allow paint to happen, to keep the test
+ * synchronous and because different browsers round that zero to different
+       * minimums. Layout should be the bulk of the time.
+       */
+      Document.get().getBody().getOffsetTop();
+
+      double thisTime = Duration.currentTimeMillis() - start;
+      record(row, thisTime);
+
+      // Clean up to keep the dom a reasonable size.
+
+      for (int i = 0; i < getInstances(); ++i) {
+        r.remove(widgets[i]);
+      }
+      row++;
+    }
+  }
+
+  private String format(double median) {
+    return NumberFormat.getFormat("0").format(median);
+  }
+
+  private int getInstances() {
+    try {
+      int instances = Integer.parseInt(number.getValue());
+      return instances;
+    } catch (NumberFormatException ignored) {
+      return 0;
+    }
+  }
+
+  private void record(int row, double thisTime) {
+    final int columns = grid.getColumnCount();
+    grid.setText(row, columns - 1, format(thisTime));
+
+    double max = 0, min = 0, mean = 0;
+
+    for (int column = 3; column < columns; column++) {
+      double value = Double.parseDouble(grid.getText(row, column));
+      mean += value;
+      max = Math.max(max, value);
+      if (min == 0) {
+        min = max;
+      } else {
+        min = Math.min(min, value);
+      }
+    }
+
+    double range = max - min;
+    double halfRange = range / 2;
+    double median = min + halfRange;
+    grid.setText(row, 0, format(median));
+
+    mean = mean / (columns - 3);
+    grid.setText(row, 1, format(mean));
+  }
+
+  @SuppressWarnings("deprecation")
+  private void saveInstances() {
+    String value = number.getValue();
+    Date expires = new Date();
+    expires.setYear(expires.getYear() + 3);
+    Cookies.setCookie(COOKIE, value, expires);
+  }
+}
=======================================
--- /dev/null
+++ /trunk/reference/Microbenchmarks/war/Microbenchmarks.html Tue Jan 5 10:30:31 2010
@@ -0,0 +1,38 @@
+<!DOCTYPE>
+
+<html>
+  <head>
+    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
+
+    <!--                                           -->
+    <!-- Any title is fine                         -->
+    <!--                                           -->
+    <title>Microbenchmarks</title>
+
+    <!--                                           -->
+    <!-- This script loads your compiled module.   -->
+    <!-- If you add any GWT meta tags, they must   -->
+    <!-- be added before this line.                -->
+    <!--                                           -->
+ <script type="text/javascript" language="javascript" src="microbenchmarks/microbenchmarks.nocache.js"></script>
+  </head>
+
+  <!--                                           -->
+  <!-- The body can have arbitrary html, or      -->
+  <!-- you can leave the body empty if you want  -->
+  <!-- to create a completely dynamic UI.        -->
+  <!--                                           -->
+  <body>
+
+    <!-- OPTIONAL: include this if you want history support -->
+ <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
+
+ <!-- RECOMMENDED if your web app will not function without JavaScript enabled -->
+    <noscript>
+ <div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">
+        Your web browser must have JavaScript enabled
+        in order for this application to display correctly.
+      </div>
+    </noscript>
+  </body>
+</html>
=======================================
--- /dev/null
+++ /trunk/reference/Microbenchmarks/war/WEB-INF/web.xml Tue Jan 5 10:30:31 2010
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app>
+
+  <!-- Default page to serve -->
+  <welcome-file-list>
+    <welcome-file>Microbenchmarks.html</welcome-file>
+  </welcome-file-list>
+
+</web-app>
-- 
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to