Przeglądaj źródła

Using []byte switch better then map/string

Steve Thielemann 1 rok temu
rodzic
commit
d5f546edbc
1 zmienionych plików z 74 dodań i 17 usunięć
  1. 74 17
      testdoor/runtime-info.go

+ 74 - 17
testdoor/runtime-info.go

@@ -11,6 +11,12 @@ import (
 
 // reused by GoRuntinesStatus()
 var buffer []byte
+var output *bytes.Buffer
+
+// This uses the most memory.  There has to be a better way.
+const DEBUG_THIS bool = false
+
+/*
 var GStatus map[string]string = map[string]string{
 	"idle":         "I",
 	"runnable":     "r",
@@ -25,10 +31,52 @@ var GStatus map[string]string = map[string]string{
 	"chan receive": "<", // missing
 	"chan send":    ">", // missing
 }
-var output *bytes.Buffer
-
-// This uses the most memory.  There has to be a better way.
-const DEBUG_THIS bool = false
+*/
+
+// Map Status to Symbol.
+func GStatusMapper(status []byte) byte {
+	switch status[0] {
+	case 'c':
+		if status[1] == 'o' {
+			return 'C'
+		}
+		switch status[5] {
+		case 'r':
+			return '<'
+		case 's':
+			return '>'
+		}
+		return '?'
+	case 'd':
+		return 'D'
+	case 'i':
+		return 'I'
+	case 'p':
+		return 'P'
+	case 'r':
+		switch status[4] {
+		case 'a':
+			return 'r'
+		case 'i':
+			return 'R'
+		}
+		return '?'
+	case 's':
+		switch status[1] {
+		case 'y':
+			return 's'
+		case 'l':
+			return 'S'
+		case 'e':
+			return 's'
+		}
+		return '?'
+	case 'w':
+		return 'W'
+	default:
+		return '?'
+	}
+}
 
 // Get Go Routines and map the Status.  See GStatus.
 func GoRoutinesStatus() []byte {
@@ -127,13 +175,16 @@ func GoRoutinesStatus() []byte {
 				status = status[:space]
 				// log.Printf("Status now: [%s]\n", status)
 			}
-
-			rstatus, ok := GStatus[string(status)]
-			if ok {
-				output.WriteString(rstatus)
-			} else {
-				log.Printf("** Status [%s] not found.\n", status)
-			}
+			rstatus := GStatusMapper(status)
+			//rstatus, ok := GStatus[string(status)]
+			output.WriteByte(rstatus)
+			/*
+				if ok {
+					output.WriteString(rstatus)
+				} else {
+					log.Printf("** Status [%s] not found.\n", status)
+				}
+			*/
 		}
 
 	}
@@ -161,12 +212,18 @@ func GoRoutinesStatus() []byte {
 				if strings.Contains(status, ",") {
 					status = strings.Split(status, ",")[0]
 				}
-				rstatus, ok := GStatus[status]
-				if ok {
-					output.WriteString(rstatus)
-				} else {
-					log.Printf("Status %s not found.\n[%s]\n", rstatus, text)
-				}
+				/*
+					rstatus := GStatusMapper(status)
+					output.WriteByte(rstatus)
+
+
+					rstatus, ok := GStatus[status]
+					if ok {
+						output.WriteString(rstatus)
+					} else {
+						log.Printf("Status %s not found.\n[%s]\n", rstatus, text)
+					}
+				*/
 			}
 		}
 	}