github.com/TeaOSLab/EdgeNode@v1.3.8/internal/waf/captcha_test.go (about)

     1  // Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package waf_test
     4  
     5  import (
     6  	"bytes"
     7  	"fmt"
     8  	"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
     9  	"github.com/dchest/captcha"
    10  	"runtime"
    11  	"testing"
    12  	"time"
    13  )
    14  
    15  func TestCaptchaMemory(t *testing.T) {
    16  	if !testutils.IsSingleTesting() {
    17  		return
    18  	}
    19  
    20  	var stat1 = &runtime.MemStats{}
    21  	runtime.ReadMemStats(stat1)
    22  
    23  	var count = 5_000
    24  	var before = time.Now()
    25  
    26  	for i := 0; i < count; i++ {
    27  		var id = captcha.NewLen(6)
    28  		var writer = &bytes.Buffer{}
    29  		err := captcha.WriteImage(writer, id, 200, 100)
    30  		if err != nil {
    31  			t.Fatal(err)
    32  		}
    33  		captcha.VerifyString(id, "abc")
    34  	}
    35  
    36  	var stat2 = &runtime.MemStats{}
    37  	runtime.ReadMemStats(stat2)
    38  	t.Log((stat2.HeapInuse-stat1.HeapInuse)>>20, "MB", fmt.Sprintf("%.0f QPS", float64(count)/time.Since(before).Seconds()))
    39  }
    40  
    41  func BenchmarkCaptcha_VerifyCode_100_50(b *testing.B) {
    42  	runtime.GOMAXPROCS(4)
    43  
    44  	b.RunParallel(func(pb *testing.PB) {
    45  		for pb.Next() {
    46  			var id = captcha.NewLen(6)
    47  			var writer = &bytes.Buffer{}
    48  			err := captcha.WriteImage(writer, id, 100, 50)
    49  			if err != nil {
    50  				b.Fatal(err)
    51  			}
    52  		}
    53  	})
    54  }
    55  
    56  func BenchmarkCaptcha_VerifyCode_200_100(b *testing.B) {
    57  	runtime.GOMAXPROCS(4)
    58  
    59  	b.RunParallel(func(pb *testing.PB) {
    60  		for pb.Next() {
    61  			var id = captcha.NewLen(6)
    62  			var writer = &bytes.Buffer{}
    63  			err := captcha.WriteImage(writer, id, 200, 100)
    64  			if err != nil {
    65  				b.Fatal(err)
    66  			}
    67  			_ = id
    68  		}
    69  	})
    70  }