duancui19840401 2015-01-15 02:49
浏览 80
已采纳

在PHP中创建测试数据的数组

Firstly, I'm still a beginner to PHP so my terminology may be a bit wrong - please let me know and I'll amend the question.

Task: I have a function which I'm looking to test to see how long it takes to run at large scale. I need to pass it data in the following format:

$data = [
                [ 'A', 'B', 'C', 'D' ],
                [ 'C', 'B' ],
                [ 'C', 'B' ],
            ];

As you can see, the number of items in an array can vary - although they are drawn from an overall set (range of integers or letters).

For my testing purposes, I'd like to be able to change the number of items in each nested array.

I also need to be able to change how many arrays are created.

Example tests I'd like to perform e.g.

  • Run one test with a small number of arrays, but a large amount of data within each.
  • Run a second test with a large number of arrays, but a small amount of data in each
  • A third with huge numbers of items and arrays.

The story so far I was Googling and know I could use range() to create an array that count sequentially (or with a certain step). But I have to set the upper and lower bounds for each array.

I figure I could use a do.. while loop to add X number of arrays within $data, but I'm not sure how I can vary the amount of data within each array.

For the function to work, I need there to be either a letter or integer repeated. In other words: I couldn't have the first array count from 1-10, the next 11-21. It's as if all the data is drawn from the pool of integers 1-10,000,000.

Bonus points if the data can be randomized in order in each array.

Really appreciate any guidance and pointers on what to use / research - I'm sure this is a totally n00b question.

Many thanks in advance.

  • 写回答

1条回答 默认 最新

  • 普通网友 2015-01-15 03:07
    关注

    Generate a random range:

    range(mt_rand(0, 100), mt_rand(101, 1000))
    

    Generate an array of letters from a range (65 = A, 90 = Z):

    array_map('chr', range(65, 90))
    

    Generate a random order:

    $data = range(..);
    shuffle($data);
    

    Take a random slice of an array:

    $data = range(..);
    $data = array_slice($data, mt_rand(0, count($data) - 1), mt_rand(1, count($data)));
    

    Generate arrays of random length:

    for ($i = 0, $length = mt_rand(0, 100); $i < $length; $i++) {
        $data[] = ..;
    }
    

    You can nest two of those to generate randomly long arrays of randomly long arrays.

    Now combine all these techniques as needed to spit out the kind of test data you want.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源