Advertisement
fkeles

synchronousClient.java

Oct 10th, 2023
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.79 KB | Source Code | 0 0
  1.     private static List<String> synchronousClient(int numberOfInstances) throws Exception {
  2.         long startTime = System.nanoTime();
  3.         System.out.println("-----------------------------------------------------------------------");
  4.         ArrayList<String> instanceIds = new ArrayList<>();
  5.         // Custom client config
  6.         ClientConfiguration clientConfig = ClientConfiguration.builder()
  7.                 .connectionTimeoutMillis(3000)
  8.                 .readTimeoutMillis(60000)
  9.                 .build();
  10.         // Auth provider
  11.         AuthenticationDetailsProvider authenticationDetailsProvider = new ConfigFileAuthenticationDetailsProvider("DEFAULT");
  12.  
  13.         // Compute client
  14.         ComputeClient computeClient = ComputeClient.builder()
  15.                 .configuration(clientConfig)
  16.                 .build(authenticationDetailsProvider);
  17.         computeClient.setRegion(Region.valueOf(System.getenv("REGION")));
  18.  
  19.         // Default values
  20.         String availabilityDomain = System.getenv("AD");
  21.         String compartmentId = System.getenv("COMPARTMENT_ID");
  22.         String imageId = System.getenv("IMAGE_ID");
  23.         String subnetId = System.getenv("SUBNET_ID");
  24.         String shape = System.getenv("SHAPE");
  25.         String instanceName = System.getenv("INSTANCE_NAME_PREFIX");
  26.  
  27.         for (int i = 0; i < numberOfInstances; i++) {
  28.             long startTime1 = System.nanoTime();
  29.             // Set instance details
  30.             LaunchInstanceDetails launchInstanceDetails = LaunchInstanceDetails.builder()
  31.                     .availabilityDomain(availabilityDomain)
  32.                     .compartmentId(compartmentId)
  33.                     .displayName(instanceName + (i + 1))
  34.                     .imageId(imageId)
  35.                     .subnetId(subnetId)
  36.                     .shape(shape)
  37.                     .build();
  38.  
  39.             // Create request
  40.             LaunchInstanceRequest launchInstanceRequest = LaunchInstanceRequest.builder()
  41.                     .launchInstanceDetails(launchInstanceDetails)
  42.                     .build();
  43.  
  44.             // Send request
  45.             LaunchInstanceResponse launchInstanceResponse = computeClient.launchInstance(launchInstanceRequest);
  46.             String instanceId = launchInstanceResponse.getInstance().getId();
  47.             instanceIds.add(instanceId);
  48.             long elapsedTime1 = System.nanoTime() - startTime1;
  49.             System.out.println(instanceId + " created in " + (elapsedTime1 / 1000000) + " ms");
  50.         }
  51.  
  52.         long elapsedTime = System.nanoTime() - startTime;
  53.         System.out.println("Total execution time in seconds: " + TimeUnit.MILLISECONDS.toSeconds(elapsedTime / 1000000));
  54.         System.out.println("-----------------------------------------------------------------------");
  55.         return instanceIds;
  56.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement