Skip to content

Commit

Permalink
implements fail fast when sending request fail #13
Browse files Browse the repository at this point in the history
Signed-off-by: olivier lamy <[email protected]>
  • Loading branch information
olamy committed Mar 30, 2017
1 parent 132f4bf commit 1adaa59
Show file tree
Hide file tree
Showing 11 changed files with 338 additions and 32 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ With all listeners we have, we can take measure in different places
8. response body start received ( ContentListener#onContent() )
9. response body completed ( CompleteListener#onComplete() )

#### Response Time
#### Latency Time

The responseTime is the time taken just before 1. and 9.
The latencyTime is the time taken just before 2. and 6. (time to get the first byte of the response)

#### Latency Time
#### Response Time

The latencyTime is the time taken just before 2. and 6.
The responseTime is the time taken just before 1. and 9. (time to get the last byte of the response)

### Using uber jar

Expand Down
19 changes: 19 additions & 0 deletions jetty-load-generator-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,23 @@
</plugins>
</build>

<profiles>
<profile>
<id>debug</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<org.mortbay.jetty.load.generator.LEVEL>DEBUG</org.mortbay.jetty.load.generator.LEVEL>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ public void failed(Throwable x) {
logger.debug("failed tree for {}", resource);
}
callback.failed(x);
LoadGenerator.this.interrupt();
}
}, nodes);
Sender sender = new Sender(client, warmup, treeCallback);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
//
// ========================================================================
// Copyright (c) 1995-2017 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//

package org.mortbay.jetty.load.generator;

import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.StatisticsHandler;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlet.StatisticsServlet;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;

