Skip to content

Instantly share code, notes, and snippets.

View percybolmer's full-sized avatar

ProgrammingPercy percybolmer

View GitHub Profile
@percybolmer
percybolmer / benchmarking-generics-Regularmove.go
Last active February 20, 2022 07:47
Benchmarking the type switched solution
package benchmarking
// Below is the Type casting based Solution
//
type CarRegular struct {
Name string
DistanceMoved int
}
type PersonRegular struct {
@percybolmer
percybolmer / benchmarking-generics-movable.go
Last active February 20, 2022 07:31
The code to benchmark for a generic moveable
package benchmarking
// Subtractable is a type constraint that defines subtractable datatypes to be used in generic functions
type Subtractable interface {
int | int64 | float32
}
// Moveable is the interace for moving a Entity
type Moveable[S Subtractable] interface {
@percybolmer
percybolmer / benchmarking-output.go
Last active February 19, 2022 14:16
Benchmarking output for the generic benchmark
goos: windows
goarch: amd64
pkg: programmingpercy/benchgeneric
cpu: Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
Benchmark_Subtract
Benchmark_Subtract/SubtractInt
Benchmark_Subtract/SubtractInt-4 1000000000 0.9002 ns/op
Benchmark_Subtract/SubtractInt-4 1000000000 0.8904 ns/op
Benchmark_Subtract/SubtractInt-4 1000000000 0.8277 ns/op
Benchmark_Subtract/SubtractInt-4 1000000000 0.8290 ns/op
@percybolmer
percybolmer / benchmarking-generics_test.go
Last active February 19, 2022 13:58
A benchmarking file for generics
package functions
import (
"math/rand"
"testing"
"time"
)
// Benchmark_Subtract is used to determine the most performant solution to subtraction
func Benchmark_Subtract(b *testing.B) {
@percybolmer
percybolmer / benchmarking-generics.go
Created February 19, 2022 13:23
A set of functions to Benchmark
package functions
// Subtract will subtract the second value from the first
func SubtractInt(a, b int) int {
return a - b
}
// Subtract64 will subtract the second value from the first
func SubtractInt64(a, b int) int {
return a - b
func FuzzTestHTTPHandler(f *testing.F) {
// Create a new server hosting our calculate func
srv := httptest.NewServer(http.HandlerFunc(CalculateHighest))
defer srv.Close()
// Create example values for the fuzzer
testCases := []ValuesRequest{
ValuesRequest{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}},
ValuesRequest{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}},
ValuesRequest{[]int{-50, -9, -8, -7, -6, -5, -4, -3, -2, -1}},
func FuzzTestHTTPHandler(f *testing.F) {
// Create a new server hosting our calculate func
srv := httptest.NewServer(http.HandlerFunc(CalculateHighest))
defer srv.Close()
// Add Corpus Seeds
testCases := []ValuesRequest{
ValuesRequest{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}},
ValuesRequest{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}},
ValuesRequest{[]int{-50, -9, -8, -7, -6, -5, -4, -3, -2, -1}},
func FuzzTestHTTPHandler(f *testing.F) {
// Create a new server hosting our calculate func
srv := httptest.NewServer(http.HandlerFunc(CalculateHighest))
defer srv.Close()
// Create example values for the fuzzer
testCases := []ValuesRequest{
ValuesRequest{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}},
ValuesRequest{[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}},
ValuesRequest{[]int{-50, -9, -8, -7, -6, -5, -4, -3, -2, -1}},
func MultipleInputs(a, b int, name string) {
// ... fancy code goes here
}
func FuzzMultipleInputs(f *testing.F) {
// We can add Multiple Seeds, but it has to be the same order as the input parameters for MultipleInputs
f.Add(10,20,"John the Ripper")
f.Fuzz(func(t *testing.T,a int,b int,name string){
MultipleInputs(a,b,name)
})
// How to define a Unit Test
func TestCalculateHighest(t *testing.T){
}
// To define a Fuzz, we use Fuzz and testing.F
func FuzzTestHTTPHandler(f *testing.F) {
}