use of org.apache.commons.lang3.time.StopWatch in project azure-sdk-for-java by Azure.
the class CreateVirtualMachinesInParallel method runSample.
/**
* Main function which runs the actual sample.
* @param azure instance of the azure client
* @return true if sample runs successfully
*/
public static boolean runSample(Azure azure) {
final String rgName = SdkContext.randomResourceName("rgCOPD", 24);
final String userName = "tirekicker";
final String sshKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfSPC2K7LZcFKEO+/t3dzmQYtrJFZNxOsbVgOVKietqHyvmYGHEC0J2wPdAqQ/63g/hhAEFRoyehM+rbeDri4txB3YFfnOK58jqdkyXzupWqXzOrlKY4Wz9SKjjN765+dqUITjKRIaAip1Ri137szRg71WnrmdP3SphTRlCx1Bk2nXqWPsclbRDCiZeF8QOTi4JqbmJyK5+0UqhqYRduun8ylAwKKQJ1NJt85sYIHn9f1Rfr6Tq2zS0wZ7DHbZL+zB5rSlAr8QyUdg/GQD+cmSs6LvPJKL78d6hMGk84ARtFo4A79ovwX/Fj01znDQkU6nJildfkaolH2rWFG/qttD azjava@javalib.com";
Map<Region, Integer> virtualMachinesByLocation = new HashMap<Region, Integer>();
// debug target
/**
virtualMachinesByLocation.put(Region.US_EAST, 5);
virtualMachinesByLocation.put(Region.US_SOUTH_CENTRAL, 5);
*/
// final demo target
virtualMachinesByLocation.put(Region.US_EAST, 12);
virtualMachinesByLocation.put(Region.US_SOUTH_CENTRAL, 12);
virtualMachinesByLocation.put(Region.US_WEST, 12);
virtualMachinesByLocation.put(Region.US_NORTH_CENTRAL, 12);
try {
//=============================================================
// Create a resource group (Where all resources gets created)
//
ResourceGroup resourceGroup = azure.resourceGroups().define(rgName).withRegion(Region.US_EAST).create();
System.out.println("Created a new resource group - " + resourceGroup.id());
List<String> publicIpCreatableKeys = new ArrayList<>();
// Prepare a batch of Creatable definitions
//
List<Creatable<VirtualMachine>> creatableVirtualMachines = new ArrayList<>();
for (Map.Entry<Region, Integer> entry : virtualMachinesByLocation.entrySet()) {
Region region = entry.getKey();
Integer vmCount = entry.getValue();
//=============================================================
// Create 1 network creatable per region
// Prepare Creatable Network definition (Where all the virtual machines get added to)
//
String networkName = SdkContext.randomResourceName("vnetCOPD-", 20);
Creatable<Network> networkCreatable = azure.networks().define(networkName).withRegion(region).withExistingResourceGroup(resourceGroup).withAddressSpace("172.16.0.0/16");
//=============================================================
// Create 1 storage creatable per region (For storing VMs disk)
//
String storageAccountName = SdkContext.randomResourceName("stgcopd", 20);
Creatable<StorageAccount> storageAccountCreatable = azure.storageAccounts().define(storageAccountName).withRegion(region).withExistingResourceGroup(resourceGroup);
String linuxVMNamePrefix = SdkContext.randomResourceName("vm-", 15);
for (int i = 1; i <= vmCount; i++) {
//=============================================================
// Create 1 public IP address creatable
//
Creatable<PublicIPAddress> publicIPAddressCreatable = azure.publicIPAddresses().define(String.format("%s-%d", linuxVMNamePrefix, i)).withRegion(region).withExistingResourceGroup(resourceGroup).withLeafDomainLabel(SdkContext.randomResourceName("pip", 10));
publicIpCreatableKeys.add(publicIPAddressCreatable.key());
//=============================================================
// Create 1 virtual machine creatable
Creatable<VirtualMachine> virtualMachineCreatable = azure.virtualMachines().define(String.format("%s-%d", linuxVMNamePrefix, i)).withRegion(region).withExistingResourceGroup(resourceGroup).withNewPrimaryNetwork(networkCreatable).withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(publicIPAddressCreatable).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withSsh(sshKey).withSize(VirtualMachineSizeTypes.STANDARD_DS3_V2).withNewStorageAccount(storageAccountCreatable);
creatableVirtualMachines.add(virtualMachineCreatable);
}
}
//=============================================================
// Create !!
StopWatch stopwatch = new StopWatch();
System.out.println("Creating the virtual machines");
stopwatch.start();
CreatedResources<VirtualMachine> virtualMachines = azure.virtualMachines().create(creatableVirtualMachines);
stopwatch.stop();
System.out.println("Created virtual machines");
for (VirtualMachine virtualMachine : virtualMachines.values()) {
System.out.println(virtualMachine.id());
}
System.out.println("Virtual Machines created: (took " + (stopwatch.getTime() / 1000) + " seconds to create) == " + virtualMachines.size() + " == virtual machines");
List<String> publicIpResourceIds = new ArrayList<>();
for (String publicIpCreatableKey : publicIpCreatableKeys) {
PublicIPAddress pip = (PublicIPAddress) virtualMachines.createdRelatedResource(publicIpCreatableKey);
publicIpResourceIds.add(pip.id());
}
//=============================================================
// Create 1 Traffic Manager Profile
//
String trafficManagerName = SdkContext.randomResourceName("tra", 15);
TrafficManagerProfile.DefinitionStages.WithEndpoint profileWithEndpoint = azure.trafficManagerProfiles().define(trafficManagerName).withExistingResourceGroup(resourceGroup).withLeafDomainLabel(trafficManagerName).withPerformanceBasedRouting();
int endpointPriority = 1;
TrafficManagerProfile.DefinitionStages.WithCreate profileWithCreate = null;
for (String publicIpResourceId : publicIpResourceIds) {
String endpointName = String.format("azendpoint-%d", endpointPriority);
if (endpointPriority == 1) {
profileWithCreate = profileWithEndpoint.defineAzureTargetEndpoint(endpointName).toResourceId(publicIpResourceId).withRoutingPriority(endpointPriority).attach();
} else {
profileWithCreate = profileWithCreate.defineAzureTargetEndpoint(endpointName).toResourceId(publicIpResourceId).withRoutingPriority(endpointPriority).attach();
}
endpointPriority++;
}
System.out.println("Creating a traffic manager profile for the VMs");
stopwatch.reset();
stopwatch.start();
TrafficManagerProfile trafficManagerProfile = profileWithCreate.create();
stopwatch.stop();
System.out.println("Created a traffic manager profile (took " + (stopwatch.getTime() / 1000) + " seconds to create): " + trafficManagerProfile.id());
return true;
} catch (Exception f) {
System.out.println(f.getMessage());
f.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().deleteByName(rgName);
System.out.println("Deleted Resource Group: " + rgName);
} catch (NullPointerException npe) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
} catch (Exception g) {
g.printStackTrace();
}
}
return false;
}
use of org.apache.commons.lang3.time.StopWatch in project azure-sdk-for-java by Azure.
the class ManageVirtualMachinesInParallelWithNetwork method runSample.
/**
* Main function which runs the actual sample.
* @param azure instance of the azure client
* @return true if sample runs successfully
*/
public static boolean runSample(Azure azure) {
final int frontendVMCount = 10;
final int backendVMCount = 10;
final String rgName = SdkContext.randomResourceName("rgNEPP", 24);
final String frontEndNsgName = SdkContext.randomResourceName("fensg", 24);
final String backEndNsgName = SdkContext.randomResourceName("bensg", 24);
final String networkName = SdkContext.randomResourceName("vnetCOMV", 24);
final String storageAccountName = SdkContext.randomResourceName("stgCOMV", 20);
final String userName = "tirekicker";
final String password = "12NewPA$$w0rd!";
try {
// Create a resource group [Where all resources gets created]
ResourceGroup resourceGroup = azure.resourceGroups().define(rgName).withRegion(Region.US_EAST).create();
//============================================================
// Define a network security group for the front end of a subnet
// front end subnet contains two rules
// - ALLOW-SSH - allows SSH traffic into the front end subnet
// - ALLOW-WEB- allows HTTP traffic into the front end subnet
Creatable<NetworkSecurityGroup> frontEndNSGCreatable = azure.networkSecurityGroups().define(frontEndNsgName).withRegion(Region.US_EAST).withExistingResourceGroup(resourceGroup).defineRule("ALLOW-SSH").allowInbound().fromAnyAddress().fromAnyPort().toAnyAddress().toPort(22).withProtocol(SecurityRuleProtocol.TCP).withPriority(100).withDescription("Allow SSH").attach().defineRule("ALLOW-HTTP").allowInbound().fromAnyAddress().fromAnyPort().toAnyAddress().toPort(80).withProtocol(SecurityRuleProtocol.TCP).withPriority(101).withDescription("Allow HTTP").attach();
//============================================================
// Define a network security group for the back end of a subnet
// back end subnet contains two rules
// - ALLOW-SQL - allows SQL traffic only from the front end subnet
// - DENY-WEB - denies all outbound internet traffic from the back end subnet
Creatable<NetworkSecurityGroup> backEndNSGCreatable = azure.networkSecurityGroups().define(backEndNsgName).withRegion(Region.US_EAST).withExistingResourceGroup(resourceGroup).defineRule("ALLOW-SQL").allowInbound().fromAddress("172.16.1.0/24").fromAnyPort().toAnyAddress().toPort(1433).withProtocol(SecurityRuleProtocol.TCP).withPriority(100).withDescription("Allow SQL").attach().defineRule("DENY-WEB").denyOutbound().fromAnyAddress().fromAnyPort().toAnyAddress().toAnyPort().withAnyProtocol().withDescription("Deny Web").withPriority(200).attach();
System.out.println("Creating security group for the front ends - allows SSH and HTTP");
System.out.println("Creating security group for the back ends - allows SSH and denies all outbound internet traffic");
@SuppressWarnings("unchecked") Collection<NetworkSecurityGroup> networkSecurityGroups = azure.networkSecurityGroups().create(frontEndNSGCreatable, backEndNSGCreatable).values();
NetworkSecurityGroup frontendNSG = null;
NetworkSecurityGroup backendNSG = null;
for (NetworkSecurityGroup nsg : networkSecurityGroups) {
if (nsg.name().equalsIgnoreCase(frontEndNsgName)) {
frontendNSG = nsg;
}
if (nsg.name().equalsIgnoreCase(backEndNsgName)) {
backendNSG = nsg;
}
}
System.out.println("Created a security group for the front end: " + frontendNSG.id());
Utils.print(frontendNSG);
System.out.println("Created a security group for the back end: " + backendNSG.id());
Utils.print(backendNSG);
// Create Network [Where all the virtual machines get added to]
Network network = azure.networks().define(networkName).withRegion(Region.US_EAST).withExistingResourceGroup(resourceGroup).withAddressSpace("172.16.0.0/16").defineSubnet("Front-end").withAddressPrefix("172.16.1.0/24").withExistingNetworkSecurityGroup(frontendNSG).attach().defineSubnet("Back-end").withAddressPrefix("172.16.2.0/24").withExistingNetworkSecurityGroup(backendNSG).attach().create();
// Prepare Creatable Storage account definition [For storing VMs disk]
Creatable<StorageAccount> creatableStorageAccount = azure.storageAccounts().define(storageAccountName).withRegion(Region.US_EAST).withExistingResourceGroup(resourceGroup);
// Prepare a batch of Creatable Virtual Machines definitions
List<Creatable<VirtualMachine>> frontendCreatableVirtualMachines = new ArrayList<>();
for (int i = 0; i < frontendVMCount; i++) {
Creatable<VirtualMachine> creatableVirtualMachine = azure.virtualMachines().define("VM-FE-" + i).withRegion(Region.US_EAST).withExistingResourceGroup(resourceGroup).withExistingPrimaryNetwork(network).withSubnet("Front-end").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withRootPassword(password).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).withNewStorageAccount(creatableStorageAccount);
frontendCreatableVirtualMachines.add(creatableVirtualMachine);
}
List<Creatable<VirtualMachine>> backendCreatableVirtualMachines = new ArrayList<>();
for (int i = 0; i < backendVMCount; i++) {
Creatable<VirtualMachine> creatableVirtualMachine = azure.virtualMachines().define("VM-BE-" + i).withRegion(Region.US_EAST).withExistingResourceGroup(resourceGroup).withExistingPrimaryNetwork(network).withSubnet("Back-end").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withRootPassword(password).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2).withNewStorageAccount(creatableStorageAccount);
backendCreatableVirtualMachines.add(creatableVirtualMachine);
}
System.out.println("Creating the virtual machines");
List<Creatable<VirtualMachine>> allCreatableVirtualMachines = new ArrayList<>();
allCreatableVirtualMachines.addAll(frontendCreatableVirtualMachines);
allCreatableVirtualMachines.addAll(backendCreatableVirtualMachines);
StopWatch stopwatch = new StopWatch();
stopwatch.start();
Collection<VirtualMachine> virtualMachines = azure.virtualMachines().create(allCreatableVirtualMachines).values();
stopwatch.stop();
System.out.println("Created virtual machines");
for (VirtualMachine virtualMachine : virtualMachines) {
System.out.println(virtualMachine.id());
}
System.out.println("Virtual Machines create: (took " + (stopwatch.getTime() / 1000) + " seconds) ");
return true;
} catch (Exception f) {
System.out.println(f.getMessage());
f.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().deleteByName(rgName);
System.out.println("Deleted Resource Group: " + rgName);
} catch (NullPointerException npe) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
} catch (Exception g) {
g.printStackTrace();
}
}
return false;
}
use of org.apache.commons.lang3.time.StopWatch in project azure-sdk-for-java by Azure.
the class ManageVirtualMachinesInParallel method runSample.
/**
* Main function which runs the actual sample.
* @param azure instance of the azure client
* @return true if sample runs successfully
*/
public static boolean runSample(Azure azure) {
final int vmCount = 10;
final Region region = Region.US_EAST;
final String rgName = SdkContext.randomResourceName("rgCOPP", 24);
final String networkName = SdkContext.randomResourceName("vnetCOMV", 24);
final String storageAccountName = SdkContext.randomResourceName("stgCOMV", 20);
final String userName = "tirekicker";
final String password = "12NewPA$$w0rd!";
try {
// Create a resource group [Where all resources gets created]
ResourceGroup resourceGroup = azure.resourceGroups().define(rgName).withRegion(region).create();
// Prepare Creatable Network definition [Where all the virtual machines get added to]
Creatable<Network> creatableNetwork = azure.networks().define(networkName).withRegion(region).withExistingResourceGroup(resourceGroup).withAddressSpace("172.16.0.0/16");
// Prepare Creatable Storage account definition [For storing VMs disk]
Creatable<StorageAccount> creatableStorageAccount = azure.storageAccounts().define(storageAccountName).withRegion(region).withExistingResourceGroup(resourceGroup);
// Prepare a batch of Creatable Virtual Machines definitions
List<Creatable<VirtualMachine>> creatableVirtualMachines = new ArrayList<>();
for (int i = 0; i < vmCount; i++) {
Creatable<VirtualMachine> creatableVirtualMachine = azure.virtualMachines().define("VM-" + i).withRegion(region).withExistingResourceGroup(resourceGroup).withNewPrimaryNetwork(creatableNetwork).withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withRootPassword(password).withSize(VirtualMachineSizeTypes.STANDARD_DS3_V2).withNewStorageAccount(creatableStorageAccount);
creatableVirtualMachines.add(creatableVirtualMachine);
}
StopWatch stopwatch = new StopWatch();
System.out.println("Creating the virtual machines");
stopwatch.start();
Collection<VirtualMachine> virtualMachines = azure.virtualMachines().create(creatableVirtualMachines).values();
stopwatch.stop();
System.out.println("Created virtual machines");
for (VirtualMachine virtualMachine : virtualMachines) {
System.out.println(virtualMachine.id());
}
System.out.println("Virtual Machines create: (took " + (stopwatch.getTime() / 1000) + " seconds) ");
return true;
} catch (Exception f) {
System.out.println(f.getMessage());
f.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().beginDeleteByName(rgName);
} catch (NullPointerException npe) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
} catch (Exception g) {
g.printStackTrace();
}
}
return false;
}
use of org.apache.commons.lang3.time.StopWatch in project ORCID-Source by ORCID.
the class MemberV2PerformanceTest method createManyWorks.
@Test
public void createManyWorks() throws JSONException, InterruptedException, URISyntaxException {
int numWorks = 1000;
// Amount of linear increase allowed
float scalingFactor = 1.5f;
int numInitialSample = numWorks / 10;
long initialSampleTime = 0;
StopWatch stopWatch = new StopWatch();
stopWatch.start();
for (int i = 1; i <= numWorks; i++) {
StopWatch singleWorkStopWatch = new StopWatch();
singleWorkStopWatch.start();
long time = System.currentTimeMillis();
Work workToCreate = (Work) unmarshallFromPath("/record_2.0/samples/work-2.0.xml", Work.class);
workToCreate.setPutCode(null);
workToCreate.getExternalIdentifiers().getExternalIdentifier().clear();
ExternalID wExtId = new ExternalID();
wExtId.setValue("Work Id " + i + " " + time);
wExtId.setType(WorkExternalIdentifierType.AGR.value());
wExtId.setRelationship(Relationship.SELF);
workToCreate.getExternalIdentifiers().getExternalIdentifier().add(wExtId);
String accessToken = getAccessToken();
ClientResponse postResponse = memberV2ApiClient.createWorkXml(this.getUser1OrcidId(), workToCreate, accessToken);
stopWatch.split();
long splitTime = stopWatch.getSplitTime();
System.out.println("Split time: " + splitTime);
if (i == numInitialSample) {
initialSampleTime = splitTime;
} else if (i > numInitialSample) {
float maxTime = (initialSampleTime / numInitialSample) * scalingFactor * i;
System.out.println("Max time: " + maxTime);
assertTrue("Split time = " + splitTime + ", but max allowed time = " + maxTime + ", when num added = " + i, splitTime <= maxTime);
}
assertNotNull(postResponse);
assertEquals(Response.Status.CREATED.getStatusCode(), postResponse.getStatus());
String locationPath = postResponse.getLocation().getPath();
assertTrue("Location header path should match pattern, but was " + locationPath, locationPath.matches(".*/v2.0/" + this.getUser1OrcidId() + "/work/\\d+"));
ClientResponse getResponse = memberV2ApiClient.viewLocationXml(postResponse.getLocation(), accessToken);
assertEquals(Response.Status.OK.getStatusCode(), getResponse.getStatus());
Work gotWork = getResponse.getEntity(Work.class);
assertEquals("common:title", gotWork.getWorkTitle().getTitle().getContent());
System.out.println("Time for single work = " + singleWorkStopWatch);
}
}
use of org.apache.commons.lang3.time.StopWatch in project pyramid by cheng-li.
the class AbstractCBMOptimizer method skipOrUpdateBinaryClassifier.
protected void skipOrUpdateBinaryClassifier(int component, int label, MultiLabelClfDataSet activeDataSet, double[] activeGammas, double totalWeight) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
double effectivePositives = effectivePositives(component, label);
double nonSmoothedPositiveProb = effectivePositives / totalWeight;
// smooth the component-wise label fraction with global label fraction
double smoothedPositiveProb = (effectivePositives + smoothingStrength * positiveCounts[label]) / (totalWeight + smoothingStrength * dataSet.getNumDataPoints());
StringBuilder sb = new StringBuilder();
sb.append("for component ").append(component).append(", label ").append(label);
sb.append(", weighted positives = ").append(effectivePositives);
sb.append(", non-smoothed positive fraction = " + (effectivePositives / totalWeight));
sb.append(", global positive fraction = " + ((double) positiveCounts[label] / dataSet.getNumDataPoints()));
sb.append(", smoothed positive fraction = " + smoothedPositiveProb);
// it be happen that p >1 for numerical reasons
if (smoothedPositiveProb >= 1) {
smoothedPositiveProb = 1;
}
if (nonSmoothedPositiveProb < skipLabelThreshold || nonSmoothedPositiveProb > 1 - skipLabelThreshold) {
double[] probs = { 1 - smoothedPositiveProb, smoothedPositiveProb };
cbm.binaryClassifiers[component][label] = new PriorProbClassifier(probs);
sb.append(", skip, use prior = ").append(smoothedPositiveProb);
sb.append(", time spent = ").append(stopWatch.toString());
if (logger.isDebugEnabled()) {
logger.debug(sb.toString());
}
return;
}
if (logger.isDebugEnabled()) {
logger.debug(sb.toString());
}
updateBinaryClassifier(component, label, activeDataSet, activeGammas);
}
Aggregations