Introduction
In my personal opinion, JSR223 Sampler in JMeter allows me to do anything literally! It kinds of opens the window of JVM for me to access through and fiddle with through use of other scripting languages, preferably Groovy. It helps me to quickly hack process, control flow of my scripting journey.
Scenario
Recently, we needed to perform an end-to-end test where we had to manually move some files to a DFS location. This took a long time because moving the files to the DFS location, especially the big ones, took a lot of time. I decided to create a custom program to move files from my local drive to the DFS storage on JSR223 Sampler. Access the code below:
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardCopyOption
// Define the source and destination directories
def sourceDir = 'C:/path/to/source/directory'
def destDir = 'C:/path/to/destination/directory'
// Create Paths for the source and destination directories
def sourcePath = Paths.get(sourceDir)
def destPath = Paths.get(destDir)
// Ensure the destination directory exists, create if it doesn't
if (!Files.exists(destPath)) {
Files.createDirectories(destPath)
}
// Create a directory stream to iterate over the files one by one
def stream = Files.newDirectoryStream(sourcePath, "*.pdf")
for (file in stream) {
if (Files.isRegularFile(file)) {
def targetFile = destPath.resolve(file.getFileName())
if (!Files.exists(targetFile)) {
Files.move(file, targetFile)
log.info("Moved file: ${file.getFileName().toString()}")
} else {
log.info("File already exists and will not be moved: ${file.getFileName().toString()}")
}
}
}
log.info("All PDF files moved successfully.")
Explanation
1. Import Necessary Classes:
Files,Paths, andStandardCopyOptionfromjava.nio.fileare imported to handle file operations.
2. Define Source and Destination Directories:
- The source and destination directories are defined as strings. Replace
'C:/path/to/source/directory'and'C:/path/to/destination/directory'with your actual directory paths.
3. Create Paths:
Paths.get(sourceDir)andPaths.get(destDir)createPathobjects for the source and destination directories.
4. Ensure Destination Directory Exists:
- If the destination directory does not exist, it is created using
Files.createDirectories(destPath).
5. Move XML Files One by One:
Files.newDirectoryStream(sourcePath, "*.xml")creates a directory stream to list XML files one by one.- The
forloop iterates over each file in the stream. - Each entry is checked to see if it is a regular file using
Files.isRegularFile(file). targetFileis the path to the destination file.- Before moving the file, the script checks if
targetFilealready exists usingFiles.exists(targetFile). - If the file does not exist, it moves the file using
Files.move(file, targetFile). - If the file already exists, it logs a message indicating that the file will not be moved.
6. Log the information:
- log.info() statements are used to log the moved files and any files that were not moved because they already existed in the destination directory.
Conclusion
In summary, using the JSR223 Sampler in JMeter with Groovy scripts helped make a custom program that easily transfers files from a local drive to DFS storage. Using the strength of JVM and Groovy made the process faster, leading to much less time needed to transfer files. The step-by-step explanation made it easy to understand what to do, from bringing in the needed parts to recording details while moving the file. In general, the JSR223 Sampler’s flexibility, along with Groovy scripting, made it easy to manage file transfers. This shows how powerful these tools can be for complicated testing situations.
Let me know in the comments how would you utilize JSR223 Sampler.
Stay Curious!
Leave a Reply