Java project to generate high load for exposed api’s of microservice

Java project to generate high load for exposed api’s of microservice

In BootStrapper class , Just get the instance of a Signleton class and start the scheduler

public class BootStrapper { public static void main(String[] args) { try { JettyServer.initServer(); CacheManager.getInstance().startScheduler(); } catch (Exception e) { LogManager.getRootLogger().error(Constant.ERROR,e); } } } public void startScheduler(){ try { for(Trigger trigger: CacheManager.getInstance().triggerToNettyRequestMap.keySet()){ JobDetail job = newJob( NettyTypeJob.class) .withIdentity(trigger.toString()+“job”,“group”).build(); scheduler.scheduleJob(job, trigger); } scheduler.start(); } catch(Exception e){ LogManager.getRootLogger().error(“error”,e); } }

Now here trigger is :
protected void loadNettyTypeRequest(){
CacheManager.getInstance().setNettyRequestMap(new HashMap<String,Map<String,FullHttpRequest>>());
CacheManager.getInstance().getProject().getRequests().forEach(request->{
AtomicInteger count=new AtomicInteger(0);
final int rate=1000;
AtomicLong requestRate=new AtomicLong(request.getRequestRate());
if(requestRate.get()/rate>0)
loadTriggerIntoNettyRequestMap(request, count, rate, 1);
requestRate.set(requestRate.get()%1000);
try(IntStream stream=IntStream.of(500,250,200,125,100,50,40,25,20,10,5,4,2,1)){
stream.forEach(num->{
if(requestRate.get()/num>0){
loadTriggerIntoNettyRequestMap(request, count, 1, rate/num);
requestRate.set(requestRate.get()%num);
}
});
}
});
}
// Request , count , rate and interval are fed/read fro a file
private void loadTriggerIntoNettyRequestMap(Request request,AtomicInteger count,long rate,int intervalInMillis){
for(int x=0;x<rate;x++){
final String suffix=Integer.toString(count.getAndIncrement());
CacheManager.getInstance().getNettyRequestMap().put(request.getRequestName()+suffix, new HashMap<String,FullHttpRequest>());
Trigger trigger = newTrigger().withIdentity(request.getRequestName()+suffix, “group”).startNow().withSchedule(simpleSchedule()
.withIntervalInMilliseconds(intervalInMillis).withMisfireHandlingInstructionFireNow().repeatForever()).build();
request.getEvents().forEach(event->{
try
{
addToNettyRequestMap(request,event,suffix);
}catch(Exception e){
LogManager.getRootLogger().error(Constant.ERROR,e);
}
});
TriggerToNettyRequestMapping triggerToNettyRequestMapping=new TriggerToNettyRequestMapping(request.getRequestName(), request.getRequestRate(), request.isSynchronous(), CacheManager.getInstance().getNettyRequestMap().get(request.getRequestName()+suffix));
CacheManager.getInstance().getTriggerToNettyRequestMap().put(trigger, triggerToNettyRequestMapping);
}
}
Job Definition:
public class NettyTypeJob implements Job {
class Fields
{
int index;
ChannelFuture channelFuture;
ChannelPromise promise;
public Fields(int index, ChannelFuture channelFuture, ChannelPromise promise) {
this.index = index;
this.channelFuture = channelFuture;
this.promise = promise;
}

}
private void takeActionOnComplete(Fields fields,List<FullHttpRequest> requests,boolean synchronous,String requestName,List<String> eventNames,String flowId,Trigger trigger){
int index=fields.index;
ChannelFuture channelFuture=fields.channelFuture;
ChannelPromise promise=fields.promise;
if(channelFuture.isSuccess()){
if(index+1<requests.size()){
promise.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if(future.isSuccess())
sequentialExecute(index+1, requests,synchronous,requestName,eventNames,flowId,trigger);

}
});
}
}
}
private void sequentialExecute(int index,List<FullHttpRequest> requests,boolean synchronous,String requestName,List<String> eventNames,String flowId,Trigger trigger){
Future<Channel> acquire=CacheManager.getInstance().getPool().acquire();
acquire.addListener(new FutureListener<Channel>(){
@Override
public void operationComplete(Future<Channel> channelFuture) throws Exception {
if(channelFuture.isSuccess()){
final Channel channel = channelFuture.getNow();
ChannelPromise promise=channel.newPromise();

FullHttpRequest request=requests.get(index).copy();
channel.attr(Constant.TRIGGER).set(trigger);
channel.attr(Constant.FLOW_ID).set(flowId);
LogManager.getLogger(NettyTypeJob.class).info(System.currentTimeMillis()+” “+flowId+” “+CacheManager.getInstance().getFlowIds().get(flowId).getRequestName()+” “+
CacheManager.getInstance().getFlowIds().get(flowId).getEventName()+” event”);
channel.writeAndFlush(request).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture) throws Exception {
takeActionOnComplete(new Fields(index, channelFuture, promise), requests, synchronous, requestName, eventNames, flowId, trigger);
}
});
}
else if(!(channelFuture.cause() instanceof IllegalStateException||channelFuture.cause() instanceof TimeoutException))
{
// log error
}
}
});
}
@Override
public void execute(JobExecutionContext executionContext) throws JobExecutionException {
Trigger trigger=executionContext.getTrigger();
TriggerToNettyRequestMapping requestMapping=CacheManager.getInstance().getTriggerToNettyRequestMap().get(trigger);
String flowId=UUID.randomUUID().toString();
sequentialExecute(0, requestMapping.getEvents().values().stream().collect(Collectors.toList()),requestMapping.isSynchronous(),requestMapping.getRequestName(),
requestMapping.getEvents().keySet().stream().collect(Collectors.toList()),flowId,trigger);
}
}