D
D
Delakey Blackhole2016-11-01 13:15:57
API
Delakey Blackhole, 2016-11-01 13:15:57

ASP.NET Core must be slow?

I created an ASP.NET Core project in the studio and
752486d1477868924
got such a class there

[Route("api/[controller]")]
    public class ValuesController : Controller
    {
        // GET api/values
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }
 
        // GET api/values/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }
 
        // POST api/values
        [HttpPost]
        public void Post([FromBody]string value)
        {
        }
 
        // PUT api/values/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody]string value)
        {
        }
 
        // DELETE api/values/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }

then created another project for testing
class Program
    {
        static void Main(string[] args)
        {
            MyWeb web = new MyWeb();
            Thread.Sleep(100);
            for (int i = 0; i < 10; i++)
            {
                Stopwatch stopWatch = new Stopwatch();
                stopWatch.Start();
 
                web.Send("http://localhost:5000/api/values/");
                stopWatch.Stop();
 
                TimeSpan ts = stopWatch.Elapsed;
 
                // Format and display the TimeSpan value.
                string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}",
                    ts.Hours, ts.Minutes, ts.Seconds,
                    ts.Milliseconds);
                Console.WriteLine("RunTime " + elapsedTime);
            }
            Console.ReadLine();
        }
 
        public class MyWeb
        {
            public MyWebResponse Send(string url)
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.KeepAlive = false;
                //request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
                request.Timeout = 20000;
                //request.ContentType = "application/x-www-form-urlencoded";
                //request.UserAgent = "Opera/9.80 (Windows NT 5.1; U; ru) Presto/2.9.168 Version/11.51";
                /* 
                 request.Method = "POST";
                 request.CookieContainer = cc;
 
                 string command = @"" + data;
                byte[] bytes = Encoding.ASCII.GetBytes(command);
                request.ContentLength = bytes.Length;
 
                Stream stream = request.GetRequestStream();
                stream.Write(bytes, 0, bytes.Length);
                */
                WebResponse response = request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string ret = reader.ReadToEnd();
 
                return new MyWebResponse()
                {
                    text = ret
                };
            }
 
 
        }
        public class MyWebResponse
        {
            public string text;
        }
    }

I start the server, I start the test project, I get pretty sad results
RunTime 00:00:02.930
RunTime 00:00:00.089
RunTime 00:00:00.082
RunTime 00:00:00.066
RunTime 00:00:02.291
RunTime 00:00:00.176
RunTime 00:00:00.555
RunTime 00:00:00.243
RunTime 00:00:00.360
RunTime 00:00:00.412

Can you tell me how to reduce the request time?
tests were carried out locally,
then I simply created a minimal http server in the console application
static void Main(string[] args)
        {
            WebSocketSharp.Server.HttpServer t = new WebSocketSharp.Server.HttpServer(IPAddress.Any,5000);
            t.OnGet += T_OnGet;
            t.Start();
            Console.ReadLine();
        }

        private static void T_OnGet(object sender, WebSocketSharp.Server.HttpRequestEventArgs e)
        {
            Console.WriteLine(e.Request.Url);
            string[] data_ = new string[] { "key","val" };
            Newtonsoft.Json.JsonConvert.SerializeObject(data_);
            var data = Encoding.UTF8.GetBytes("ddd");
            e.Response.WriteContent(data);
        }

and got good results
20161101-xhw2-23kb.jpg
PS I have a task to write a small api for a mobile application, I thought to make it on asp but 50-150 milliseconds against 1 in a console application is somehow not very

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Grekhov, 2016-11-12
@Sterk

Absolutely nothing is said about setting up asp.net core. I can advise you to turn off logging to the console. Make a release version and run separately from the studio.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question