# Copyright (C) The Arvados Authors. All rights reserved.
#
# SPDX-License-Identifier: AGPL-3.0

require 'safe_json'
require 'test_helper'
require 'helpers/manifest_examples'
require 'helpers/time_block'

class CollectionsApiPerformanceTest < ActionDispatch::IntegrationTest
  include ManifestExamples

  slow_test "crud cycle for a collection with a big manifest" do
    bigmanifest = time_block 'make example' do
      make_manifest(streams: 100,
                    files_per_stream: 100,
                    blocks_per_file: 20,
                    bytes_per_block: 2**26,
                    api_token: api_token(:active))
    end
    json = time_block "JSON encode #{bigmanifest.length>>20}MiB manifest" do
      SafeJSON.dump({"manifest_text" => bigmanifest})
    end
    time_block 'create' do
      post '/arvados/v1/collections',
        params: {collection: json},
        headers: auth(:active)
      assert_response :success
    end
    uuid = json_response['uuid']
    time_block 'read' do
      get '/arvados/v1/collections/' + uuid, params: {}, headers: auth(:active)
      assert_response :success
    end
    time_block 'list' do
      get '/arvados/v1/collections',
        params: {select: ['manifest_text'], filters: [['uuid', '=', uuid]].to_json},
        headers: auth(:active)
      assert_response :success
    end
    time_block 'update' do
      put '/arvados/v1/collections/' + uuid,
        params: {collection: json},
        headers: auth(:active)
      assert_response :success
    end
    time_block 'delete' do
      delete '/arvados/v1/collections/' + uuid, params: {}, headers: auth(:active)
    end
  end

  slow_test "memory usage" do
    hugemanifest = make_manifest(streams: 1,
                                 files_per_stream: 2000,
                                 blocks_per_file: 200,
                                 bytes_per_block: 2**26,
                                 api_token: api_token(:active))
    json = time_block "JSON encode #{hugemanifest.length>>20}MiB manifest" do
      SafeJSON.dump({manifest_text: hugemanifest})
    end
    vmpeak "post" do
      post '/arvados/v1/collections',
        params: {collection: json},
        headers: auth(:active)
    end
  end
end
