001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.turbo;
015
016import java.io.File;
017import java.io.IOException;
018
019import ch.qos.logback.core.testUtil.EnvUtilForTests;
020import org.junit.Before;
021import org.junit.Ignore;
022import org.junit.Test;
023
024import ch.qos.logback.classic.ClassicTestConstants;
025import ch.qos.logback.classic.Logger;
026import ch.qos.logback.classic.LoggerContext;
027import ch.qos.logback.classic.issue.lbclassic135.LoggingRunnable;
028import ch.qos.logback.classic.joran.JoranConfigurator;
029import ch.qos.logback.core.contention.MultiThreadedHarness;
030import ch.qos.logback.core.contention.RunnableWithCounterAndDone;
031import ch.qos.logback.core.joran.spi.JoranException;
032
033@Ignore
034public class ReconfigurePerf {
035    final static int THREAD_COUNT = 500;
036    // final static int LOOP_LEN = 1000 * 1000;
037
038    // the space in the file name mandated by
039    // http://jira.qos.ch/browse/LBCORE-119
040    final static String CONF_FILE_AS_STR = ClassicTestConstants.INPUT_PREFIX + "turbo/scan_perf.xml";
041
042    // it actually takes time for Windows to propagate file modification changes
043    // values below 100 milliseconds can be problematic the same propagation
044    // latency occurs in Linux but is even larger (>600 ms)
045    final static int DEFAULT_SLEEP_BETWEEN_UPDATES = 110;
046
047    int sleepBetweenUpdates = DEFAULT_SLEEP_BETWEEN_UPDATES;
048
049    static int numberOfCycles = 100;
050    static int totalTestDuration;
051
052    LoggerContext loggerContext = new LoggerContext();
053    Logger logger = loggerContext.getLogger(this.getClass());
054    MultiThreadedHarness harness;
055
056    @Before
057    public void setUp() {
058        // take into account propagation latency occurs on Linux
059        if (EnvUtilForTests.isLinux()) {
060            sleepBetweenUpdates = 850;
061            totalTestDuration = sleepBetweenUpdates * numberOfCycles;
062        } else {
063            totalTestDuration = sleepBetweenUpdates * numberOfCycles * 2;
064        }
065        harness = new MultiThreadedHarness(totalTestDuration);
066    }
067
068    void configure(File file) throws JoranException {
069        JoranConfigurator jc = new JoranConfigurator();
070        jc.setContext(loggerContext);
071        jc.doConfigure(file);
072    }
073
074    RunnableWithCounterAndDone[] buildRunnableArray() {
075        RunnableWithCounterAndDone[] rArray = new RunnableWithCounterAndDone[THREAD_COUNT];
076        for (int i = 0; i < THREAD_COUNT; i++) {
077            rArray[i] = new LoggingRunnable(logger);
078        }
079        return rArray;
080    }
081
082    // Tests whether ConfigurationAction is installing ReconfigureOnChangeFilter
083    @Test
084    public void scan1() throws JoranException, IOException, InterruptedException {
085        File file = new File(CONF_FILE_AS_STR);
086        configure(file);
087        System.out.println("Running scan1()");
088        doRun();
089    }
090
091    void doRun() throws InterruptedException {
092        RunnableWithCounterAndDone[] runnableArray = buildRunnableArray();
093        harness.execute(runnableArray);
094    }
095}