Beispiel #1
0
 public void GetOnce(HttpCtx ctx, int query_id)
 {
     if (storage.Cities.Count > 1)
     {
         ctx.Reset();
         var r = rnd.NextDouble();
         if (r < PROB_FILTER)
         {
             getFilter(query_id, ctx);
         }
         else
         if (r < PROB_GROUP)
         {
             getGroup(query_id, ctx);
         }
         else
         if (r < PROB_RECOMMEND)
         {
             getRecommend(query_id, ctx);
         }
         else
         {
             getSuggest(query_id, ctx);
         }
     }
 }
Beispiel #2
0
        private void prepareHttpContext(StreamReader reader, HttpCtx ctx)
        {
            //var size = int.Parse(reader.ReadLine().Split(' ')[0]);
            ctx.Reset();
            var line = reader.ReadLine(); // skip the 1st line

            do
            {
                line = reader.ReadLine();
                if (ctx.Method == null)
                {
                    ctx.ParseFirstLine(line);
                }
                else
                if (ctx.RequestBodyLength == 0)
                {
                    line.FindIntegerAfter("content-length", out ctx.RequestBodyLength);
                }
            } while (!string.IsNullOrEmpty(line));

            if (ctx.RequestBodyLength > 0)
            {
                Span <char> spanChar = stackalloc char[ctx.RequestBodyLength];
                Span <byte> spanByte = ctx.Buffer.AsSpan()
                                       .Slice(ctx.RequestBodyStart, ctx.RequestBodyLength);
                reader.Read(spanChar);
                for (int i = 0; i < ctx.RequestBodyLength; i++)
                {
                    spanByte[i] = (byte)spanChar[i];
                }
                reader.ReadLine(); // empty line
            }
        }
Beispiel #3
0
 public static void Release(HttpCtx bb)
 {
     bb.Reset();
     bag.Add(bb);
 }
Beispiel #4
0
        public TimeSpan TestPhase(int phase, string path, bool verify)
        {
            Log.Info("Testing phase " + phase);
            TimeSpan totalPhaseTime  = new TimeSpan();
            var      getPost         = phase == 2 ? "post" : "get";
            var      ammoFileName    = path + "/ammo/phase_" + phase + "_" + getPost + ".ammo";
            var      answersFileName = path + "/answers/phase_" + phase + "_" + getPost + ".answ";
            var      postActions     = new List <Action>();
            var      ctx             = new HttpCtx();

            using (var reqFs = new FileStream(ammoFileName, FileMode.Open, FileAccess.Read))
                using (var reqStream = new StreamReader(reqFs, Encoding.UTF8))
                    using (var ansFs = new FileStream(answersFileName, FileMode.Open, FileAccess.Read))
                        using (var ansStream = new StreamReader(ansFs, Encoding.ASCII, false))
                        {
                            while (!reqStream.EndOfStream)
                            {
                                prepareHttpContext(reqStream, ctx);
                                var startTime = Stats.Watch.Elapsed;
                                router.ProcessRequest(ctx); // actual request processing
                                if (ctx.PostAction != null)
                                {
                                    postActions.Add(ctx.PostAction);
                                    ctx.PostAction = null;
                                }
                                var elapsed = Stats.Watch.Elapsed - startTime;
                                Stats.ReportContextTime(ctx.ContextType, elapsed);
                                totalPhaseTime += elapsed;

                                if (verify)
                                {
                                    var ans       = ansStream.ReadLine().Split('\t');
                                    var expStatus = int.Parse(ans[2]);
                                    var strAnsw   = ""; // Encoding.ASCII.GetString(ctx.Buffer, ctx.ResponseStart, ctx.ResponseBodyStart- ctx.ResponseStart);
                                    if (ans.Length > 3)
                                    {
                                        strAnsw = ans[3];
                                    }

                                    //var qPath = ctx.Request.Path.ToString();
                                    if (ctx.StatusCode != expStatus)
                                    {
                                        Console.WriteLine("\n{0} {1}", ans[0], Uri.UnescapeDataString(ans[1]));
                                        Console.WriteLine("Received statusCode {0} instead of {1}",
                                                          ctx.StatusCode, expStatus);
                                        router.ProcessRequest(ctx);
                                    }
                                    else
                                    if (strAnsw.Length == 0 && ctx.ResponseBodyLength == 0)
                                    {
                                        // both null, everything is OK
                                    }
                                    else
                                    {
                                        // compare jsons
                                        object jsResp = null;
                                        object jsAnsw = null;
                                        try
                                        {
                                            jsResp = JsonSerializer.Deserialize <dynamic>(ctx.Buffer, ctx.ResponseBodyStart);
                                            jsAnsw = JsonSerializer.Deserialize <dynamic>(strAnsw);
                                        }
                                        catch (Exception e)
                                        {
                                            Console.WriteLine(e.Message);
                                            break;
                                        }
                                        if (!compareDynamic(jsAnsw, jsResp))
                                        {
                                            //compareDynamic(jsAnsw, jsResp);
                                            Console.WriteLine("\n{0} {1}", ans[0], Uri.UnescapeDataString(ans[1]));
                                            Console.WriteLine("\tReceived\n{0}\n\tInstead of\n{1}",
                                                              Encoding.UTF8.GetString(ctx.Buffer, ctx.ResponseBodyStart, ctx.ResponseBodyLength),
                                                              Encoding.UTF8.GetString(JsonSerializer.Serialize <dynamic>(jsAnsw))
                                                              );
                                            //Console.ReadLine();
                                            router.ProcessRequest(ctx);
                                        }
                                    }
                                }
                                ctx.Reset();
                            }
                        }

            // process post-actions
            if (verify)  // if not verifying, likely testing for performance
            {
                foreach (var a in postActions)
                {
                    a.Invoke();
                }
            }
            postActions.Clear();

            return(totalPhaseTime);
        }