Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Effective way to generate random strings up to 100MB for test in PHP?

Tags:

php

random

For testing purposes I need a function like this:

/**
 *  @param int $sizeInBytes
 *
 * @returns string with random data
 */
function randomData($sizeInBytes)
{
 ...
}

Any ideas of a effective implementation? There is need for speed but not for real randomness (more a kind of "lorem ipsum"). My simplest idea would be to use real large file in the filesystem and fetch the required size by stream. But this needs at least a 100MB file. Is there a better way?

like image 979
Marko Avatar asked Jan 02 '12 14:01

Marko


4 Answers

How about just creating a very long string if you have the memory available anyways?

That should not take all that long :)

 $x = str_repeat(
     'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sollicitudin turpis ut augue lacinia at ullamcorper dolor condimentum. Nunc elementum suscipit laoreet. Phasellus vel sem justo, a vulputate arcu. Sed rutrum elit nec elit lobortis ultrices. Quisque elit nulla, rutrum et varius sit amet, pulvinar eget purus. Aliquam erat volutpat. Fusce turpis lectus, vestibulum sed ornare sed, facilisis sit amet lacus. Nunc lobortis posuere ultricies. Phasellus aliquet cursus gravida. Curabitur eu erat ac augue rutrum mattis. Suspendisse sit amet urna nec velit commodo feugiat. Maecenas vulputate dictum diam, eu tempor erat volutpat in. Donec id nulla tortor, nec iaculis nibh. Pellentesque scelerisque nisl sit amet ligula dictum commodo. Donec porta mi in lorem porttitor id suscipit lacus auctor.', 
     125000
 );

You could of course just write that to a file one but creating it in memory doesn't really take all that long.

The code above produces an 98MB string in about 100ms and creating a 200MB string takes about 170ms on my box. That should be good enough for most cases.


As noted in the comment below: You might have to change your php.ini setting if you limit the amount of memory your script is allowed to consume (or change it via memory_limit('...');). Also strings > 1.5gb might cause issues but thats not a concern here I'd say.

like image 166
edorian Avatar answered Nov 11 '22 23:11

edorian


If you are in a Unix environment, you can use the file /dev/random to pull as many megabytes as you want from it.

like image 2
kikito Avatar answered Nov 11 '22 22:11

kikito


Well, if you want random text, then you can use a dictionary with the words and another dictionary with the punctuation, and then generate the return string with random elements from the word dictionary, with a certain probability of random elements from the punctuation dictionary.

Like this you only need the dictionary's in memory, but it will be heavier on the server's CPU.

You can also use this method in conjunction with what you purposed, having a small dictionary with sentences, and randomly selecting sentences or even paragraphs.

like image 1
Lee Scott Avatar answered Nov 11 '22 23:11

Lee Scott


Why not use an actual lipsum generator script, such as this one?

like image 1
Mark Baker Avatar answered Nov 12 '22 00:11

Mark Baker