forked from danielBCN/faas-parallelism-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenchFunc.cs
More file actions
72 lines (64 loc) · 2.12 KB
/
BenchFunc.cs
File metadata and controls
72 lines (64 loc) · 2.12 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
using System;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace Daniel.Bench
{
public static class BenchFunc
{
[FunctionName("BenchFunc")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
Microsoft.Azure.WebJobs.ExecutionContext functionContext,
ILogger log)
{
var init = DateTimeOffset.Now.ToUnixTimeMilliseconds();
log.LogInformation("C# HTTP trigger function START.");
var id = functionContext.InvocationId;
string instanceId = Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID");
DoWork();
// DoSleep();
log.LogInformation("C# HTTP trigger function END.");
var end = DateTimeOffset.Now.ToUnixTimeMilliseconds();
var res = new
{
Init = init,
End = end,
InvocationID = id,
InstanceID = instanceId
};
// string responseMessage = $"{init}, {end}, {id}";
string responseMessage = JsonConvert.SerializeObject(res);
return new OkObjectResult(responseMessage);
}
private static void DoSleep()
{
Thread.Sleep(1000);
}
private static void DoWork()
{
// MonteCarlo sim PI
int iterations = 20_000_000;
var rand = new System.Random();
int count = 0;
double x, y;
for (int i = 0; i < iterations; i++)
{
x = rand.NextDouble();
y = rand.NextDouble();
if (x * x + y * y <= 1.0)
{
count++;
}
}
double pi = 4.0 * count / iterations;
// Console.WriteLine(pi);
}
}
}