从不同目录获取图像文件的多个列表

huangapple go评论46阅读模式
英文:

Multiple list for fetching image file from different directory

问题

以下是代码的翻译部分,不包括代码本身:

// 导入文件类型库
import groovy.io.FileType

// 从变量中获取文件夹名
def dir = new File(vars.get("dirName"))

// 选择模式的模式数组
def selectionPattern = ["All", "Single"]

// 初始化各种列表
def list = []
def listNew = []
def list_series = []
def list_images = []
def modalityDir = []
def modalityFileName = []
def finalModalityName = []
def rnd = new Random()
def rndSelect = new Random()

// 随机选择模式
def userSelectionForModalityFolder = selectionPattern[rndSelect.nextInt(selectionPattern.size())]

if (userSelectionForModalityFolder === "All") {
    // 遍历主文件夹
    dir.eachDir() { dirList ->
        list << dirList
    }

    // 随机选择一个主文件夹
    def randomParentDirectorySelected = list[rnd.nextInt(list.size())]

    // 遍历所选主文件夹的子文件夹
    randomParentDirectorySelected.eachDir() { listDir ->
        list_series << listDir
    }

    // 遍历所有子文件夹
    for (allSelectionDir in list_series) {
        allSelectionDir.eachDir() { allSelectDirectory ->
            list_images << allSelectDirectory
        }
    }

    // 获取所有图像文件
    for (int b = 0; b < list_images.size(); b++) {
        File[] modalityFiles = list_images[b].listFiles()
        modalityFileName << modalityFiles
    }

    // 将所有图像文件名加入最终列表
    for (int n = 0; n < modalityFileName.size(); n++) {
        for (int p = 0; p < modalityFileName[n].size(); p++) {
            finalModalityName << modalityFileName[n][p]
        }
    }

    // 将最终列表放入变量
    vars.putObject("All_Modality", finalModalityName)
} else {
    // 遍历主文件夹
    dir.eachDir() { dirList ->
        list << dirList
    }

    // 随机选择一个主文件夹
    def randomParentDirectorySelected = list[rnd.nextInt(list.size())]

    // 遍历所选主文件夹的子文件夹
    randomParentDirectorySelected.eachDir() { secDirList ->
        listNew << secDirList
    }

    if (listNew.size() > 2) {
        def totalFile = ranSelect.nextInt(listNew.size())
    } else {
        Random ranSelect = new Random()
        def totalFile = ranSelect.nextInt(listNew.size() + 4)

        // 随机选择一个文件夹
        def random_ModalitySelection = listNew[rnd.nextInt(listNew.size())]

        // 遍历所选文件夹的子文件夹
        random_ModalitySelection.eachDir() { modalityD ->
            modalityDir << modalityD
        }

        // 获取所有图像文件
        for (int i = 0; i < modalityDir.size(); i++) {
            File[] modalityFiles = modalityDir[i].listFiles()
            modalityFileName << modalityFiles
        }

        // 将所有图像文件名加入最终列表
        for (int j = 0; j < modalityFileName.size(); j++) {
            for (int k = 0; k < modalityFileName[j].size(); k++) {
                finalModalityName << modalityFileName[j][k]
            }
        }

        // 将最终列表放入变量
        vars.putObject("All_Modality", finalModalityName)
    }
}

希望这有所帮助。如果您有任何其他问题,请随时提出。

英文:

Have different images in various folder categorized according to the size of the folder. And Each folder have multiple directory like CX and MX OR VX. Each of this CX/MX/VX have SER00001, SER00002, SER00003 And each of this folder have all the images.

