-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
109 lines (89 loc) · 3.77 KB
/
Program.cs
File metadata and controls
109 lines (89 loc) · 3.77 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
namespace AzPerf
{
using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Diagnostics;
using System.IO.Compression;
using System.Security.Cryptography;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.RetryPolicies;
using System.Threading.Tasks;
using System.Text;
using System.Collections.Generic;
using System.Linq;
class Program
{
public static CloudBlobContainer[] GetRandomContainers()
{
string connectionString = "";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer[] blobContainers = new CloudBlobContainer[8];
for (int i = 0; i < blobContainers.Length; i++)
{
blobContainers[i] = blobClient.GetContainerReference(GenerateString(5, new Random((int)DateTime.Now.Ticks), LowerCaseAlphabet));
Console.WriteLine(blobContainers[i].Uri);
blobContainers[i].CreateIfNotExistsAsync().Wait();
}
return blobContainers;
}
public const string LowerCaseAlphabet = "abcdefghijklmnopqrstuvwyxz";
public static string GenerateString(int size, Random rng, string alphabet)
{
char[] chars = new char[size];
for (int i = 0; i < size; i++)
{
chars[i] = alphabet[rng.Next(alphabet.Length)];
}
return new string(chars);
}
static void Main(string[] args)
{
CloudBlobContainer[] containers = GetRandomContainers();
// path to the directory to upload
string path = "test";
if (args.Length > 0)
{
path = System.Convert.ToString(args[0]);
}
Stopwatch time = Stopwatch.StartNew();
try
{
Console.WriteLine("iterating in directiory:", path);
// Seed the Random value using the Ticks representing current time and date
// Since int is used as seen we cast (loss of long data)
int count = 0;
int max_outstanding = 100;
int completed_count = 0;
Semaphore sem = new Semaphore(max_outstanding, max_outstanding);
List<Task> Tasks = new List<Task>();
foreach (string fileName in Directory.GetFiles(path))
{
Console.WriteLine("Starting {0}", fileName);
var container = containers[count % 8];
Random r = new Random((int)DateTime.Now.Ticks);
String s = (r.Next() % 10000).ToString("X5");
CloudBlockBlob blockBlob = container.GetBlockBlobReference(s);
blockBlob.StreamWriteSizeInBytes = 100 * 1024 * 1024;
sem.WaitOne();
Tasks.Add(blockBlob.UploadFromFileAsync(fileName, null, new BlobRequestOptions() { ParallelOperationThreadCount = 8, DisableContentMD5Validation = true, StoreBlobContentMD5 = false }, null).ContinueWith((t) => {
sem.Release();
Interlocked.Increment(ref completed_count);
}));
count++;
}
Task.WhenAll(Tasks).Wait();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
time.Stop();
Console.WriteLine("Upload has been completed in {0} seconds.", time.Elapsed.TotalSeconds.ToString());
Console.ReadLine();
}
}
}