File size: 939 Bytes
3b67767
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package shell

import (
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

func TestShellPerformanceComparison(t *testing.T) {
	shell := NewShell(&Options{WorkingDir: t.TempDir()})

	// Test quick command
	start := time.Now()
	stdout, stderr, err := shell.Exec(t.Context(), "echo 'hello'")
	exitCode := ExitCode(err)
	duration := time.Since(start)

	require.NoError(t, err)
	require.Equal(t, 0, exitCode)
	require.Contains(t, stdout, "hello")
	require.Empty(t, stderr)

	t.Logf("Quick command took: %v", duration)
}

// Benchmark CPU usage during polling
func BenchmarkShellPolling(b *testing.B) {
	shell := NewShell(&Options{WorkingDir: b.TempDir()})

	b.ReportAllocs()

	for b.Loop() {
		// Use a short sleep to measure polling overhead
		_, _, err := shell.Exec(b.Context(), "sleep 0.02")
		exitCode := ExitCode(err)
		if err != nil || exitCode != 0 {
			b.Fatalf("Command failed: %v, exit code: %d", err, exitCode)
		}
	}
}