|
| 1 | +// ----------------------------------------------------------------------------------------- |
| 2 | +// <copyright file="BlobsGettingStarted.cpp" company="Microsoft"> |
| 3 | +// Copyright 2013 Microsoft Corporation |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// </copyright> |
| 16 | +// ----------------------------------------------------------------------------------------- |
| 17 | + |
| 18 | +#include "samples_common.h" |
| 19 | + |
| 20 | +#include <thread> |
| 21 | +#include <chrono> |
| 22 | +#include <random> |
| 23 | +#include <algorithm> |
| 24 | + |
| 25 | +#include <was/storage_account.h> |
| 26 | +#include <was/blob.h> |
| 27 | +#include <cpprest/filestream.h> |
| 28 | +#include <cpprest/containerstream.h> |
| 29 | + |
| 30 | +namespace azure { namespace storage { namespace samples { |
| 31 | + |
| 32 | + SAMPLE(BlobsPerformanceBenchmark, blobs_performance_benchmark) |
| 33 | + void blobs_performance_benchmark() |
| 34 | + { |
| 35 | + try |
| 36 | + { |
| 37 | + azure::storage::cloud_storage_account storage_account = azure::storage::cloud_storage_account::parse(storage_connection_string); |
| 38 | + |
| 39 | + azure::storage::cloud_blob_client blob_client = storage_account.create_cloud_blob_client(); |
| 40 | + azure::storage::cloud_blob_container container = blob_client.get_container_reference(_XPLATSTR("benchmark-container")); |
| 41 | + |
| 42 | + container.create_if_not_exists(); |
| 43 | + |
| 44 | + const uint64_t blob_size = 1024 * 1024 * 1024; |
| 45 | + const int parallelism = 20; |
| 46 | + |
| 47 | + std::vector<uint8_t> buffer; |
| 48 | + buffer.resize(blob_size); |
| 49 | + |
| 50 | + std::mt19937_64 rand_engine(std::random_device{}()); |
| 51 | + std::uniform_int_distribution<uint64_t> dist; |
| 52 | + std::generate(reinterpret_cast<uint64_t*>(buffer.data()), reinterpret_cast<uint64_t*>(buffer.data() + buffer.size()), [&dist, &rand_engine]() { return dist(rand_engine); }); |
| 53 | + |
| 54 | + azure::storage::blob_request_options options; |
| 55 | + options.set_parallelism_factor(8); |
| 56 | + options.set_use_transactional_crc64(false); |
| 57 | + options.set_use_transactional_md5(false); |
| 58 | + options.set_store_blob_content_md5(false); |
| 59 | + options.set_stream_write_size_in_bytes(32 * 1024 * 1024); |
| 60 | + |
| 61 | + std::vector<pplx::task<void>> tasks; |
| 62 | + |
| 63 | + auto start = std::chrono::steady_clock::now(); |
| 64 | + for (int i = 0; i < parallelism; ++i) |
| 65 | + { |
| 66 | + auto blob = container.get_block_blob_reference(_XPLATSTR("blob") + utility::conversions::to_string_t(std::to_string(i))); |
| 67 | + auto task = blob.upload_from_stream_async(concurrency::streams::container_buffer<std::vector<uint8_t>>(buffer).create_istream(), blob_size, azure::storage::access_condition(), options, azure::storage::operation_context()); |
| 68 | + tasks.emplace_back(std::move(task)); |
| 69 | + } |
| 70 | + for (auto& t : tasks) |
| 71 | + { |
| 72 | + t.get(); |
| 73 | + } |
| 74 | + auto end = std::chrono::steady_clock::now(); |
| 75 | + |
| 76 | + double elapsed_s = double(std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()) / 1000; |
| 77 | + uint64_t data_mb = blob_size * parallelism / 1024 / 1024; |
| 78 | + |
| 79 | + std::cout << "Uploaded " << data_mb << "MB in " << elapsed_s << " seconds, throughput " << data_mb / elapsed_s << "MBps" << std::endl; |
| 80 | + |
| 81 | + tasks.clear(); |
| 82 | + start = std::chrono::steady_clock::now(); |
| 83 | + { |
| 84 | + std::vector<concurrency::streams::container_buffer<std::vector<uint8_t>>> download_buffers; |
| 85 | + for (int i = 0; i < parallelism; ++i) |
| 86 | + { |
| 87 | + auto blob = container.get_block_blob_reference(_XPLATSTR("blob") + utility::conversions::to_string_t(std::to_string(i))); |
| 88 | + download_buffers.emplace_back(concurrency::streams::container_buffer<std::vector<uint8_t>>()); |
| 89 | + auto task = blob.download_to_stream_async(download_buffers.back().create_ostream(), azure::storage::access_condition(), options, azure::storage::operation_context()); |
| 90 | + tasks.emplace_back(std::move(task)); |
| 91 | + } |
| 92 | + for (auto& t : tasks) |
| 93 | + { |
| 94 | + t.get(); |
| 95 | + } |
| 96 | + end = std::chrono::steady_clock::now(); |
| 97 | + } |
| 98 | + |
| 99 | + elapsed_s = double(std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()) / 1000; |
| 100 | + |
| 101 | + std::cout << "Downloaded " << data_mb << "MB in " << elapsed_s << " seconds, throughput " << data_mb / elapsed_s << "MBps" << std::endl; |
| 102 | + } |
| 103 | + catch (const azure::storage::storage_exception& e) |
| 104 | + { |
| 105 | + std::cout << e.what() << std::endl; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | +}}} // namespace azure::storage::samples |
0 commit comments