#!/usr/bin/perl -w
#!perl
#!/usr/bin/perl

#############################################################################
# Copyright 2009                                                            #
# Purpose: Generate large text file.                                        #
#                                                                           #
#############################################################################

#############################################################################
#                             PIL                                           #
#                 Software Development Group                                #
#                        Peter Mortensen                                    #
#                  E-mail: mortens.spammaygohere@drmortensen.eu             #
#                   WWW: http://www.pil.sdu.dk/                             #
#                                                                           #
#  Program for ......................................                       #
#                                                                           #
#    FILENAME:   generate.pl                                                #
#                                                                           #
# CREATED: PM 2009-08-29   Vrs. 1.0.                                        #
# UPDATED: PM 2009-09-03   Cleaned up.                                      #
#                                                                           #
#                                                                           #
#############################################################################

#Future:
# 1.


require 5.002;
use strict;
use warnings;
use diagnostics;

my $MBtargetSize;
my $outPutFileName;


print "\n\n--------------------- start ----------------------------------\n";

#For using parameters from AppleScript and Windows NT
if  ( $ENV{MBSIZE} )
{
  $MBtargetSize = $ENV{'MBSIZE'};
  print  "Approximate size: $MBtargetSize\n";

  $outPutFileName = $ENV{'OUTFILE'};
  print  "Output file: $outPutFileName\n";

  print  "\n";
}
else
{
  print  "\n\n\nNo environment variables detected.... Bailing out. Bye-bye!\n\n\n";
  die;
}

open ( NUMBERS_OUTFILE,">$outPutFileName") || die "Could not open output file $outPutFileName\n\n";

my $end2 = 0;

my $lineCount = 0;
my $sizeInBytes = 0;
my $sampleLineLength = 0;

my $linePower = 11; #As it is the line length depends exponentially on this
                    #number...
                    #
                    #  4: approx  250 characters
                    #  6: approx 1000 characters   38 MB/secs until 2 GB written.
                    #                              Long delay and then continues at 38 MB/secs.
                    #  8: approx 4100 characters   74 MB/secs until 2 GB written.
                    #  9: approx 8500 characters   97 MB/secs until 1 GB written.
                    #                              11 secs, then 23 secs delay.
                    # 10: approx 17000 characters  117 MB/secs until 2.3 GB written.
                    #                               20 secs, then 56 secs delay.
                    # 11: approx 34000 characters  130 MB/secs until 1.2 GB written.
                    #                               9 secs, then 31 secs delay.
                    #                              130 MB/secs until 2.7 GB written.


my $downCounterStartValue = 2000; #This value will change dynamically during 
                                  #the run. This particular start value is 
                                  #based on experience running it on a 
                                  #particular system. It should not be set 
                                  #too high, otherwise the interval between 
                                  #the few first progress lines will be too high.

my $downCounter = $downCounterStartValue;

my $startTime = time();

my $oldTime = 0; #For .
my $oldProgressTime = $oldTime;

my $progressInterval = 2; #Unit: seconds.
$progressInterval = 1;

while (! $end2)
{
	if ($downCounter > 0)
	{
		my $line = "";

		#Output one line to the file.

		my $i;
		for ($i = 0; $i < $linePower; $i++)
		{
			my $rn = rand 10;
			my $rnStr = "$rn ";

			#Append and double line (in order to get very long line for minimum overhead).
			$line = $line . $line . $rnStr;

			$lineCount++;
		} #for

		$line = $line . "\n";
		my $lineLength = length( $line);
		$sizeInBytes += $lineLength;
		$sampleLineLength = $lineLength;
		
		#Sample start of $line: 
		#
		#2.94036865234375 2.94036865234375 3.19793701171875 2.94036865234375 2.94036865234375 3.19793701171875 5.66619873046875
		#
		print NUMBERS_OUTFILE $line;
	}
	else
	{
		my $currentTime = time();
		my $timeSinceLastZeroCount = $currentTime - $oldTime;
		my $totalTimeElapsed = $currentTime - $startTime;

		if ( $timeSinceLastZeroCount < $progressInterval)
		{
			#Operation time was too low (too high overheadsystem call overf), increase $downCounter
			$downCounterStartValue = ($downCounterStartValue * 3) / 2;
		}
		else
		{
			if ($timeSinceLastZeroCount > $progressInterval)
			{
				$downCounterStartValue = ($downCounterStartValue * 3) / 4;
			}
			else
			{
				#Right on the money. Keep using the current value.
			}
		}
		$oldTime = $currentTime;

	    #my $timeSinceLastProgress = $currentTime - $oldProgressTime;
        #
		#if ($timeSinceLastProgress >= )

        print "downCounterStartValue: $downCounterStartValue. Sample line length: $sampleLineLength.\n";
		if ($totalTimeElapsed > 0)
		{
			my $sizeInMegaBytes = $sizeInBytes / 1024 / 1024;
			my $writeRate = $sizeInMegaBytes / $totalTimeElapsed;
	        print "After $totalTimeElapsed seconds: $lineCount lines written ($sizeInMegaBytes MB). $writeRate MB/s\n\n";

	        if ( $sizeInMegaBytes > $MBtargetSize)
			{
				$end2 = 1;
			}
		}

		$downCounter = $downCounterStartValue;
	} #Downcounter reached zero.

	$downCounter--;
} #while

close NUMBERS_OUTFILE;

print "\n\n--------------------- end ----------------------------------\n";