Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby1.9 -wKU
- # encoding: UTF-8
- #
- # !!! enforce line endings LF only !!!
- # shell may fail to correctly recognize interpreter if CR precedes or replaces LF
- #
- #---------#---------#---------#---------#---------#---------#---------#---------#---------#---------#
- module RandomFile
- BUFFERDEFAULT=1024**2
- def self.min( *values )
- least = values[0]
- values.each do|v|
- least = v if v < least
- end
- #puts "min(#{values.inspect}) => #{least}"
- least
- end
- def self.max( *values )
- most = values[0]
- values.each do|v|
- most = v if v > most
- end
- #puts "max(#{values.inspect}) => #{most}"
- most
- end
- def self.randominteger( floor, ceiling )
- (low,high)=[floor,ceiling].sort
- r = low + rand(high-low+1)
- #puts "random in [ #{low}, #{high} ] => #{r}"
- r
- end
- class Status
- def initialize( size, parent=nil )
- @parent = parent
- @size = size.to_i
- @done = 0
- #puts inspect
- end
- def add( change )
- #puts "<Status #{object_id.to_s(16)}: (#{@done}+#{change})/#{@size} = #{progress(change)} in #{@parent.inspect}>"
- @done += change
- @parent.add( change ) unless nil==@parent
- #puts inspect
- end
- def progress( plus=0 )
- (@done.to_f+plus)/@size
- end
- def percent( plus=0 )
- progress(plus)*100
- end
- def inspect
- "<Status #{object_id.to_s(16)}: #{@done}/#{@size} = #{progress}, #{percent}%, in #{@parent.inspect}>"
- end
- end
- def self.randomstring( count, buffer="", pstat=nil )
- tobuff = count
- status = Status.new( count, pstat )
- #puts "randomstring( #{count} ) @ #{buffer.length}+#{tobuff} :: starting"
- [[8,2**64-1,"Q"],[4,2**32-1,"L"],[2,2**16-1,"S"],[1,2**8-1,"C"]].each do |bytes,ceiling,pcode|
- #puts "randomstring( #{count} ) @ #{buffer.length}+#{tobuff} :: code #{pcode} for #{bytes}-byte substrings in [0..#{ceiling}]"
- while bytes <= tobuff
- b = [randominteger(0,ceiling)].pack(pcode)
- buffer << b
- tobuff -= b.length
- status.add(b.length)
- #puts "randomstring( #{count} ) @ #{buffer.length}+#{tobuff} :: added #{b.length}, #{status.inspect}"
- end
- end
- #puts "randomstring( #{count} ) @ #{buffer.length}+#{tobuff} :: done"
- buffer
- end
- def self.randomoutput( count, outstream=STDOUT, buffersize=BUFFERDEFAULT, pstat=nil )
- towrite = count
- buffsize = min( towrite, buffersize )
- buffer = ""
- written = 0
- status = Status.new( count, pstat )
- #puts "randomoutput( #{count} ) @ #{written}+#{towrite} :: starting"
- while 0 < towrite
- randomstring( buffsize, buffer, status ) # fill buffer
- w = outstream.write(buffer) # write buffer
- towrite -= w # update remaining
- written += w
- #puts "randomoutput( #{count} ) @ #{written}+#{towrite} :: added #{buffer.length}, #{status.inspect}"
- buffsize = min( towrite, buffersize ) # shrink buffer near end
- buffer.clear # empty without deallocating
- end
- #puts "randomoutput( #{count} ) @ #{buffer.length}+#{towrite} :: done"
- towrite
- end
- def self.randomfile( count, filename, buffersize=BUFFERDEFAULT, pstat=nil )
- status = Status.new( count, pstat )
- #puts "randomfile( #{count}, #{name} ) :: starting"
- File.open( filename, "wb" ) do |filestream|
- randomoutput( count, filestream, buffersize, status )
- end
- #puts "randomfile( #{count}, #{name} ) :: done"
- end
- end
- if $0 == __FILE__
- sizes = ARGV
- sizes = [RandomFile::BUFFERDEFAULT] if 0 == sizes.length
- sizes.each do |s|
- size = s.to_i
- next if 0 == size
- name = "randomfile-#{size}.noise"
- status = RandomFile::Status.new( size )
- #puts "generating #{size} bytes of noise in #{name}"
- t = Thread.new do
- pnew=0.0
- dnew=0
- fmt = "\r%3.0f%% %1s "
- stc = ["-","\\","|","/"]
- i = 0
- c = stc[i]
- until 100 == pnew do
- pold=pnew
- dold=dnew
- sleep 0.25
- pnew = status.percent
- if [ 0, pold ].include?( pnew )
- dnew = dold
- c = "?"
- elsif 100 == pnew
- dnew = dold
- c = " "
- else
- dnew = RandomFile::max(dold,-Math.log10(pnew-pold).floor)
- fmt = "\r%#{dnew+4}.#{dnew}f%% %1s " unless dnew == dold
- i = i+1
- i = 0 if i >= stc.length
- c = stc[i]
- end
- print fmt % [ pnew, c ]
- end
- puts
- end
- RandomFile.randomfile( size, name, 2**24-1, status )
- t.join
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement