https://github.com/jarred-sumner/cpu
https://github.com/jarred-sumner/cpu
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/jarred-sumner/cpu
- Owner: Jarred-Sumner
- License: mit
- Created: 2025-03-08T07:56:21.000Z (3 months ago)
- Default Branch: master
- Last Pushed: 2025-03-08T07:57:58.000Z (3 months ago)
- Last Synced: 2025-03-08T08:29:54.704Z (3 months ago)
- Language: TypeScript
- Size: 0 Bytes
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
A simple, readable alternative to the Unix `time` command.
```sh
$ cpu bun --print '123'
123
┌─────────┬──────────────┬─────────────┬─────────────┐
│ 0.01s │ 0.01s user │ 0.00s sys │ 20 MB mem │
└─────────┴──────────────┴─────────────┴─────────────┘
```This tool runs commands and shows their execution time and memory usage in an easy-to-read format. It works just like the standard `time` command, but with a cleaner display.
## Installation
```bash
# Install globally
bun install -g @jarred/cpu# Or use directly
bunx @jarred/cpu
```## Usage
```bash
# Basic usage
cpu sleep 1
┌─────────┬──────────────┬─────────────┬────────────┐
│ 1.00s │ 0.00s user │ 0.00s sys │ 2 MB mem │
└─────────┴──────────────┴─────────────┴────────────┘
```### Verbose output
```bash
# With verbose output
cpu -v node -e "console.log('Hello')"
┌─────────┬──────────────┬─────────────┬─────────────┐
│ 0.03s │ 0.02s user │ 0.01s sys │ 30 MB mem │Detailed metrics:
Time:
Total: 0.035s
User CPU: 0.02s (57.5% of total)
System CPU: 0.01s (28.8% of total)
Memory:
Peak: 30 MB
System: 64 GB total (0.05% used)
Process:
Context switches: 62 (voluntary: 1, involuntary: 61)
IO operations: in: 0, out: 0
Exit code: 0
└─────────┴──────────────┴─────────────┴─────────────┘
```More examples:
```bash
# Time a script
cpu ./my-script.sh# Compare Bun & Node.js
cpu bun -e "console.log('Hello world')"
cpu node -e "console.log('Hello world')"# Time a build process
cpu bun run build
cpu npm run build
```## Comparison with time command
**Standard time command output:**
```sh
real 0m1.007s
user 0m0.003s
sys 0m0.003s
```**cpu command output:**
```sh
┌─────────┬──────────────┬─────────────┬─────────────┐
│ 0.01s │ 0.01s user │ 0.00s sys │ 20 MB mem │
└─────────┴──────────────┴─────────────┴─────────────┘
```Differences:
- Includes memory usage
- Uses a simple table format
- Has a verbose mode for additional details
- Adds basic color coding when supported## How it works
This tool uses Bun's `Bun.spawn`'s `resourceUsage()` API, which accesses the same underlying `getrusage` system call that the standard Unix `time` command uses. This means you get the same accurate metrics, just displayed in a more readable format. The implementation is efficient with minimal overhead to the process being measured.
### Options
- `-v, --verbose`: Show additional metrics
- `-h, --help`: Show help message
- `--version`: Show version information## Output explained
### Standard output shows:
- Total execution time
- User CPU time
- System CPU time
- Peak memory usage### Verbose output (-v) adds:
- CPU percentages
- Memory usage percentages
- Context switch counts
- I/O operation counts
- Process exit code## Development
```bash
# Clone the repository
git clone https://github.com/jarred/cpu.git
cd cpu# Install dependencies
bun install# Run locally
bun run index.ts
```## License
MIT © Jarred Sumner