File: download.rb

package info (click to toggle)
ruby-mongo 2.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,020 kB
  • sloc: ruby: 110,810; makefile: 5
file content (76 lines) | stat: -rw-r--r-- 1,980 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# frozen_string_literal: true

require_relative 'base'
require_relative 'upload'
require_relative '../dispatcher'

module Mongo
  module DriverBench
    module Parallel
      module GridFS
        # This benchmark tests driver performance downloading files from
        # GridFS to disk.
        #
        # @api private
        class Download < Mongo::DriverBench::Parallel::GridFS::Base
          bench_name 'GridFS multi-file download'

          private

          # The source object to use for this benchmark. Each batch is a tuple
          # consisting of the list position, and the element in the list at
          # that position.
          #
          # @api private
          class Source
            def initialize(list)
              @list = list
              @n = 0
            end

            def next
              id = @list.pop or return nil
              [ @n, id ].tap { @n += 1 }
            end
          end

          def setup
            super
            prepare_bucket(initialize: false)

            dispatcher = Dispatcher.new(Upload::Source.new(self)) do |file_name|
              upload_file(file_name)
            end
            dispatcher.run

            @destination = File.join(Dir.tmpdir, 'parallel')
          end

          def before_task
            super
            FileUtils.rm_rf(@destination)
            FileUtils.mkdir_p(@destination)

            ids = bucket.files_collection.find.map { |doc| doc['_id'] }
            @dispatcher = Dispatcher.new(Source.new(ids)) do |(n, id)|
              download_file(n, id)
            end
          end

          def do_task
            @dispatcher.run
          end

          def download_file(index, id)
            path = File.join(@destination, file_name_at(index))
            FileUtils.mkdir_p(File.dirname(path))

            File.open(path, 'w') do |file|
              bucket.download_to_stream(id, file)
            end
          end
        end
      end
    end
  end
end