Efficient way to generate random strings up to 100 MB for testing in PHP?

For testing purposes, I need a function like this:

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

Any ideas for effective implementation? There is a need for speed, but not for real chance (more of a kind of "lorem ipsum"). My simplest idea is to use a real large file in the file system and get the required stream size. But this requires at least 100 MB of file. Is there a better way?

+4
source share
5 answers

How to simply create a very long line if you have available memory?

It should not last so 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 ); 

Of course, you can simply write this to a file, but creating it in memory is actually not so long.

The above code creates a 98MB line 98MB about 100ms , and creating a 200MB line takes about 170ms in my field. This should be good enough for most cases.


As noted in the comment below: You may need to change the php.ini setting if you limit the amount of memory your script can use (or change it with memory_limit('...'); ). Also, strings> 1.5gb can cause problems, but I'm not talking about that.

+9
source

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

+2
source

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

Similarly, you only need a dictionary in memory, but it will be heavier on the server processor.

You can also use this method in combination with what you intend, with a small vocabulary with sentences and a random selection of sentences or even paragraphs.

+1
source

Why not use the actual sponge generator script like this

+1
source

On linux, you can read / dev / random, and I'm not quite sure, but you can use http://php.net/manual/en/function.fseek.php to read a specific one or use http://php.net/ manual / en / function.exec.php to create a file and then read it.

+1
source

Source: https://habr.com/ru/post/1388941/


All Articles