package org.teleal.cling.test;

import org.teleal.cling.binding.xml.DeviceDescriptorBinder;
import org.teleal.cling.binding.xml.ServiceDescriptorBinder;
import org.teleal.cling.binding.xml.UDA10DeviceDescriptorBinderImpl;
import org.teleal.cling.binding.xml.UDA10DeviceDescriptorBinderSAXImpl;
import org.teleal.cling.binding.xml.UDA10ServiceDescriptorBinderImpl;
import org.teleal.cling.model.meta.RemoteDevice;
import org.teleal.cling.model.meta.RemoteDeviceIdentity;
import org.teleal.cling.model.meta.RemoteService;
import org.teleal.cling.model.types.UDN;
import org.teleal.common.io.IO;

import java.io.FileInputStream;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;

/**
 * @author Christian Bauer
 */
public class PerformanceTest {

    public static void main(String[] args) throws Exception {

        InputStream is = new FileInputStream("/Users/cb/software/cling/trunk/core/src/test/resources/test-dvc-uda10.xml");
        final String xml = IO.readLines(is);
        is.close();


        DeviceDescriptorBinder binder = new UDA10DeviceDescriptorBinderSAXImpl();

        long start = System.currentTimeMillis();
        for(int i = 0; i < 1000; i++) {
            RemoteDevice device = new RemoteDevice(createRemoteDeviceIdentity(180));
            device = binder.describe(device, xml);
        }

        System.out.println("TIME: " + (System.currentTimeMillis() - start));

        System.out.println("#############################################");

        binder = new UDA10DeviceDescriptorBinderImpl();

        start = System.currentTimeMillis();
        for(int i = 0; i < 1000; i++) {
            RemoteDevice device = new RemoteDevice(createRemoteDeviceIdentity(180));
            device = binder.describe(device, xml);
        }

        System.out.println("TIME: " + (System.currentTimeMillis() - start));
/*
        final DeviceDescriptorBinder binder = new UDA10DeviceDescriptorBinderImpl2();

        long start = System.currentTimeMillis();
        for(int i = 0; i < 100; i++) {
            System.out.println(" - " + i);
            RemoteDevice device = new RemoteDevice(createRemoteDeviceIdentity(180));
            device = binder.describe(device, xml);
        }

        System.out.println("TIME: " + (System.currentTimeMillis() - start));

        final ServiceDescriptorBinder binder = new UDA10ServiceDescriptorBinderImpl2();

        long start = System.currentTimeMillis();
        for(int i = 0; i < 100; i++) {
            System.out.println(" - " + i);
            RemoteService svc = binder.read(RemoteService.class, xml);
        }

        System.out.println("TIME: " + (System.currentTimeMillis() - start));
*/
    }

    public static RemoteDeviceIdentity createRemoteDeviceIdentity(int maxAgeSeconds)  throws Exception {
        RemoteDeviceIdentity rootIdentity = null;
        try {
            rootIdentity = new RemoteDeviceIdentity(
                    UDN.uniqueSystemIdentifier("test"),
                    maxAgeSeconds,
                    new URL("http://127.0.0.1/"),
                    null,
                    InetAddress.getByName("127.0.0.1")
            );
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
        return rootIdentity;
    }

}
