// Path: componentrenderer/src/test/java/de/datenhahn/vaadin/componentrenderer/testbench/util/JmxMemoryUtil.java
// public class JmxMemoryUtil {
//     private ObjectName memory;
//     private MBeanServerConnection remote;
//     private JMXServiceURL target;
// 
//     public JmxMemoryUtil() throws IOException, AttachNotSupportedException, AgentLoadException,
//             AgentInitializationException, AttributeNotFoundException, MBeanException, ReflectionException,
//             InstanceNotFoundException, MalformedObjectNameException {
//         final AttachProvider attachProvider = AttachProvider.providers().get(0);
// 
//         VirtualMachineDescriptor descriptor = null;
//         for (VirtualMachineDescriptor virtualMachineDescriptor : attachProvider.listVirtualMachines()) {
//             descriptor = virtualMachineDescriptor;
//             if (isJettyDescriptor(descriptor)) {
//                 break;
//             }
//         }
// 
//         if (descriptor == null) {
//             throw new RuntimeException("No jetty descriptor found, did you start jetty using mvn jetty:run?");
//         }
// 
//         final VirtualMachine virtualMachine = attachProvider.attachVirtualMachine(descriptor);
//         virtualMachine.loadAgent(System.getProperty("java.home")
//                                  + "/../jre/lib/management-agent.jar", "com.sun.management.jmxremote");
//         final Object
//                 portObject =
//                 virtualMachine.getAgentProperties().get("com.sun.management.jmxremote.localConnectorAddress");
// 
//         target = new JMXServiceURL(portObject + "");
// 
// 
//     }
// 
//     private void reconnect() throws IOException, MalformedObjectNameException {
//         final JMXConnector connector = JMXConnectorFactory.connect(target);
//         remote = connector.getMBeanServerConnection();
//         memory = new ObjectName("java.lang:type=Memory");
//     }
// 
//     public long currentMem() throws AttributeNotFoundException, MBeanException, ReflectionException,
//             InstanceNotFoundException, IOException, MalformedObjectNameException
//     {
//         reconnect();
//         CompositeData cd = (CompositeData) remote.getAttribute(memory, "HeapMemoryUsage");
//         return ((Long) cd.get("used")) / 1024;
//     }
// 
//     public long maxMem() throws AttributeNotFoundException, MBeanException, ReflectionException,
//             InstanceNotFoundException, IOException, MalformedObjectNameException
//     {
//         reconnect();
//         CompositeData cd = (CompositeData) remote.getAttribute(memory, "HeapMemoryUsage");
//         return ((Long) cd.get("max")) / 1024;
//     }
// 
//     public String memString() throws AttributeNotFoundException, MBeanException, ReflectionException,
//             InstanceNotFoundException, IOException, MalformedObjectNameException
//     {
//         return currentMem() + " / " + maxMem() + " used";
//     }
// 
//     public void forceGc() throws ReflectionException, MBeanException, InstanceNotFoundException, IOException,
//             MalformedObjectNameException {
//         reconnect();
//         remote.invoke(memory, "gc", null, null);
//         try {
//             Thread.sleep(100);
//             remote.invoke(memory, "gc", null, null);
//             Thread.sleep(100);
//             remote.invoke(memory, "gc", null, null);
//             Thread.sleep(100);
//         } catch (InterruptedException e) {
//             e.printStackTrace();
//         }
//     }
// 
//     private boolean isJettyDescriptor(VirtualMachineDescriptor virtualMachineDescriptor) {
//         if (virtualMachineDescriptor.displayName()
//                                     .equals("org.codehaus.plexus.classworlds.launcher.Launcher jetty:run"))
//         {
//             System.out.println(virtualMachineDescriptor.displayName());
//             return true;
//         }
//         return false;
//     }
// }


