Class: LoadTester::FileDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/loadtester/filedefinition.rb

Overview

A File with random contents of given size

Constant Summary collapse

RANDOM_GENERATOR =
Random.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, destination, size, file_source_name, file_destination_name, type) ⇒ FileDefinition

Returns a new instance of FileDefinition.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/loadtester/filedefinition.rb', line 10

def initialize(source, destination, size, file_source_name,
               file_destination_name, type)
  self.source = source
  self.destination = destination
  self.size = size
  self.file_source_name = file_source_name
  self.file_destination_name = file_destination_name
  self.type = type
  populate unless valid?
  self.load! if type == :write
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



6
7
8
# File 'lib/loadtester/filedefinition.rb', line 6

def data
  @data
end

#destinationObject

Returns the value of attribute destination.



6
7
8
# File 'lib/loadtester/filedefinition.rb', line 6

def destination
  @destination
end

#file_destination_nameObject

Returns the value of attribute file_destination_name.



6
7
8
# File 'lib/loadtester/filedefinition.rb', line 6

def file_destination_name
  @file_destination_name
end

#file_source_nameObject

Returns the value of attribute file_source_name.



6
7
8
# File 'lib/loadtester/filedefinition.rb', line 6

def file_source_name
  @file_source_name
end

#run_timeObject

Returns the value of attribute run_time.



6
7
8
# File 'lib/loadtester/filedefinition.rb', line 6

def run_time
  @run_time
end

#sizeObject

Returns the value of attribute size.



6
7
8
# File 'lib/loadtester/filedefinition.rb', line 6

def size
  @size
end

#sourceObject

Returns the value of attribute source.



6
7
8
# File 'lib/loadtester/filedefinition.rb', line 6

def source
  @source
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/loadtester/filedefinition.rb', line 6

def type
  @type
end

Instance Method Details

#generateObject



44
45
46
47
48
49
50
51
# File 'lib/loadtester/filedefinition.rb', line 44

def generate
  mkdir_p File.dirname src
  random_string = ''
  size.to_i.times do
    random_string += RANDOM_GENERATOR.rand(256).chr
  end
  IO.binwrite src, random_string
end

#load!Object



66
67
68
69
# File 'lib/loadtester/filedefinition.rb', line 66

def load!
  self.data = File.read src
  mkdir_p File.join(destination, size.pretty)
end

#mkdir_p(path) ⇒ Object

Because FileUtils has too many dependencies on stupid Things like ImageMagick



24
25
26
27
28
29
30
31
32
# File 'lib/loadtester/filedefinition.rb', line 24

def mkdir_p(path)
  path = File.expand_path path
  path_parts = path.split File::SEPARATOR
  path_so_far = File::SEPARATOR
  path_parts.each do |part|
    path_so_far = File.join path_so_far, part
    Dir.mkdir(path_so_far) unless Dir.exist? path_so_far
  end
end

#populateObject



34
35
36
37
38
39
40
41
42
# File 'lib/loadtester/filedefinition.rb', line 34

def populate
  mkdir_p File.dirname src
  mkdir_p File.dirname target
  generate unless File.exist? src
  File.open target, 'wb' do |fd|
    fd.write data
    fd.fsync
  end if type == :read
end

#readObject



71
72
73
74
75
76
# File 'lib/loadtester/filedefinition.rb', line 71

def read
  self.run_time = Benchmark.measure do
    IO.binread target
  end
  run_time.real
end

#srcObject



53
54
55
56
57
# File 'lib/loadtester/filedefinition.rb', line 53

def src
  File.join source,
            size.pretty,
            file_source_name.to_s
end

#targetObject



59
60
61
62
63
64
# File 'lib/loadtester/filedefinition.rb', line 59

def target
  File.join destination,
            type.to_s,
            size.pretty,
            file_destination_name.to_s
end

#valid?Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
# File 'lib/loadtester/filedefinition.rb', line 88

def valid?
  if type == :read
    File.exist? target
  else
    File.exist?(src) && File.directory?(File.dirname(target))
  end
end

#writeObject



78
79
80
81
82
83
84
85
86
# File 'lib/loadtester/filedefinition.rb', line 78

def write
  self.run_time = Benchmark.measure do
    File.open(target, 'wb') do |fd|
      fd.write data
      fd.fsync
    end
  end
  run_time.real
end