public class FailFastTest
{

private static final Logger LOGGER = Log.getLogger( FailFastTest.class );
protected Resource resource;
protected Server server;
protected ServerConnector connector;
TestHandler testHandler;


@Before
public void startJetty()
throws Exception
{
StatisticsHandler statisticsHandler = new StatisticsHandler();
QueuedThreadPool serverThreads = new QueuedThreadPool();
serverThreads.setName( "server" );
server = new Server( serverThreads );
connector = new ServerConnector( server, new HttpConnectionFactory( new HttpConfiguration() ) );
server.addConnector( connector );
server.setHandler( statisticsHandler );
ServletContextHandler statsContext = new ServletContextHandler( statisticsHandler, "/" );
statsContext.addServlet( new ServletHolder( new StatisticsServlet() ), "/stats" );
testHandler = new TestHandler();
testHandler.server = server;
statsContext.addServlet( new ServletHolder( testHandler ), "/" );
statsContext.setSessionHandler( new SessionHandler() );
server.start();
}

@After
public void stopJetty()
throws Exception
{
if ( server.isRunning() )
{
server.stop();
}
}

@Test
public void should_fail_fast()
throws Exception
{
AtomicInteger onFailure = new AtomicInteger( 0 ), onCommit = new AtomicInteger( 0 );
LoadGenerator.Builder builder = //
new LoadGenerator.Builder() //
.host( "localhost" ) //
.port( connector.getLocalPort() ) //
.resource( new Resource( "/index.html?fail=5" ) ) //
.warmupIterationsPerThread( 1 ) //
.usersPerThread( 1 ) //
.threads( 1 ) //
.resourceRate( 5 )
.iterationsPerThread( 25 ) //
//.runFor( 10, TimeUnit.SECONDS ) //
.requestListener( new Request.Listener.Adapter() {
@Override
public void onFailure( Request request, Throwable failure )
{
LOGGER.info( "fail: {}", onFailure.incrementAndGet() );
}

@Override
public void onCommit( Request request )
{
LOGGER.info( "onCommit: {}", onCommit.incrementAndGet() );
}
} );
boolean exception = false;
try
{
builder.build().begin().get();
}
catch ( Exception e )
{
exception = true;
}
Assert.assertTrue( exception );
LOGGER.info( "onFailure: {}, onCommit: {}", onFailure, onCommit);
int onFailureCall = onFailure.get();
Assert.assertTrue("onFailureCall is " + onFailureCall, onFailureCall < 5);
}


static class TestHandler
extends HttpServlet
{

AtomicInteger getNumber = new AtomicInteger( 0 );
Server server;

@Override
protected void service( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException
{
String fail = request.getParameter( "fail" );
if ( getNumber.get() >= Integer.parseInt( fail ) )
{
try
{
server.stop();
}
catch ( Exception e )
{
throw new RuntimeException( e.getMessage(), e );
}
}
response.getOutputStream().write( "Jetty rocks!!".getBytes() );
response.flushBuffer();
getNumber.addAndGet( 1 );
}
}

}
29 changes: 20 additions & 9 deletions jetty-load-generator-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,27 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<org.mortbay.jetty.load.generator.LEVEL>DEBUG</org.mortbay.jetty.load.generator.LEVEL>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>debug</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<org.mortbay.jetty.load.generator.LEVEL>DEBUG</org.mortbay.jetty.load.generator.LEVEL>
<org.mortbay.jetty.load.generator.starter.LEVEL>DEBUG</org.mortbay.jetty.load.generator.starter.LEVEL>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
3 changes: 0 additions & 3 deletions jetty-load-generator-starter/simple_profile.groovy

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public abstract class AbstractLoadGeneratorStarter

private Resource resource;

private Request.Listener[] listeners;

public AbstractLoadGeneratorStarter( LoadGeneratorStarterArgs runnerArgs )
{
this.starterArgs = runnerArgs;
Expand All @@ -83,6 +85,11 @@ public void run()
.warmupIterationsPerThread( starterArgs.getWarmupNumber() ) //
.scheme( starterArgs.getScheme() ); //

if (starterArgs.getThreads() > 0)
{
loadGeneratorBuilder.threads( starterArgs.getThreads() );
}

if ( starterArgs.getMaxRequestsQueued() > 0 )
{
loadGeneratorBuilder.maxRequestsQueued( starterArgs.getMaxRequestsQueued() );
Expand Down Expand Up @@ -159,7 +166,12 @@ protected Resource.Listener[] getResourceListeners()

protected Request.Listener[] getListeners()
{
return new Request.Listener[0];
return listeners == null ? new Request.Listener[0] : this.listeners;
}

protected void setListeners(Request.Listener[] listeners)
{
this.listeners = listeners;
}

public ExecutorService getExecutorService()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.mortbay.jetty.load.generator.starter;

import com.beust.jcommander.JCommander;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

/**
*
Expand All @@ -27,6 +29,8 @@ public class LoadGeneratorStarter
extends AbstractLoadGeneratorStarter
{

private static final Logger LOGGER = Log.getLogger( LoadGeneratorStarter.class);

public LoadGeneratorStarter( LoadGeneratorStarterArgs runnerArgs )
{
super( runnerArgs );
Expand Down Expand Up @@ -67,9 +71,8 @@ public static void main( String[] args )
}
catch ( Exception e )
{
e.printStackTrace();
LOGGER.info( "error happened", e);
new JCommander( runnerArgs ).usage();
System.exit( 1 );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public class LoadGeneratorStarterArgs
@Parameter( names = { "--users", "-u" }, description = "Simulated users number" )
private int users = 1;

@Parameter( names = { "--threads" }, description = "Threads number" )
private int threads = 0;

@Parameter( names = { "--transaction-rate", "-tr" }, description = "Transaction rate / second" )
private int transactionRate = 1;

Expand Down Expand Up @@ -358,18 +361,28 @@ public void setMaxRequestsQueued( int maxRequestsQueued )
this.maxRequestsQueued = maxRequestsQueued;
}

public int getThreads()
{
return threads;
}

public void setThreads( int threads )
{
this.threads = threads;
}

@Override
public String toString()
{
return "LoadGeneratorStarterArgs{" + "profileXmlPath='" + profileXmlPath + '\'' + ", profileJsonPath='"
+ profileJsonPath + '\'' + ", profileGroovyPath='" + profileGroovyPath + '\'' + ", host='" + host + '\''
+ ", port=" + port + ", users=" + users + ", transactionRate=" + transactionRate + ", transport='"
+ transport + '\'' + ", selectors=" + selectors + ", runningTime=" + runningTime + ", runningTimeUnit='"
+ runningTimeUnit + '\'' + ", runIteration=" + runIteration + ", reportHost='" + reportHost + '\''
+ ", scheme='" + scheme + '\'' + ", reportPort=" + reportPort + ", notInterrupt=" + notInterrupt
+ ", statsFile='" + statsFile + '\'' + ", params=" + params + ", help=" + help + ", displayStatsAtEnd="
+ displayStatsAtEnd + ", collectServerStats=" + collectServerStats + ", warmupNumber=" + warmupNumber
+ ", maxRequestsQueued=" + maxRequestsQueued + '}';
+ ", port=" + port + ", users=" + users + ", threads=" + threads + ", transactionRate=" + transactionRate
+ ", transport='" + transport + '\'' + ", selectors=" + selectors + ", runningTime=" + runningTime
+ ", runningTimeUnit='" + runningTimeUnit + '\'' + ", runIteration=" + runIteration + ", reportHost='"
+ reportHost + '\'' + ", scheme='" + scheme + '\'' + ", reportPort=" + reportPort + ", notInterrupt="
+ notInterrupt + ", statsFile='" + statsFile + '\'' + ", params=" + params + ", help=" + help
+ ", displayStatsAtEnd=" + displayStatsAtEnd + ", collectServerStats=" + collectServerStats
+ ", warmupNumber=" + warmupNumber + ", maxRequestsQueued=" + maxRequestsQueued + '}';
}

public enum Transport {
Expand Down
Loading

0 comments on commit 1adaa59

Please sign in to comment.