namespace Benchmark { using System ; using System.Web ; using System.Web.UI ; using System.Threading.Tasks ; public partial class Default : System. Web . UI . Page { Randomrnd = null ; public void Page_Load ( object sender,EventArgse ) { /
namespace Benchmark
{
using System;
using System.Web;
using System.Web.UI;
using System.Threading.Tasks;
public partial class Default : System.Web.UI.Page
{
Random rnd=null;
public void Page_Load(object sender, EventArgs e)
{
//RegisterAsyncTask(new PageAsyncTask(PerformIOasync,PerformIOasync,null,null));
rnd = new Random ();
//Task.Run (new Action (PerformIOasync));
PerformIOasync ();
}
public async void PerformIOasync()
{
string s=""; //generate a random string of 108KB and a random filename
string fname = rndchar() + rndchar() + rndchar() + rndchar() +".txt";
for(int i=0;i108000;i++)
{
char ch=rndchar();
s += ch;
}
//write s to a file
//file_put_contents($fname,$s);
var slowTask1 = Taskstring>.Factory.StartNew(()=> WriteToDisk(fname,s));
await slowTask1;
var SlowTask2 = Taskstring>.Factory.StartNew(()=> ReadFromDisk(fname,s));
await SlowTask2;
Response.Write(SlowTask2.Result.ToString());
}
private string WriteToDisk(string fname, string s){
System.IO.File.WriteAllText (AppDomain.CurrentDomain.BaseDirectory + fname, s);
return "";
}
private string ReadFromDisk(string fname, string s){
return System.IO.File.ReadAllText (AppDomain.CurrentDomain.BaseDirectory + fname);
}
private char rndchar()
{
return (char)rnd.Next (65, 112);
}
}
}