-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathStoragePlayground.razor
More file actions
284 lines (241 loc) · 9.79 KB
/
StoragePlayground.razor
File metadata and controls
284 lines (241 loc) · 9.79 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
@page "/storage-playground"
@using System.Linq
@using System.Diagnostics
@using System.Text
@using ManagedCode.Storage.BrowserWasmHost
@inject IBrowserStorage Storage
@inject ILogger<StoragePlayground> Logger
<PageTitle>Storage Playground</PageTitle>
<main style="padding:24px;font-family:sans-serif;display:grid;gap:16px;max-width:960px;">
<h1>Browser Storage playground</h1>
<label for="directory-input">Directory</label>
<input id="directory-input" @bind="directory" />
<label for="file-name-input">File name</label>
<input id="file-name-input" @bind="fileName" />
<label for="content-input">Content</label>
<textarea id="content-input" rows="6" @bind="content"></textarea>
<label for="size-mib-input">Large payload size (MiB)</label>
<input id="size-mib-input" type="number" min="1" @bind="sizeMiB" />
<div style="display:flex;gap:12px;flex-wrap:wrap;">
<button id="save-text-button" @onclick="SaveTextAsync">Save Text</button>
<button id="load-text-button" @onclick="LoadTextAsync">Load Text</button>
<button id="save-large-button" @onclick="SaveLargeAsync">Save Large</button>
<button id="load-large-button" @onclick="LoadLargeAsync">Load Large</button>
<button id="exists-button" @onclick="CheckExistsAsync">Exists</button>
<button id="list-button" @onclick="ListAsync">List</button>
<button id="delete-button" @onclick="DeleteAsync">Delete</button>
</div>
<section>
<h2>Status</h2>
<pre id="status-output">@status</pre>
</section>
<section>
<h2>Exists</h2>
<pre id="exists-output">@existsOutput</pre>
</section>
<section>
<h2>Loaded content</h2>
<pre id="loaded-output">@loadedContent</pre>
</section>
<section>
<h2>Large payload</h2>
<pre id="large-output">@largeOutput</pre>
</section>
<section>
<h2>Files</h2>
<ul id="blob-list">
@foreach (var blob in blobs)
{
<li>@blob.FullName (@blob.Length bytes)</li>
}
</ul>
</section>
<VfsPlayground />
</main>
@code {
private const long ProgressLogIntervalBytes = 100L * 1024 * 1024;
private List<BlobMetadata> blobs = [];
private string content = "playwright-browser-storage-seed";
private string directory = "browser-storage-tests";
private string existsOutput = "unknown";
private string fileName = "playwright-browser-storage.txt";
private string largeOutput = "idle";
private string loadedContent = string.Empty;
private int sizeMiB = 20;
private string status = "ready";
protected override Task OnAfterRenderAsync(bool firstRender)
{
if (!firstRender)
return Task.CompletedTask;
status = "interactive-ready";
StateHasChanged();
return Task.CompletedTask;
}
private async Task SaveTextAsync()
{
var result = await Storage.UploadAsync(content, new UploadOptions { FileName = fileName, Directory = directory });
status = result.IsSuccess ? $"saved:{result.Value!.FullName}" : $"save-failed:{result.Problem?.Detail}";
}
private async Task LoadTextAsync()
{
var result = await Storage.GetStreamAsync(GetResolvedFileName());
if (result.IsFailed || result.Value is null)
{
loadedContent = string.Empty;
status = $"load-failed:{result.Problem?.Detail}";
return;
}
await using var stream = result.Value;
using var reader = new StreamReader(stream, Encoding.UTF8, leaveOpen: false);
loadedContent = await reader.ReadToEndAsync();
status = $"loaded:{GetResolvedFileName()}";
}
private async Task SaveLargeAsync()
{
try
{
status = "large-saving";
largeOutput = "saving";
await InvokeAsync(StateHasChanged);
var totalLength = (long)Math.Max(sizeMiB, 1) * 1024 * 1024;
var resolvedFileName = GetResolvedFileName();
var startedAt = Stopwatch.StartNew();
await using var stream = new DeterministicPayloadStream(
totalLength,
ComputeSeed($"{directory}|{fileName}|{sizeMiB}|storage"),
ProgressLogIntervalBytes,
bytesProcessed => ReportLargeSaveProgress(resolvedFileName, bytesProcessed, totalLength));
var result = await Storage.UploadAsync(stream, new UploadOptions
{
FileName = fileName,
Directory = directory,
MimeType = "application/octet-stream"
});
if (result.IsFailed)
{
largeOutput = $"generated:{stream.Position}";
status = $"large-save-failed:{result.Problem?.Detail}";
return;
}
if (!stream.IsCompleted)
{
largeOutput = $"generated:{stream.Position}";
status = $"large-save-failed:UnexpectedLength:{stream.Position}";
return;
}
var length = stream.Position;
var crc = stream.CompletedCrc;
largeOutput = $"expected:{length}:{crc}";
status = $"large-saved:{length}:{crc}";
Logger.LogInformation("Completed browser storage save for {FileName} ({Bytes} bytes, crc {Crc}) in {ElapsedMilliseconds} ms.",
resolvedFileName,
length,
crc,
startedAt.ElapsedMilliseconds);
}
catch (Exception ex)
{
largeOutput = "error";
status = $"large-save-failed:{ex.GetType().Name}:{ex.Message}";
}
}
private async Task LoadLargeAsync()
{
try
{
status = "large-loading";
largeOutput = "loading";
await InvokeAsync(StateHasChanged);
var resolvedFileName = GetResolvedFileName();
var startedAt = Stopwatch.StartNew();
var result = await Storage.GetStreamAsync(GetResolvedFileName());
if (result.IsFailed || result.Value is null)
{
largeOutput = "error";
status = $"large-load-failed:{result.Problem?.Detail}";
return;
}
await using var stream = result.Value;
var (length, crc) = await ComputeStreamDigestAsync(stream, bytesProcessed => ReportLargeLoadProgress(resolvedFileName, bytesProcessed));
largeOutput = $"actual:{length}:{crc}";
status = $"large-loaded:{length}:{crc}";
Logger.LogInformation("Completed browser storage load for {FileName} ({Bytes} bytes, crc {Crc}) in {ElapsedMilliseconds} ms.",
resolvedFileName,
length,
crc,
startedAt.ElapsedMilliseconds);
}
catch (Exception ex)
{
largeOutput = "error";
status = $"large-load-failed:{ex.GetType().Name}:{ex.Message}";
}
}
private async Task CheckExistsAsync()
{
var result = await Storage.ExistsAsync(new ExistOptions { FileName = fileName, Directory = directory });
existsOutput = result.IsSuccess ? result.Value.ToString() : "error";
status = result.IsSuccess ? $"exists:{result.Value}" : $"exists-failed:{result.Problem?.Detail}";
}
private async Task ListAsync()
{
blobs = await Storage.GetBlobMetadataListAsync(directory).ToListAsync();
status = $"listed:{blobs.Count}";
}
private async Task DeleteAsync()
{
var result = await Storage.DeleteAsync(new DeleteOptions { FileName = fileName, Directory = directory });
status = result.IsSuccess ? $"deleted:{result.Value}" : $"delete-failed:{result.Problem?.Detail}";
}
private string GetResolvedFileName()
{
return string.IsNullOrWhiteSpace(directory) ? fileName : $"{directory}/{fileName}";
}
private static int ComputeSeed(string seedInput)
{
var seed = 17;
foreach (var ch in seedInput)
seed = unchecked(seed * 31 + ch);
return seed;
}
private void ReportLargeSaveProgress(string resolvedFileName, long bytesProcessed, long totalLength)
{
Logger.LogInformation("Browser storage save progress for {FileName}: {ProcessedMiB}/{TotalMiB} MiB.",
resolvedFileName,
ToMiB(bytesProcessed),
ToMiB(totalLength));
status = $"large-saving:{ToMiB(bytesProcessed)}MiB";
_ = InvokeAsync(StateHasChanged);
}
private void ReportLargeLoadProgress(string resolvedFileName, long bytesProcessed)
{
Logger.LogInformation("Browser storage load progress for {FileName}: {ProcessedMiB} MiB.", resolvedFileName, ToMiB(bytesProcessed));
status = $"large-loading:{ToMiB(bytesProcessed)}MiB";
_ = InvokeAsync(StateHasChanged);
}
private static async Task<(long Length, uint Crc)> ComputeStreamDigestAsync(Stream stream, Action<long>? progressCallback = null)
{
var buffer = new byte[64 * 1024];
long totalLength = 0;
var crc = Crc32Helper.Begin();
var nextProgressReportBytes = ProgressLogIntervalBytes;
while (true)
{
var bytesRead = await stream.ReadAsync(buffer);
if (bytesRead == 0)
break;
totalLength += bytesRead;
crc = Crc32Helper.Update(crc, buffer.AsSpan(0, bytesRead));
while (progressCallback is not null && totalLength >= nextProgressReportBytes)
{
progressCallback(nextProgressReportBytes);
nextProgressReportBytes += ProgressLogIntervalBytes;
}
}
return (totalLength, Crc32Helper.Complete(crc));
}
private static long ToMiB(long bytes)
{
return bytes / (1024L * 1024L);
}
}