Use Case:

  1. Select any random directory of any size, like 5MB or 30MB or 1GB
  2. Each 5MB/30MB/1Gb folder have folder like CX, MX or VX.
  3. Each CX/MX/VX have SER00001/SER00002/SER00003... folders.
  4. Each SER00001/SER00002/SER00003... folders contain the images.
  5. Based upon the selection like all or single the CX, MX or VX will be selected like for all all the CX,VX &amp; MX will be selected and inside all the folder eventually the images under it. Now for single only one from CX,VX and MX will be selected and and all the SER folder and eventually the images under it

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import groovy.io.FileType;
def dir = new File(vars.get(&quot;dirName&quot;));
def selectionPattern = [&quot;All&quot;,&quot;Single&quot;];
def list = [];
def listNew = [];
def list_series = [];
def list_images = [];
def modalityDir = [];
def modalityFileName = [];
def finalModalityName = [];
def rnd = new Random();
def rndSelect = new Random();
def userSelectionForModalityFolder = selectionPattern[rndSelect.nextInt(selectionPattern.size())];
if (userSelectionForModalityFolder === &quot;All&quot;) {
dir.eachDir() {
dirList -&gt;
list &lt;&lt; dirList
}
def randomParentDirectorySelected = list[rnd.nextInt(list.size())];
randomParentDirectorySelected.eachDir() {
listDir -&gt;
list_series &lt;&lt; listDir
}
for (allSelectionDir in list_series) {
allSelectionDir.eachDir() {
allSelectDirectory -&gt;
list_images &lt;&lt; allSelectDirectory
}
}
for (int b = 0; b &lt; list_images.size(); b++) {
File[] modalityFiles = list_images[b].listFiles();
modalityFileName &lt;&lt; modalityFiles
}
for (int n = 0; n &lt; modalityFileName.size(); n++) {
for (int p = 0; p &lt; modalityFileName[n].size(); p++) {
finalModalityName &lt;&lt; modalityFileName[n]

} } vars.putObject(&quot;All_Modality&quot;,finalModalityName); } else { dir.eachDir() { dirList -&gt; list &lt;&lt; dirList } def randomParentDirectorySelected = list[rnd.nextInt(list.size())] randomParentDirectorySelected.eachDir() { secDirList -&gt; listNew &lt;&lt; secDirList } if (listNew.size() &gt; 2) { def totalFile = ranSelect.nextInt(listNew.size()); } else { Random ranSelect = new Random(); def totalFile = ranSelect.nextInt(listNew.size() + 4); def random_ModalitySelection = listNew[rnd.nextInt(listNew.size())] random_ModalitySelection.eachDir() { modalityD -&gt; modalityDir &lt;&lt; modalityD } for (int i = 0; i &lt; modalityDir.size(); i++) { File[] modalityFiles = modalityDir[i].listFiles(); modalityFileName &lt;&lt; modalityFiles } for (int j = 0; j &lt; modalityFileName.size(); j++) { for (int k = 0; k &lt; modalityFileName[j].size(); k++) { finalModalityName &lt;&lt; modalityFileName[j][k] } } vars.putObject(&quot;All_Modality&quot;,finalModalityName); } }

<!-- end snippet -->

This code work fine but I have declared too many list inside my code, when I am running for large number of image file I am getting longer response in the code

答案1

得分: 1

  1. 我认为使用随机数据不是一个好主意,因为我认为测试需要是可重复的,而采用你的方法,如果你检测到性能问题,就必须采取额外的步骤来重新运行测试场景。
  2. 如果生成列表需要时间/资源,你可以将它移动到“setUp Thread Group”中,为每个虚拟用户预先生成文件列表并将其写入CSV文件。然后在主线程组中,你可以使用“CSV Data Set Config”,这将是最快的选项,并提供了第1点提到的可重复性。
英文:
  1. I don't think it's a good idea to use random data because I believe tests need to be repeatable and with your approach if you detect a performance issue you will have to take extra steps to replay the test scenario
  2. If generating the list is time/resource consuming you could move it to setUp Thread Group to pre-generate the list of files for each virtual user and write it to the CSV file. Then in the main Thread Group you can use CSV Data Set Config, it will be the fastest option and will give you the repeatability mentioned in the point 1.

huangapple
  • 本文由 发表于 2023年2月27日 07:37:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定