/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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 org.apache.catalina.connector;

import java.net.URI;

import org.junit.Assert;
import org.junit.Test;

import org.apache.catalina.startup.LoggingBaseTest;

public class TestResponsePerformance extends LoggingBaseTest {

    private final int ITERATIONS = 100000;

    @Test
    public void testToAbsolutePerformance() throws Exception {
        Request req = new TesterRequest();
        Response resp = new Response();
        resp.setRequest(req);

        // Warm up
        doHomebrew(resp);
        doUri();

        // Performance varies significantly between local testing and CI system.
        // This test regularly causes CI failures. Therefore one homebrew win is
        // sufficient for this test to pass.

        final int attempts = 5;
        boolean homebrewWin = false;
        int count = 0;

        while (count < attempts && !homebrewWin) {
            long homebrew = doHomebrew(resp);
            long uri = doUri();
            log.info("Current 'home-brew': " + homebrew + "ms, Using URI: " + uri + "ms");
            if (homebrew < uri) {
                homebrewWin = true;
            }
            count++;
        }
        Assert.assertTrue(homebrewWin);
    }


    private long doHomebrew(Response resp) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < ITERATIONS; i++) {
            resp.toAbsolute("bar.html");
        }
        return System.currentTimeMillis() - start;
    }


    private long doUri() {
        long start = System.currentTimeMillis();
        for (int i = 0; i < ITERATIONS; i++) {
            URI base = URI.create(
                    "http://localhost:8080/level1/level2/foo.html");
            base.resolve(URI.create("bar.html")).toASCIIString();
        }
        return System.currentTimeMillis() - start;
    }
}

@@ -1344,77 +1344,76 @@
  // 
-Performance varies significantly between local testing and CI system.
+To allow for timing differences between runs, a %22best of n%22 approach
 %0A   
@@ -1424,154 +1424,107 @@
  // 
-Th
 is t
-est regularly causes CI failures. Therefore one homebrew win is%0A        // sufficient for this test to pass.%0A%0A        final int attempts = 5
+aken for this test%0A        final int bestOf = 5;%0A        final int winTarget = (bestOf + 1) / 2
 ;%0A  
@@ -1533,15 +1533,11 @@
     
-boolean
+int
  hom
@@ -1551,13 +1551,9 @@
 n = 
-false
+0
 ;%0A  
@@ -1601,21 +1601,18 @@
 t %3C 
-attempts
+bestOf
  && 
-!
 home
@@ -1618,16 +1618,28 @@
 ebrewWin
+ %3C winTarget
 ) %7B%0A    
@@ -1868,15 +1868,10 @@
 wWin
- = true
+++
 ;%0A  
@@ -1950,16 +1950,29 @@
 ebrewWin
+ == winTarget
 );%0A    %7D

Revert 1657510. Back to the "best of n" approach.