簡體   English   中英

Azure Blob 存儲讀取時間非常慢

[英]Azure Blob storage very slow read time

我將 JSON 數據保存為 Azure Blob 存儲 - 標准層中的塊 Blob。 文件大小為 14.5MB,它包含大約 25,000 個 OLHC 數據對象,我從位於同一區域的 Azure Function 訪問 blob。 該代碼只是讀取 blob 進行反序列化,但需要 20-40 秒。 有什么我錯過的嗎?

    public static async Task<Stream> GetBlob(string ConnectionString, string ContainerName, string Path)
    {
        BlobClient blobClient = new BlobClient(ConnectionString, ContainerName, Path);
        MemoryStream ms = new MemoryStream();

        try
        {
            await blobClient.DownloadToAsync(ms);
            ms.Seek(0, SeekOrigin.Begin);
            return ms;
        } catch (Exception ex)
        {
            ms.Dispose();
            throw;
        }        
    }

我請求 function 中的 blob

        log.LogInformation($"Begin Downloading Blob ");
        using (Stream blob = await Core.Azure.Blob.GetBlob(blobString, "containerName", fileName))
        {
            log.LogInformation($"End Downloading Blob ");
            log.LogInformation($"Begin Reading Blob ");
            using (StreamReader reader = new StreamReader(blob))
            {
                string json = await reader.ReadToEndAsync();
                log.LogInformation($"Begin Deserialize Blob ");
                sticks = JsonConvert.DeserializeObject<List<MyModel>>(json);
                log.LogInformation($"End Deserialize Blob ");
            }
        }
        log.LogInformation($"{symbol} End Get Blob ");

檢查 Blob 是否存在 Function

    public static async Task<bool> CheckExists(string ConnectionString, string ContainerName, string Path)
    {
        BlobClient blobClient = new BlobClient(ConnectionString, ContainerName, Path);
        return await blobClient.ExistsAsync();
    }

這是計時最長 47 秒的結果

我切換到 stream 閱讀器和 JSON 閱讀器,它下降到 10-30 秒.. 但仍然是很長的時間

我在這里添加了時間

2021-01-09 23:53:26.656 開始下載 Blob
2021-01-09 23:53:30.163 結束下載 Blob
2021-01-09 23:53:30.163 開始閱讀 Blob
2021-01-09 23:53:37.040 開始反序列化 Blob
2021-01-09 23:53:49.737 結束反序列化 Blob

另一個運行
OHLCData.Json 14.44 MB(28,000 行)

2021-01-10 12:40:49.970 開始檢查 Blob 是否存在
2021-01-10 12:40:58.962 結束檢查 Blob 存在
2021-01-10 12:40:58.962 開始下載 Blob
2021-01-10 12:41:08.181 結束下載 Blob
2021-01-10 12:41:08.187 開始閱讀 Blob
2021-01-10 12:41:25.713 開始反序列化 Blob
2021-01-10 12:41:33.817 結束反序列化 Blob
2021-01-10 12:41:33.817 結束獲取 Blob

您正在將整個 blob 下載到 memory stream (不必要的額外 memory 殺死),轉換為字符串然后反序列化它。 I would rather do it directly from blob stream in one shot leveraging the stream support of Newtonsoft.Json like below to optimize speed and memory use.

BlobClient blobClient = new BlobClient(ConnectionString, ContainerName, Path);
using (var stream = await blobClient.OpenReadAsync())
using (var sr = new StreamReader(stream))
using (var jr = new JsonTextReader(sr))
{
    result = JsonSerializer.CreateDefault().Deserialize<T>(jr);
}

您也可以使用System.Text.Json API 進行類似操作。

JsonSerializerOptions Options = new JsonSerializerOptions();
using (var stream = await blobClient.OpenReadAsync())
{
    result = await JsonSerializer.DeserializeAsync<T>(stream , Options);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM