/*
 * This file is part of the OpenNMS(R) Application.
 *
 * OpenNMS(R) is Copyright (C) 2011 The OpenNMS Group, Inc.  All rights reserved.
 * OpenNMS(R) is a derivative work, containing both original code, included code and modified
 * code that was published under the GNU General Public License. Copyrights for modified
 * and included code are below.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * For more information contact:
 * OpenNMS Licensing       <license@opennms.org>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 */
package org.opennms.netmgt.eventd;

import java.net.InetSocketAddress;
import java.net.UnknownHostException;

import org.opennms.core.utils.InetAddressUtils;
import org.opennms.netmgt.mock.OpenNMSTestCase;
import org.opennms.netmgt.model.events.EventBuilder;
import org.opennms.netmgt.model.events.EventProxy;
import org.opennms.netmgt.model.events.EventProxyException;
import org.opennms.netmgt.utils.TcpEventProxy;


/**
 * MemoryLeakTest
 *
 * @author brozow
 */
public class MemoryLeakTest extends OpenNMSTestCase {

    public void xxxTestMemory() throws Exception {
        EventProxy proxy = new TcpEventProxy(new InetSocketAddress(InetAddressUtils.addr("127.0.0.1"), 5837));
        double eventRate = 10.0 / 1000.0;
        
        long start = System.currentTimeMillis();
        long count = 0;
        while(true) {
            long now = Math.max(System.currentTimeMillis(), 1);
            double actualRate = ((double)count) / ((double)(now - start));
            if (actualRate < eventRate) {
                sendEvent(proxy, count);
                count++;
            }
            Thread.sleep(10);
            System.err.println(String.format("Expected Rate: %f Actual Rate: %f Events Sent: %d", eventRate, actualRate, count));
        }
        
    }

    private void sendEvent(EventProxy proxy, long count) throws Exception {
        EventBuilder bldr = new EventBuilder("uei.opennms.org/internal/authentication/successfulLogin", "MemoryLeakTest");
        bldr.addParam("user", "brozow");
        
        proxy.send(bldr.getEvent());
    }
    


}

@@ -1361,94 +1361,8 @@
 ss;%0A
-import java.net.UnknownHostException;%0A%0Aimport org.opennms.core.utils.InetAddressUtils;
 %0Aimp
@@ -1514,68 +1514,8 @@
 xy;%0A
-import org.opennms.netmgt.model.events.EventProxyException;%0A
 impo
@@ -1659,16 +1659,73 @@
 stCase %7B
+%0A    %0A    private static final long MINS = 8*60*60*1000L;
 %0A%0A    pu
@@ -1737,17 +1737,17 @@
 void xxx
-T
+t
 estMemor
@@ -1840,30 +1840,8 @@
 ess(
-InetAddressUtils.addr(
 %22127
@@ -1851,15 +1851,36 @@
 0.1%22
-)
 , 
-5837
+OpenNMSTestCase.PROXY_PORT
 ));%0A
@@ -1906,17 +1906,17 @@
 tRate = 
-1
+2
 0.0 / 10
@@ -2021,12 +2021,49 @@
 ile(
-true
+System.currentTimeMillis() - start %3C MINS
 ) %7B%0A
@@ -2348,17 +2348,17 @@
 d.sleep(
-1
+3
 0);%0A    
@@ -2791,24 +2791,356 @@
 etEvent());%0A
+%0A        long free = Runtime.getRuntime().freeMemory();%0A        long max = Runtime.getRuntime().maxMemory();%0A        %0A        double pct = ((double)free)/((double)max);%0A        System.err.println(%22%25 Free Memory is %22+pct);%0A        %0A        if (pct %3C 0.01) %7B%0A            throw new IllegalStateException(%22Memory Used up!%22);%0A        %7D%0A%0A
     %7D%0A    %0A%0A

update for improved performance