import com.vaadin.testbench.elements.GridElement;
import de.datenhahn.vaadin.componentrenderer.testbench.util.JmxMemoryUtil;
import org.apache.commons.lang3.time.StopWatch;
import org.junit.Test;
import java.io.IOException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.fail;
        System.out.println("  BATCH_SIZE: " + BATCH_SIZE);

        setupOperaDriver();
        getDriver().get("http://localhost:8080/testbench");

        runMemoryTest();
    }

    public void profileInternetExplorer() throws InterruptedException, IOException {
        System.out.println("\n\n=== Test Internet Explorer");
        System.out.println("  BATCH_SIZE: " + BATCH_SIZE);

        setupInternetExplorerDriverDriver();
        getDriver().get("http://localhost:8080/testbench");

        for (int i = 1; i <= 3; i++) {
            runMemoryTest();
        }
    }

    /**
     * This test scrolls MAX_SCROLL lines in BATCH_SIZE lines per scroll.
     * And outputs the memory usage after every scroll. The heap size
     * before and after the scroll is recorded.
     * After the scroll a garbage collection is triggered and it is
     * checked that there is no memory leak (memory which can't be collected).
     */
    private void runMemoryTest() throws IOException {

        try {
// Path: componentrenderer/src/test/java/de/datenhahn/vaadin/componentrenderer/testbench/util/JmxMemoryUtil.java
// public class JmxMemoryUtil {
//     private ObjectName memory;
//     private MBeanServerConnection remote;
//     private JMXServiceURL target;
// 
//     public JmxMemoryUtil() throws IOException, AttachNotSupportedException, AgentLoadException,
//             AgentInitializationException, AttributeNotFoundException, MBeanException, ReflectionException,
//             InstanceNotFoundException, MalformedObjectNameException {
//         final AttachProvider attachProvider = AttachProvider.providers().get(0);
// 
//         VirtualMachineDescriptor descriptor = null;
//         for (VirtualMachineDescriptor virtualMachineDescriptor : attachProvider.listVirtualMachines()) {
//             descriptor = virtualMachineDescriptor;
//             if (isJettyDescriptor(descriptor)) {
//                 break;
//             }
//         }
// 
//         if (descriptor == null) {
//             throw new RuntimeException("No jetty descriptor found, did you start jetty using mvn jetty:run?");
//         }
// 
//         final VirtualMachine virtualMachine = attachProvider.attachVirtualMachine(descriptor);
//         virtualMachine.loadAgent(System.getProperty("java.home")
//                                  + "/../jre/lib/management-agent.jar", "com.sun.management.jmxremote");
//         final Object
//                 portObject =
//                 virtualMachine.getAgentProperties().get("com.sun.management.jmxremote.localConnectorAddress");
// 
//         target = new JMXServiceURL(portObject + "");
// 
// 
//     }
// 
//     private void reconnect() throws IOException, MalformedObjectNameException {
//         final JMXConnector connector = JMXConnectorFactory.connect(target);
//         remote = connector.getMBeanServerConnection();
//         memory = new ObjectName("java.lang:type=Memory");
//     }
// 
//     public long currentMem() throws AttributeNotFoundException, MBeanException, ReflectionException,
//             InstanceNotFoundException, IOException, MalformedObjectNameException
//     {
//         reconnect();
//         CompositeData cd = (CompositeData) remote.getAttribute(memory, "HeapMemoryUsage");
//         return ((Long) cd.get("used")) / 1024;
//     }
// 
//     public long maxMem() throws AttributeNotFoundException, MBeanException, ReflectionException,
//             InstanceNotFoundException, IOException, MalformedObjectNameException
//     {
//         reconnect();
//         CompositeData cd = (CompositeData) remote.getAttribute(memory, "HeapMemoryUsage");
//         return ((Long) cd.get("max")) / 1024;
//     }
// 
//     public String memString() throws AttributeNotFoundException, MBeanException, ReflectionException,
//             InstanceNotFoundException, IOException, MalformedObjectNameException
//     {
//         return currentMem() + " / " + maxMem() + " used";
//     }
// 
//     public void forceGc() throws ReflectionException, MBeanException, InstanceNotFoundException, IOException,
//             MalformedObjectNameException {
//         reconnect();
//         remote.invoke(memory, "gc", null, null);
//         try {
//             Thread.sleep(100);
//             remote.invoke(memory, "gc", null, null);
//             Thread.sleep(100);
//             remote.invoke(memory, "gc", null, null);
//             Thread.sleep(100);
//         } catch (InterruptedException e) {
//             e.printStackTrace();
//         }
//     }
// 
//     private boolean isJettyDescriptor(VirtualMachineDescriptor virtualMachineDescriptor) {
//         if (virtualMachineDescriptor.displayName()
//                                     .equals("org.codehaus.plexus.classworlds.launcher.Launcher jetty:run"))
//         {
//             System.out.println(virtualMachineDescriptor.displayName());
//             return true;
//         }
//         return false;
//     }
// }


// Path: componentrenderer/src/test/java/de/datenhahn/vaadin/componentrenderer/testbench/MemoryIT.java
import com.vaadin.testbench.elements.GridElement;
import de.datenhahn.vaadin.componentrenderer.testbench.util.JmxMemoryUtil;
import org.apache.commons.lang3.time.StopWatch;
import org.junit.Test;
import java.io.IOException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.fail;

        System.out.println("  BATCH_SIZE: " + BATCH_SIZE);

        setupOperaDriver();
        getDriver().get("http://localhost:8080/testbench");

        runMemoryTest();
    }

    public void profileInternetExplorer() throws InterruptedException, IOException {
        System.out.println("\n\n=== Test Internet Explorer");
        System.out.println("  BATCH_SIZE: " + BATCH_SIZE);

        setupInternetExplorerDriverDriver();
        getDriver().get("http://localhost:8080/testbench");

        for (int i = 1; i <= 3; i++) {
            runMemoryTest();
        }
    }

    /**
     * This test scrolls MAX_SCROLL lines in BATCH_SIZE lines per scroll.
     * And outputs the memory usage after every scroll. The heap size
     * before and after the scroll is recorded.
     * After the scroll a garbage collection is triggered and it is
     * checked that there is no memory leak (memory which can't be collected).
     */
    private void runMemoryTest() throws IOException {

        try {
            JmxMemoryUtil jmxMemoryUtil = new JmxMemoryUtil();