2017-09-01 14:09:11 +00:00
|
|
|
package profiling
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestProfilingCPU(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "profiling")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = StartCPUProfile(dir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Block for a bit to collect some metrics.
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
err = StopCPUProfile()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Verify that the file has some content.
|
|
|
|
file, err := os.Open(filepath.Join(dir, CPUFilename))
|
|
|
|
require.NoError(t, err)
|
2018-02-12 11:16:06 +00:00
|
|
|
defer func() {
|
|
|
|
err := file.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
2017-09-01 14:09:11 +00:00
|
|
|
|
|
|
|
t.Logf("CPU profile saved in %s for %s", filepath.Join(dir, CPUFilename), os.Args[0])
|
|
|
|
|
|
|
|
info, err := file.Stat()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, info.Size() > 0, "a file with CPU profile is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProfilingMem(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "profiling")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = WriteHeapFile(dir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Verify that the file has some content.
|
|
|
|
file, err := os.Open(filepath.Join(dir, MemFilename))
|
|
|
|
require.NoError(t, err)
|
2018-02-12 11:16:06 +00:00
|
|
|
defer func() {
|
|
|
|
err := file.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
2017-09-01 14:09:11 +00:00
|
|
|
|
|
|
|
t.Logf("Memory profile saved in %s for %s", filepath.Join(dir, MemFilename), os.Args[0])
|
|
|
|
|
|
|
|
info, err := file.Stat()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, info.Size() > 0, "a file with memory profile is empty")
|
|
|
|
}
|