Advertisement
Guest User

Untitled

a guest
Jun 20th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.06 KB | None | 0 0
  1. package com.redhatkeynote.score.client;
  2.  
  3. import com.redhatkeynote.score.Achievement;
  4. import com.redhatkeynote.score.AchievementList;
  5. import jdk.nashorn.internal.ir.annotations.Ignore;
  6.  
  7. import java.time.Duration;
  8. import java.time.Instant;
  9. import java.util.Arrays;
  10. import java.util.HashSet;
  11. import java.util.concurrent.*;
  12.  
  13. import org.junit.AfterClass;
  14. import org.junit.BeforeClass;
  15. import org.junit.Test;
  16. import org.kie.api.KieServices;
  17. import org.kie.api.command.Command;
  18. import org.kie.api.command.KieCommands;
  19. import org.kie.api.runtime.ExecutionResults;
  20. import org.kie.api.runtime.KieSession;
  21.  
  22. import com.redhatkeynote.score.Player;
  23. import org.kie.api.runtime.StatelessKieSession;
  24. import org.kie.server.api.marshalling.MarshallingFormat;
  25. import org.kie.server.api.marshalling.json.JSONMarshaller;
  26. import org.kie.server.api.model.ServiceResponse;
  27. import org.kie.server.client.KieServicesClient;
  28. import org.kie.server.client.KieServicesConfiguration;
  29. import org.kie.server.client.KieServicesFactory;
  30. import org.kie.server.client.RuleServicesClient;
  31.  
  32. public class LoadTest {
  33.  
  34.     @Test
  35.     public void testGame() throws Exception {
  36.         String serverUrl = "http://score-production.apps-test.redhatkeynote.com/kie-server/services/rest/server";
  37.         String user = "kieserver";
  38.         String password = "xxxxxxxxx";
  39.         String containerId = "score_70e71112d291851c17a1214b5a650837";
  40.         final int PLAYER_COUNT = 100;
  41.         final int TEAM_COUNT = 4;
  42.         final int REQUESTS_PER_THREAD = 2500;
  43.         final int THREADS = 1;
  44.  
  45.         KieServicesConfiguration configuration = KieServicesFactory.newRestConfiguration( serverUrl, user, password );
  46.         configuration.setMarshallingFormat( MarshallingFormat.JSON );
  47.         HashSet<Class<?>> extraClasses = new HashSet<Class<?>>( Arrays.asList( Player.class, AchievementList.class, Achievement.class ) );
  48.         configuration.addJaxbClasses( extraClasses );
  49.         KieServicesClient kc =  KieServicesFactory.newKieServicesClient( configuration );
  50.         RuleServicesClient rules = kc.getServicesClient( RuleServicesClient.class );
  51.         //final JSONMarshaller json = new JSONMarshaller( extraClasses, LoadTest.class.getClassLoader() );
  52.  
  53.         KieCommands cmd = KieServices.Factory.get().getCommands();
  54.         Callable<Void>[] tasks = new Callable[THREADS];
  55.         for( int t = 0; t < THREADS; t++ ) {
  56.             final int task_index = t;
  57.             tasks[t] = () -> {
  58.                 Command[] base = new Command[3];
  59.                 base[1] = cmd.newInsert( new AchievementList(), "newAchievements" );
  60.                 base[2] = cmd.newFireAllRules();
  61.  
  62.                 for ( int i = 0; i < REQUESTS_PER_THREAD; i++ ) {
  63.                     int player = ((i*THREADS)+task_index)%PLAYER_COUNT;
  64.                     int team = player%TEAM_COUNT;
  65.                     base[0] = cmd.newInsert( new Player( "p" + player, "John Doe", team, i, i % 15, player % 2 == 0 ) );
  66.  
  67.                     Command batch = cmd.newBatchExecution( Arrays.asList( base ), "ScoreSession" );
  68.                     ServiceResponse<ExecutionResults> results = rules.executeCommandsWithResults( containerId, batch );
  69.                     //System.out.println( json.marshall( results ) );
  70.                 }
  71.                 return null;
  72.             };
  73.         }
  74.  
  75.         ExecutorService executors = Executors.newFixedThreadPool( THREADS );
  76.         Future[] results = new Future[tasks.length];
  77.         Instant start = Instant.now();
  78.         for( int i = 0; i < tasks.length; i++ ) {
  79.             results[i] = executors.submit( tasks[i] );
  80.         }
  81.         for( int i = 0; i < results.length; i++ ) {
  82.             // make sure each task finished
  83.             results[i].get();
  84.         }
  85.         Instant end = Instant.now();
  86.         executors.shutdown();
  87.         long millis = Duration.between( start, end ).toMillis();
  88.         double throughput = ((double)REQUESTS_PER_THREAD*THREADS) / ((double) millis / 1000.0);
  89.         System.out.format( "Time spent = %d, throughput = %4.2f requests/second\n", millis, throughput );
  90.     }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement