From 35a85de7b7495878ae4dcb73449f18c28131496c Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期五, 03 二月 2023 17:17:16 +0800
Subject: [PATCH] add go test,complete

---
 main.go |  181 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 178 insertions(+), 3 deletions(-)

diff --git a/main.go b/main.go
index b7323ea..c3572b3 100644
--- a/main.go
+++ b/main.go
@@ -1,9 +1,184 @@
 package main
 
-import "fmt"
+import (
+	"fmt"
+	"goshm/shmparser"
+	"io/ioutil"
+	"os"
+	"time"
+)
 
-import "goshm/shmparser"
+const (
+	printlog = true
+	savepic  = false
+)
+
+func readFile(file string) []byte {
+	if f, e := os.Open(file); e == nil {
+		if d, ee := ioutil.ReadAll(f); ee == nil {
+			f.Close()
+			return d
+		}
+		f.Close()
+	}
+	return nil
+}
+
+func printImage(shm []byte, saveFile string) {
+	img := shmparser.Shm2Image(shm)
+
+	if printlog {
+		fmt.Printf("image id [%d] data size [%d] rsln [%dx%d] timestamp [%s] cid [%s] cname [%s]\n",
+			img.Id, len(img.Data), img.Width, img.Height, img.Timestamp, img.Cid, img.Cname)
+	}
+
+	if savepic {
+		ioutil.WriteFile(saveFile, img.Data, 0666)
+	}
+}
+
+func testImage(shm []byte, order bool) {
+	files := []string{
+		"./opic/720x576.jpg",
+		"./opic/720x576.bgr",
+		"./opic/1920x1080.jpg",
+		"./opic/1920x1080.bgr",
+	}
+
+	if order {
+		for i, f := range files {
+			w, h := 1920, 1080
+			if i < 2 {
+				w, h = 720, 576
+			}
+
+			d := readFile(f)
+			shmparser.Image2Shm(shm, "cameraid", "cameraname", d, w, h, 1122331122)
+			printImage(shm, fmt.Sprintf("./npic/%dx%d.%d", w, h, i))
+		}
+	} else {
+		for i := len(files) - 1; i >= 0; i-- {
+			w, h := 1920, 1080
+			if i < 2 {
+				w, h = 720, 576
+			}
+			d := readFile(files[i])
+			shmparser.Image2Shm(shm, "cameraid", "cameraname", d, w, h, 1122331122)
+			printImage(shm, fmt.Sprintf("./npic/%dx%d.%d", w, h, i))
+		}
+	}
+}
+
+func testOnce(shm []byte, count int) int64 {
+	start := time.Now()
+	for i := 0; i < count; i++ {
+		testRule(shm)
+	}
+
+	for i := 0; i < count; i++ {
+		testImage(shm, true)
+		testImage(shm, false)
+	}
+	for i := 0; i < count; i++ {
+		testImage(shm, false)
+	}
+	for i := 0; i < count; i++ {
+		testImage(shm, true)
+	}
+	for i := 0; i < count; i++ {
+		testImage(shm, true)
+		testImage(shm, false)
+	}
+	for i := 0; i < count; i++ {
+		testImage(shm, false)
+		testImage(shm, true)
+	}
+
+	for i := 0; i < count; i++ {
+		testRule(shm)
+	}
+	return int64(time.Now().Sub(start))
+}
 
 func main() {
-	fmt.Println("hello")
+	shm := make([]byte, 10*1024*1024)
+	count := 1000
+	countTest := 1
+
+	var avg int64 = 0
+	for i := 1; i <= countTest; i++ {
+		tm := testOnce(shm, count)
+		if i == 1 {
+			avg = tm
+		} else {
+			avg = avg/int64(i)*int64(i-1) + tm/int64(i)
+		}
+	}
+
+	fmt.Printf("run %d time test, each %d use %d ms\n", count*countTest, count, avg/1e6)
+}
+
+func testRule(shm []byte) {
+	makeRule(shm)
+	packSdk(shm, 2, 5)
+	packSdk(shm, 0, 5)
+	for i := 0; i < 5; i++ {
+		packSdk(shm, 0, 5)
+	}
+	printRule(shm)
+}
+
+func makeRule(shm []byte) {
+	shmparser.Rule2Shm(shm, "camera_start")
+}
+
+func packTargets(count int, typ string) []shmparser.Target {
+	var t []shmparser.Target
+	for i := 0; i < count; i++ {
+		t = append(t, shmparser.Target{
+			Id:         uint64(i) + 123123123678,
+			Confidence: 99 - i,
+			Rc: shmparser.Rect{
+				Left:   0,
+				Top:    0,
+				Right:  1920,
+				Bottom: 1080,
+			},
+			Type: typ,
+			Feat: []byte("packTargets-feat"),
+			Attr: []byte("packTargets-attr"),
+		})
+	}
+	return t
+}
+
+func packSdk(shm []byte, s, e int) {
+	types := []string{"face", "yolo", "bike", "fire", "car"}
+
+	for i := s; i < e; i++ {
+		t := packTargets(2+i, types[i])
+		sdk := shmparser.Resdk{
+			Type:      types[i],
+			Timestamp: "2023-02-03 11:56:38.987",
+			Targets:   t,
+		}
+		shmparser.AddResSdk2RuleInShm(shm, sdk)
+	}
+}
+
+func printRule(shm []byte) {
+	rule := shmparser.Shm2Rule(shm)
+	if !printlog {
+		return
+	}
+
+	fmt.Printf("<rule> handletrack [%#v] datatype [%s]\n", rule.Handletrack, rule.Datatype)
+	for k, v := range rule.Sdks {
+		fmt.Printf("\t<sdk> [%d] Type [%s] Id [%s] Name [%s] Timestamp [%s]\n",
+			k, v.Type, v.Id, v.Name, v.Timestamp)
+		for i, t := range v.Targets {
+			fmt.Printf("\t\t<target> [%d] Id [%d] Type [%s] Confidence [%d] Rect [%#v] Feat [%s] Attr [%s]\n",
+				i, t.Id, t.Type, t.Confidence, t.Rc, string(t.Feat), string(t.Attr))
+		}
+	}
 }

--
Gitblit v1.8.0