kamyker commented on issue #26: Raw requests to skip JObject conversion
URL: 
https://github.com/apache/openwhisk-runtime-dotnet/issues/26#issuecomment-559715123
 
 
   Thought that these
   ```
   string body = await new 
StreamReader(httpContext.Request.Body).ReadToEndAsync(); 
   JObject inputObject = string.IsNullOrEmpty(body) ? null : 
JObject.Parse(body); 
   ```
   should be replaced with
   ```
   JObject inputObject = await JObject.LoadAsync(new JsonTextReader(new 
StreamReader(httpContext.Request.Body)));
   ```
   but after testing it seems that JObject.LoadAsync is 2-3 times slower. 
JObject.Load is few % faster but no async at all so worse solution.
   
   
   Did few more tests and deserializing into defined object is faster than 
JObject so @shawnallen85 idea will be faster than current one. Below is 4 times 
faster:
   ```
   //Type functionInputType somewhere defined
   
   using ( StreamReader sr = new StreamReader( stream ) )
   using ( JsonReader r = new JsonTextReader( sr ) )
   {
        while ( r.Read() )
        {
                if ( r.Value != null && r.Depth == 1 )
                {
                        if ( r.TokenType == JsonToken.PropertyName )
                        {
                                currentProperty = r.Value.ToString();
                                if ( currentProperty == "value" )
                                {
                                        r.Read();
                                        body = typeof( JsonSerializer 
).GetMethods().FirstOrDefault( FindDeserializerMethod )
                                                ?.MakeGenericMethod( 
functionInputType ).Invoke( new JsonSerializer(), new object[] { r } );
   
                                        bool FindDeserializerMethod( MethodInfo 
m )
                                        {
                                                var parameters = 
m.GetParameters();
                                                return m.Name.Equals( 
"Deserialize", StringComparison.OrdinalIgnoreCase )
                                                                && 
m.IsGenericMethod
                                                                && 
parameters.Length == 1
                                                                && 
parameters[0].ParameterType == typeof( JsonReader );
                                        }
                                }
                                else
                                {
                                        r.Read();
                                        if ( r.TokenType == JsonToken.String ) 
//other types here also? numbers?
                                        {
                                                string envKey = 
$"__OW_{currentProperty.ToUpperInvariant()}";
                                                StringBuilder sb  = new 
StringBuilder();
   
                                                string envVal = r.Value != null 
? r.Value.ToString() : "";
                                                
Environment.SetEnvironmentVariable( envKey, envVal );
                                                Console.WriteLine( 
$"SetEnvironmentVariable: {envKey}, {envVal}" );
                                        }
                                        else
                                        {
                                                r.Skip();
                                        }
                                }
                        }
                }
        }
   }
   ```
   
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to