-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Rewrite rideshare example in golang (#634)
- Loading branch information
1 parent
ee52a68
commit 09e4e51
Showing
24 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM golang:1.17 | ||
|
||
WORKDIR /go/src/app | ||
COPY . . | ||
|
||
RUN echo $(pwd) | ||
|
||
RUN go get -d -v ./... | ||
RUN go install -v ./... | ||
|
||
CMD ["go", "run", "."] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
FROM python:3.9 | ||
|
||
RUN pip3 install requests | ||
|
||
COPY load-generator.py ./load-generator.py | ||
|
||
ENV PYTHONUNBUFFERED=1 | ||
|
||
CMD [ "python", "load-generator.py" ] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
## Golang Example | ||
|
||
To run the example run the following commands: | ||
``` | ||
# Pull latest pyroscope image: | ||
docker pull pyroscope/pyroscope:latest | ||
# Run the example project: | ||
docker-compose up --build | ||
# Reset the database (if needed): | ||
# docker-compose down | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package bike | ||
|
||
import "github.com/pyroscope-io/pyroscope/tree/main/examples/golang/utility" | ||
|
||
func OrderBike(search_radius int64) { | ||
utility.FindNearestVehicle(search_radius, "bike") | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package car | ||
|
||
import "github.com/pyroscope-io/pyroscope/tree/main/examples/golang/utility" | ||
|
||
func OrderCar(search_radius int64) { | ||
utility.FindNearestVehicle(search_radius, "car") | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
version: '3.9' | ||
services: | ||
us-east-1: | ||
environment: | ||
- REGION=us-east-1 | ||
- PYROSCOPE_SERVER_ADDRESS=http://pyroscope:4040 | ||
build: | ||
context: . | ||
|
||
us-west-1: | ||
environment: | ||
- REGION=us-west-1 | ||
- PYROSCOPE_SERVER_ADDRESS=http://pyroscope:4040 | ||
|
||
build: | ||
context: . | ||
|
||
eu-west-1: | ||
environment: | ||
- REGION=eu-west-1 | ||
- PYROSCOPE_SERVER_ADDRESS=http://pyroscope:4040 | ||
build: | ||
context: . | ||
|
||
pyroscope: | ||
image: pyroscope/pyroscope:latest | ||
environment: | ||
- PYROSCOPE_LOG_LEVEL=debug | ||
ports: | ||
- '4040:4040' | ||
command: | ||
- 'server' | ||
|
||
load-generator: | ||
build: | ||
context: . | ||
dockerfile: Dockerfile.load-generator |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/pyroscope-io/pyroscope/tree/main/examples/golang | ||
|
||
go 1.17 | ||
|
||
require github.com/pyroscope-io/client v0.0.0-20211206204731-3fd0a4b8239c |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/pyroscope-io/client v0.0.0-20211206204731-3fd0a4b8239c h1:QVplAdQO7l4v4b/C2ANubLOMHTSr+5VD5cFpjPgRCJ8= | ||
github.com/pyroscope-io/client v0.0.0-20211206204731-3fd0a4b8239c/go.mod h1:zRdQXIGxy0H2QbKEkCmZBR6KOLLIFYLWsdzVI0MRm2E= |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import random | ||
import requests | ||
import time | ||
|
||
HOSTS = [ | ||
'us-east-1', | ||
'us-west-1', | ||
'eu-west-1', | ||
] | ||
|
||
VEHICLES = [ | ||
'bike', | ||
'scooter', | ||
'car', | ||
] | ||
|
||
if __name__ == "__main__": | ||
print(f"starting load generator") | ||
time.sleep(3) | ||
while True: | ||
host = HOSTS[random.randint(0, len(HOSTS) - 1)] | ||
vehicle = VEHICLES[random.randint(0, len(VEHICLES) - 1)] | ||
print(f"requesting {vehicle} from {host}") | ||
resp = requests.get(f'http://{host}:5000/{vehicle}') | ||
print(f"received {resp}") | ||
time.sleep(random.uniform(0.2, 0.4)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
|
||
"github.com/pyroscope-io/client/pyroscope" | ||
"github.com/pyroscope-io/pyroscope/tree/main/examples/golang/bike" | ||
"github.com/pyroscope-io/pyroscope/tree/main/examples/golang/car" | ||
"github.com/pyroscope-io/pyroscope/tree/main/examples/golang/scooter" | ||
) | ||
|
||
func bikeRoute(w http.ResponseWriter, r *http.Request) { | ||
bike.OrderBike(1) | ||
w.Write([]byte("<h1>Bike ordered</h1>")) | ||
} | ||
|
||
func scooterRoute(w http.ResponseWriter, r *http.Request) { | ||
scooter.OrderScooter(2) | ||
w.Write([]byte("<h1>Scooter ordered</h1>")) | ||
} | ||
|
||
func carRoute(w http.ResponseWriter, r *http.Request) { | ||
car.OrderCar(3) | ||
w.Write([]byte("<h1>Car ordered</h1>")) | ||
} | ||
|
||
func index(w http.ResponseWriter, r *http.Request) { | ||
result := "<h1>environment vars:</h1>" | ||
for _, env := range os.Environ() { | ||
result += env + "<br>" | ||
} | ||
w.Write([]byte(result)) | ||
} | ||
|
||
func main() { | ||
serverAddress := os.Getenv("PYROSCOPE_SERVER_ADDRESS") | ||
if serverAddress == "" { | ||
serverAddress = "http://localhost:4040" | ||
} | ||
pyroscope.Start(pyroscope.Config{ | ||
ApplicationName: "ride-sharing-app", | ||
ServerAddress: serverAddress, | ||
Logger: pyroscope.StandardLogger, | ||
Tags: map[string]string{"region": os.Getenv("REGION")}, | ||
}) | ||
|
||
http.HandleFunc("/", index) | ||
http.HandleFunc("/bike", bikeRoute) | ||
http.HandleFunc("/scooter", scooterRoute) | ||
http.HandleFunc("/car", carRoute) | ||
http.ListenAndServe(":5000", 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package scooter | ||
|
||
import "github.com/pyroscope-io/pyroscope/tree/main/examples/golang/utility" | ||
|
||
func OrderScooter(search_radius int64) { | ||
utility.FindNearestVehicle(search_radius, "scooter") | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package utility | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"time" | ||
|
||
"github.com/pyroscope-io/client/pyroscope" | ||
) | ||
|
||
func mutexLock(n int64) { | ||
var i int64 = 0 | ||
|
||
// start time is number of seconds since epoch | ||
start_time := time.Now().Unix() | ||
|
||
for (time.Now().Unix() - start_time) < n*10 { | ||
i++ | ||
} | ||
} | ||
|
||
func checkDriverAvailability(n int64) { | ||
var i int64 = 0 | ||
|
||
// start time is number of seconds since epoch | ||
start_time := time.Now().Unix() | ||
|
||
for (time.Now().Unix() - start_time) < n/2 { | ||
i++ | ||
} | ||
|
||
// Every 4 minutes this will artificially create make requests in us-west-1 region slow | ||
// this is just for demonstration purposes to show how performance impacts show up in the | ||
// flamegraph | ||
force_mutex_lock := time.Now().Minute()*4%8 == 0 | ||
if os.Getenv("REGION") == "us-west-1" && force_mutex_lock { | ||
mutexLock(n) | ||
} | ||
|
||
} | ||
|
||
func FindNearestVehicle(n int64, vehicle string) { | ||
pyroscope.TagWrapper(context.Background(), pyroscope.Labels("vehicle", vehicle), func(ctx context.Context) { | ||
var i int64 = 0 | ||
|
||
start_time := time.Now().Unix() | ||
for (time.Now().Unix() - start_time) < n { | ||
i++ | ||
} | ||
|
||
if vehicle == "car" { | ||
checkDriverAvailability(n) | ||
} | ||
}) | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.