comparison_test.go 939 B

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