Skip to content

Commit

Permalink
docs: Rewrite rideshare example in golang (#634)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rperry2174 committed Dec 22, 2021
1 parent ee52a68 commit 09e4e51
Show file tree
Hide file tree
Showing 24 changed files with 232 additions and 0 deletions.
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.
11 changes: 11 additions & 0 deletions examples/golang-push/rideshare/Dockerfile
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", "."]
9 changes: 9 additions & 0 deletions examples/golang-push/rideshare/Dockerfile.load-generator
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" ]
13 changes: 13 additions & 0 deletions examples/golang-push/rideshare/README.md
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
```
7 changes: 7 additions & 0 deletions examples/golang-push/rideshare/bike/bike.go
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")
}
7 changes: 7 additions & 0 deletions examples/golang-push/rideshare/car/car.go
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")
}
37 changes: 37 additions & 0 deletions examples/golang-push/rideshare/docker-compose.yml
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
5 changes: 5 additions & 0 deletions examples/golang-push/rideshare/go.mod
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
2 changes: 2 additions & 0 deletions examples/golang-push/rideshare/go.sum
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=
26 changes: 26 additions & 0 deletions examples/golang-push/rideshare/load-generator.py
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))
53 changes: 53 additions & 0 deletions examples/golang-push/rideshare/main.go
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)
}
7 changes: 7 additions & 0 deletions examples/golang-push/rideshare/scooter/scooter.go
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")
}
55 changes: 55 additions & 0 deletions examples/golang-push/rideshare/utility/utility.go
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.

0 comments on commit 09e4e51

Please sign in to comment.