2017-10-22 08:07:36 +00:00
|
|
|
// Copyright 2017 Google Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"cloud.google.com/go/profiler"
|
|
|
|
"compress/gzip"
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
2017-12-03 20:01:25 +00:00
|
|
|
"sync"
|
2017-10-22 08:07:36 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-12-03 20:01:25 +00:00
|
|
|
var (
|
|
|
|
service = flag.String("service", "", "service name")
|
|
|
|
mutexProfiling = flag.Bool("mutex_profiling", false, "enable mutex profiling")
|
2018-03-30 09:41:12 +00:00
|
|
|
duration = flag.Int("duration", 600, "duration of the benchmark in seconds")
|
|
|
|
apiAddr = flag.String("api_address", "", "API address of the profiler (e.g. 'cloudprofiler.googleapis.com:443')")
|
2017-12-03 20:01:25 +00:00
|
|
|
)
|
2017-10-22 08:07:36 +00:00
|
|
|
|
|
|
|
// busywork continuously generates 1MiB of random data and compresses it
|
|
|
|
// throwing away the result.
|
2017-12-03 20:01:25 +00:00
|
|
|
func busywork(mu *sync.Mutex) {
|
2018-03-30 09:41:12 +00:00
|
|
|
ticker := time.NewTicker(time.Duration(*duration) * time.Second)
|
2017-10-22 08:07:36 +00:00
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
return
|
|
|
|
default:
|
2017-12-03 20:01:25 +00:00
|
|
|
mu.Lock()
|
2017-10-22 08:07:36 +00:00
|
|
|
busyworkOnce()
|
2017-12-03 20:01:25 +00:00
|
|
|
mu.Unlock()
|
2017-10-22 08:07:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func busyworkOnce() {
|
|
|
|
data := make([]byte, 1024*1024)
|
|
|
|
rand.Read(data)
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
gz := gzip.NewWriter(&b)
|
|
|
|
if _, err := gz.Write(data); err != nil {
|
|
|
|
log.Printf("Failed to write to gzip stream: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := gz.Flush(); err != nil {
|
|
|
|
log.Printf("Failed to flush to gzip stream: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := gz.Close(); err != nil {
|
|
|
|
log.Printf("Failed to close gzip stream: %v", err)
|
|
|
|
}
|
|
|
|
// Throw away the result.
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *service == "" {
|
|
|
|
log.Print("Service name must be configured using --service flag.")
|
2017-12-03 20:01:25 +00:00
|
|
|
} else if err := profiler.Start(
|
|
|
|
profiler.Config{
|
|
|
|
Service: *service,
|
|
|
|
MutexProfiling: *mutexProfiling,
|
|
|
|
DebugLogging: true,
|
2018-03-30 09:41:12 +00:00
|
|
|
APIAddr: *apiAddr,
|
2017-12-03 20:01:25 +00:00
|
|
|
}); err != nil {
|
2017-10-22 08:07:36 +00:00
|
|
|
log.Printf("Failed to start the profiler: %v", err)
|
|
|
|
} else {
|
2017-12-03 20:01:25 +00:00
|
|
|
mu := new(sync.Mutex)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(5)
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
busywork(mu)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2017-10-22 08:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("busybench finished profiling.")
|
2017-12-03 20:01:25 +00:00
|
|
|
// Do not exit, since the pod in the GKE test is set to always restart.
|
2017-10-22 08:07:36 +00:00
|
|
|
select {}
|
|
|
|
}
|