Skip to content

Commit

Permalink
upload temp file setup
Browse files Browse the repository at this point in the history
  • Loading branch information
samvaity committed Dec 6, 2021
1 parent 3d6ee96 commit 6a46c09
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,52 @@

import com.azure.core.perf.core.ServiceTest;
import com.azure.perf.test.core.PerfStressOptions;
import com.azure.perf.test.core.TestDataCreationHelper;
import com.azure.storage.blob.BlobAsyncClient;
import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobContainerAsyncClient;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceAsyncClient;
import com.azure.storage.blob.BlobServiceClient;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.UUID;

public class UploadFromFileTest extends ServiceTest<PerfStressOptions> {

private static final Path TEMP_FILE;
private static final String TEMP_FILE_PATH;

static {
try {
TEMP_FILE = Files.createTempFile(null, null);
TEMP_FILE_PATH = TEMP_FILE.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public UploadFromFileTest(PerfStressOptions options) {
super(options);
}

@Override
public Mono<Void> globalSetupAsync() {
return super.globalSetupAsync().then(createTempFile());
return super.globalSetupAsync();
}

@Override
public Mono<Void> globalCleanupAsync() {
return deleteTempFile().then(super.globalCleanupAsync());
}

private Mono<Void> createTempFile() {
return Mono.fromCallable(() -> {
TestDataCreationHelper.writeToFile(TEMP_FILE_PATH, options.getSize(), 8192);
return 1;
}).then();
}

private Mono<Void> deleteTempFile() {
try {
Files.delete(TEMP_FILE);
return Mono.empty();
} catch (IOException e) {
throw new RuntimeException(e);
}
return super.globalCleanupAsync();
}

@Override
public void run() {
blobClientBuilder.buildClient().uploadFromFile(TEMP_FILE_PATH, true);
String blobName = "perfblobtest-" + UUID.randomUUID();

BlobServiceClient storageClient = blobServiceClientBuilder.buildClient();

BlobContainerClient blobContainerClient = storageClient.createBlobContainer("perfupload" + UUID.randomUUID());
BlobClient blobClient = blobContainerClient.getBlobClient(blobName);
blobClient.uploadFromFile(filePath, true);

}

@Override
public Mono<Void> runAsync() {
return blobClientBuilder.buildAsyncClient().uploadFromFile(TEMP_FILE_PATH, true);
String blobName = "perfblobtest-" + UUID.randomUUID();

BlobServiceAsyncClient storageAsyncClient = blobServiceClientBuilder.buildAsyncClient();

BlobContainerAsyncClient
blobContainerAsyncClient = storageAsyncClient.createBlobContainer("perfupload" + UUID.randomUUID()).block();
BlobAsyncClient blobAsyncClient = blobContainerAsyncClient.getBlobAsyncClient(blobName);
return blobAsyncClient.uploadFromFile(filePath, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,31 @@
import com.azure.core.util.CoreUtils;
import com.azure.perf.test.core.PerfStressOptions;
import com.azure.perf.test.core.PerfStressTest;
import com.azure.storage.blob.BlobClientBuilder;
import com.azure.storage.blob.BlobServiceClientBuilder;

public abstract class ServiceTest<TOptions extends PerfStressOptions> extends PerfStressTest<TOptions> {

protected final BlobClientBuilder blobClientBuilder;
protected final BlobServiceClientBuilder blobServiceClientBuilder;
protected final String filePath;
private final Configuration configuration;

public ServiceTest(TOptions options) {
super(options);
configuration = Configuration.getGlobalConfiguration().clone();
String connectionString = configuration.get("STORAGE_CONNECTION_STRING");
filePath = configuration.get("FILE_PATH");


if (CoreUtils.isNullOrEmpty(connectionString)) {
throw new IllegalStateException("Environment variable STORAGE_CONNECTION_STRING must be set");
}

if (CoreUtils.isNullOrEmpty(filePath)) {
throw new IllegalStateException("Environment variable 'FILE_PATH' must be set");
}


// Set up the service client builder
blobClientBuilder = new BlobClientBuilder()
.connectionString(connectionString);
blobServiceClientBuilder = new BlobServiceClientBuilder().connectionString(connectionString);
}
}

0 comments on commit 6a46c09

Please sign in to comment.