Last active
August 29, 2015 13:59
-
-
Save minisu/10715863 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Manages statistic updates for a Runner in a load test. | |
*/ | |
public class WebRunnerStatsSender | |
{ | |
private final Map<String, VariableGroup> resourceToVariableGroup = new HashMap<>(); | |
private final Clock clock; | |
public WebRunnerStatsSender( Clock clock ) | |
{ | |
this.clock = clock; | |
} | |
/** | |
* Register a resource. This is done once per resource in the beginning of a test. | |
*/ | |
public void addResource( String resource ) | |
{ | |
resourceToVariableGroup.put( resource, new VariableGroup( resource ) ); | |
} | |
/** | |
* Updates the time taken statistic with a new sample. | |
* This is done once per response (i.e. typically several times per second throughout the test). | |
*/ | |
public void updateTimeTaken( String resource, long timeTaken ) | |
{ | |
long timeStamp = clock.millis(); | |
VariableGroup resorceVariables = getVariablesFor( resource ); | |
if( resorceVariables != null ) | |
{ | |
resorceVariables.timeTakenVariable.update( timeStamp, timeTaken ); | |
} | |
} | |
private VariableGroup getVariablesFor( String resource ) | |
{ | |
return resourceToVariableGroup.get( resource ); | |
} | |
} | |
//////////////////////////////////////////////////////////////////////// | |
/** | |
* Manages statistic updates for a Runner in a load test. | |
*/ | |
public class WebRunnerStatsSender | |
{ | |
private volatile ImmutableMap<String, VariableGroup> resourceToVariableGroup = ImmutableMap.of(); | |
private final Clock clock; | |
public WebRunnerStatsSender( Clock clock ) | |
{ | |
this.clock = clock; | |
} | |
/** | |
* Register a resource. This is done once per resource in the beginning of a test. | |
*/ | |
public void addResources( Iterable<String> resources ) | |
{ | |
ImmutableMap.Builder<String, VariableGroup> mapBuilder = ImmutableMap.builder(); | |
mapBuilder.putAll( resourceToVariableGroup ); | |
for( String resource : resources ) | |
{ | |
mapBuilder.put( resource, new VariableGroup( resource ) ); | |
} | |
resourceToVariableGroup = mapBuilder.build(); | |
} | |
/** | |
* Updates the time taken statistic with a new sample. | |
* This is done once per response (i.e. typically several times per second throughout the test). | |
*/ | |
public void updateTimeTaken( String resource, long timeTaken ) | |
{ | |
long timeStamp = clock.millis(); | |
VariableGroup resorceVariables = getVariablesFor( resource ); | |
if( resorceVariables != null ) | |
{ | |
resorceVariables.timeTakenVariable.update( timeStamp, timeTaken ); | |
} | |
} | |
private VariableGroup getVariablesFor( String resource ) | |
{ | |
return resourceToVariableGroup.get( resource ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment