libopenapi-validator icon indicating copy to clipboard operation
libopenapi-validator copied to clipboard

Is it normal to find that the time consumption is around 300-400 ms?

Open xuanskyer opened this issue 1 year ago • 2 comments

I conducted a simple time-consuming test on a local MacPro (macOS 14.4.1 (23E224), 16GB, M22022) using code similar to the following

package mw

import (
	"context"
	"errors"
	"io/ioutil"
	"net/http"
	"strings"
	"time"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/common/adaptor"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"github.com/cyberandao/api-gateway/biz/response"
	"github.com/pb33f/libopenapi"
	libopenapiValidator "github.com/pb33f/libopenapi-validator"
)

var HighLevelValidator libopenapiValidator.Validator

func InitOpenApiValidator() error {
	// ... create OpenAPI Document
	// load an OpenAPI 3 specification from bytes
	petstore, err := ioutil.ReadFile("apis-combined.json")
	if err != nil {
		hlog.Errorf("ioutil.ReadFile is bad err: %v", err)
		return err
	}
	// create a new document from specification bytes
	openApiDocument, err := libopenapi.NewDocument(petstore)
	if err != nil {
		hlog.Errorf("document is bad err: %v", err)
		return err
	}
	// Create a new Validator
	var validatorErrs []error
	HighLevelValidator, validatorErrs = libopenapiValidator.NewValidator(openApiDocument)
	if len(validatorErrs) > 0 {
		hlog.Errorf("document is bad err: %v", validatorErrs)
		return errors.New("validatorErrs error")
	}
	return nil
}

func OpenApiValidator() app.HandlerFunc {
	return func(ctx context.Context, c *app.RequestContext) {
		// parseURL

		httpReq, err := adaptor.GetCompatRequest(&c.Request)
		if err != nil {
			hlog.Errorf("adaptor.GetCompatRequest is bad err: %v", err)
			response.ResAbort(c, err)
			return
		}

		if validator(httpReq) != nil {
			response.ResAbort(c, err)
			return
		}
		c.Next(ctx)
	}
}

func validator(httpReq *http.Request) error {

	start := time.Now()

	// Validate the request
	requestValid, validationErrors := HighLevelValidator.ValidateHttpRequest(httpReq)

	
	duration := time.Since(start)

	
	hlog.Infof("Request to duration: %s ", duration)

	if !requestValid {
		hlog.Errorf("request is bad requestValid: %v, validationErrors: %+v", requestValid, validationErrors)
		return errors.New("requestValid is bad")
	}
	return nil
}

Is it normal to find that the time consumption is around 300-400 ms?

xuanskyer avatar Aug 19 '24 12:08 xuanskyer

No, that is very very slow. I do not know why you're seeing those results, https://github.com/pb33f/wiretap does this in 1ms or less.

daveshanley avatar Sep 29 '24 14:09 daveshanley

currently all 322 tests pass in 450ms, so yes I am not sure what is happening with your spec. How large is it?

daveshanley avatar Sep 29 '24 18:09 daveshanley

Closing this, no further feedback received.

daveshanley avatar May 20 '25 21:05 daveshanley