-
-
Save janisz/9db4d50fc09f2eba81781af8dbc26a03 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"testing" | |
"os" | |
"bufio" | |
"io" | |
"encoding/json" | |
"errors" | |
"strings" | |
"github.com/buger/jsonparser" | |
) | |
const ( | |
statusUpdateEventType = "status_update_event" | |
healthStatusChangedEventType = "health_status_changed_event" | |
unsuportedEventType = "Unsuported" | |
) | |
type baseEvent struct { | |
Type string `json:"eventType"` | |
} | |
func eventType_JSON(jsonBlob []byte) (string, error) { | |
event := baseEvent{} | |
err := json.Unmarshal(jsonBlob, &event) | |
if err != nil { | |
return "", err | |
} else if event.Type == "" { | |
return "", errors.New("no event") | |
} else { | |
return event.Type, nil | |
} | |
} | |
func eventType_Strings(body string) string { | |
if strings.Contains(body, "\"" + statusUpdateEventType + "\"") { | |
return statusUpdateEventType | |
} else if strings.Contains(body, "\"" + healthStatusChangedEventType + "\"") { | |
return healthStatusChangedEventType | |
} | |
return unsuportedEventType | |
} | |
func eventType_JSONparser(jsonBlob []byte) (string, error) { | |
return jsonparser.GetUnsafeString(jsonBlob, "eventType") | |
} | |
func BenchmarkEventType_JSON(b *testing.B) { | |
events, err := events() | |
if err != nil { | |
b.Fatal(err) | |
} | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
for _, event := range events { | |
eventType_JSON(event) | |
} | |
} | |
} | |
func BenchmarkEventType_Contains(b *testing.B) { | |
events, err := events() | |
if err != nil { | |
b.Fatal(err) | |
} | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
for _, event := range events { | |
eventType_Strings(string(event)) | |
} | |
} | |
} | |
func BenchmarkEventType_JSONparser(b *testing.B) { | |
events, err := events() | |
if err != nil { | |
b.Fatal(err) | |
} | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
for _, event := range events { | |
eventType_JSONparser(event) | |
} | |
} | |
} | |
func events() (events [][]byte, err error) { | |
f, err := os.Open("events.jsons") | |
if err != nil { | |
return | |
} | |
eventsReader := bufio.NewReader(f) | |
events = make([][]byte, 0, 3634) | |
for { | |
event, _, err := eventsReader.ReadLine() | |
if err == io.EOF { | |
break | |
} | |
if err != nil { | |
return nil, err | |
} | |
events = append(events, event) | |
} | |
return events, nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file should be grabbed from marathon events. This could be done by registering server.go as a marathon subscriber and redirect STDOUT to file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
go test -cpu 1 -bench=. -benchtime=10s ./ -test.benchmem | |
testing: warning: no tests to run | |
BenchmarkEventType_JSON 200 89816025 ns/op 37996780 B/op 826231 allocs/op | |
BenchmarkEventType_Contains 10 1324784240 ns/op 410147632 B/op 103282 allocs/op | |
BenchmarkEventType_JSONparser 10 1444820113 ns/op 0 B/op 0 allocs/op | |
PASS | |
ok github.com/allegro/marathon-consul 58.452s | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
) | |
func rootHandler(w http.ResponseWriter, r *http.Request) { | |
body, _ := ioutil.ReadAll(r.Body) | |
fmt.Println(string(body)) | |
w.WriteHeader(http.StatusOK) | |
} | |
func main() { | |
http.HandleFunc("/", rootHandler) | |
log.Fatal(http.ListenAndServe(":8080", nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment