Documentation
¶
Index ¶
- Variables
- func GenerateGraph(profilePath, outputPath, format string)
- func GetBsonIdFromUUId(uuidStr string) (binData string)
- func GetStringUUidFromBsonId(binData string) (uuidStr string, err error)
- func MakeDirIfNotExists(path string)
- func RunAPITest(tests []APITestCase)
- func RunBenchmark(tc APITestCase)
- func RunProfiling(tc APITestCase)
- func WriteProfileAndExport(profileType, dir string)
- type APITestCase
- type Resp
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Add common contexts required in api testing. CommonContext = map[string]any{ "user_name": "Navjot Sharma", } // Add common headers required in api test cases. CommonHeaders = map[string]string{ "Content-Type": "application/json", } // To print api response we should enable this flag LogResponse = false // Formats in which profiling results will be saved. // Options: "png", "pdf". Add both to enable both outputs. ProfilingOutputFormats = []string{"png"} //ex: []string{"png"} or []string{"pdf"} or []string{"png", "pdf"} )
var EnabledProfilingTypes = []string{
"heap",
"goroutine",
"block",
"mutex",
"threadcreate",
}
Change `EnabledProfilingTypes` below to enable or disable specific profiling types. Available options: - "heap" - "goroutine" - "block" - "mutex" - "threadcreate" Example: To enable only heap and goroutine profiling, change it to:
var EnabledProfilingTypes = []string{"heap", "goroutine"}
CPU profiling (cpu.prof)is always enabled by default, Uncomment code .
Functions ¶
func GenerateGraph ¶
func GenerateGraph(profilePath, outputPath, format string)
GenerateGraph uses the Go pprof tool to generate a visualization of the profiling data. It takes the path to a .prof file, the desired output path, and the output format ("png" or "pdf").
func GetBsonIdFromUUId ¶
GetBsonIdFromUUId converts a UUID string into a MongoDB BinData representation. The function removes dashes from the UUID string, decodes it into bytes, encodes the bytes into a Base64 string, and formats it as a BinData string.
- uuidStr: A string representing the UUID in standard format (e.g., "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").
- binData: A string formatted as "BinData(0, '<Base64EncodedBytes>')", or an empty string if an error occurs during decoding.
func GetStringUUidFromBsonId ¶
To converts a MongoDB BinData representation back into a UUID string format. The function decodes the Base64-encoded bytes from the BinData string and formats them as a UUID string.
- binData: A string formatted as "BinData(0, '<Base64EncodedBytes>')".
- uuidStr: A string representing the UUID in standard format (e.g., "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"), or an empty string if an error occurs.
func MakeDirIfNotExists ¶
func MakeDirIfNotExists(path string)
To make a new directory, if directory not exists in the path.
func RunAPITest ¶
func RunAPITest(tests []APITestCase)
RunAPITest is a helper function to execute a series of API test cases. It iterates over a slice of APITestCase and runs each test case using the provided testing framework.
- tests: A slice of APITestCase, where each test case contains the logic and metadata for an individual API test.
Each test case is executed in its own subtest, identified by the Name field of the APITestCase. The RunSingle method of each test case is invoked to perform the actual test logic.
Example ¶
package main import ( "net/http" "testing" pariksha "github.com/DevNavix/pariksha" "github.com/gin-gonic/gin" ) func main() { var t *testing.T tests := []pariksha.APITestCase{ { Name: "Example Test Case", Method: http.MethodGet, URL: "/example", HandlerFunc: func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "success"}) }, ExpectedCode: http.StatusOK, T: t, }, } // Run the test cases using the Helper framework pariksha.RunAPITest(tests) }
func RunBenchmark ¶ added in v1.2.3
func RunBenchmark(tc APITestCase)
To executes a benchmark test for the API test case. It marks the function as a helper for better error reporting and runs the benchmark using the testing.B framework. The benchmark repeatedly calls the ExecuteHandler method for the number of iterations specified by the testing.B instance.
This method is designed to be used in conjunction with the Go testing package's benchmarking tools.
Example ¶
package main import ( "net/http" "testing" pariksha "github.com/DevNavix/pariksha" "github.com/gin-gonic/gin" ) func main() { var b *testing.B // Add a true test case which would hit api successfully... testCase := pariksha.APITestCase{ Name: "BenchmarkExample", Method: http.MethodGet, URL: "/example", HandlerFunc: func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "success"}) }, B: b, } // Use func RunBenchmark to perform benchmarking, and Profiling if want to perform profiling. pariksha.RunBenchmark(testCase) }
func RunProfiling ¶ added in v1.2.3
func RunProfiling(tc APITestCase)
RunProfiling executes the API test case with profiling enabled. This method is useful for analyzing the performance of the test case by collecting profiling data during its execution.
Example ¶
package main import ( "net/http" "testing" pariksha "github.com/DevNavix/pariksha" "github.com/gin-gonic/gin" ) func main() { var b *testing.B // Add a true test case which would hit api successfully... testCase := pariksha.APITestCase{ Name: "BenchmarkExample", Method: http.MethodGet, URL: "/example", HandlerFunc: func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "success"}) }, B: b, } // Use func RunBenchmark to perform benchmarking, and Profiling if want to perform profiling. pariksha.RunProfiling(testCase) }
func WriteProfileAndExport ¶
func WriteProfileAndExport(profileType, dir string)
WriteProfileAndExport captures a runtime profile (like "heap", "goroutine", etc.) writes it to a temporary .prof file, and generates output files (e.g., PNG or PDF)
Types ¶
type APITestCase ¶
type APITestCase struct { Name string Method string URL string HandlerFunc gin.HandlerFunc RequestBody string PathParams map[string]string Headers map[string]string ExpectedCode int ContextKeys map[string]any T *testing.T B *testing.B FunctionName string }
func (*APITestCase) ExecuteHandler ¶
func (tc *APITestCase) ExecuteHandler() *httptest.ResponseRecorder
To sets up the Gin test context, builds the HTTP request from the APITestCase fields, executes the handler function, and returns the response recorder. This is used for testing or benchmarking HTTP handler behavior.
func (APITestCase) GetProfileDir ¶
func (tc APITestCase) GetProfileDir() (profileDir string)
GetProfileDir returns the directory path where profiling files should be stored
func (APITestCase) RunSingle ¶
func (tc APITestCase) RunSingle()
RunSingle executes a single API test case by simulating an HTTP request and validating the response against expected values.
func (APITestCase) SetAPITestPathParams ¶
func (tc APITestCase) SetAPITestPathParams() (params gin.Params)
If your Endpoint includes a path parameter, e.g., "/users/123", and your handler expects it as "/users/:id", you must set it manually.