comparison_test.go 974 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package shell
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func TestShellPerformanceComparison(t *testing.T) {
  9. shell := NewShell(&Options{WorkingDir: t.TempDir()})
  10. // Test quick command
  11. start := time.Now()
  12. stdout, stderr, err := shell.Exec(t.Context(), "echo 'hello'")
  13. exitCode := ExitCode(err)
  14. duration := time.Since(start)
  15. require.NoError(t, err)
  16. assert.Equal(t, 0, exitCode)
  17. assert.Contains(t, stdout, "hello")
  18. assert.Empty(t, stderr)
  19. t.Logf("Quick command took: %v", duration)
  20. }
  21. // Benchmark CPU usage during polling
  22. func BenchmarkShellPolling(b *testing.B) {
  23. shell := NewShell(&Options{WorkingDir: b.TempDir()})
  24. b.ReportAllocs()
  25. for b.Loop() {
  26. // Use a short sleep to measure polling overhead
  27. _, _, err := shell.Exec(b.Context(), "sleep 0.02")
  28. exitCode := ExitCode(err)
  29. if err != nil || exitCode != 0 {
  30. b.Fatalf("Command failed: %v, exit code: %d", err, exitCode)
  31. }
  32. }
  33. }