Added beter metrics for compression mechanism

This commit is contained in:
partisan 2024-12-21 22:21:04 +01:00
parent bc8a62666b
commit 3545a5f73b
2 changed files with 86 additions and 59 deletions

19
main.go
View file

@ -260,18 +260,14 @@ func PackageAndUploadProcess() {
log.Fatalf("Error: no valid build or upload path provided.") log.Fatalf("Error: no valid build or upload path provided.")
} }
// // This is stupid, it wait for the path to exist (up to a maximum wait time) // Calculate and display uncompressed size
// err := waitForPath(pathToUse, 60, 5) // Max 60 seconds, checking every 5 seconds
// if err != nil {
// log.Fatalf("Error: Build path or upload path not found: %v", err)
// }
uncompressedSize, err := spitfire.GetDirectorySize(pathToUse) uncompressedSize, err := spitfire.GetDirectorySize(pathToUse)
if err != nil { if err != nil {
log.Fatalf("Failed to calculate uncompressed size: %v", err) log.Fatalf("Failed to calculate uncompressed size: %v", err)
} }
fmt.Printf("Uncompressed directory size: %d bytes\n", uncompressedSize) fmt.Printf("Uncompressed directory size: %s\n", spitfire.BytesToHumanReadable(uncompressedSize))
// Compress the build directory
outputCompressedFile := filepath.Join(".", fmt.Sprintf("%s-%s-%s-%s.tar.gz", component, arch, release, platform)) outputCompressedFile := filepath.Join(".", fmt.Sprintf("%s-%s-%s-%s.tar.gz", component, arch, release, platform))
if compress { if compress {
err := spitfire.CompressDirectory(pathToUse, outputCompressedFile) err := spitfire.CompressDirectory(pathToUse, outputCompressedFile)
@ -281,12 +277,19 @@ func PackageAndUploadProcess() {
fmt.Printf("Build directory compressed to: %s\n", outputCompressedFile) fmt.Printf("Build directory compressed to: %s\n", outputCompressedFile)
} }
// Calculate and display compressed size
compressedSize, err := spitfire.GetFileSize(outputCompressedFile) compressedSize, err := spitfire.GetFileSize(outputCompressedFile)
if err != nil { if err != nil {
log.Fatalf("Failed to get compressed file size: %v", err) log.Fatalf("Failed to get compressed file size: %v", err)
} }
fmt.Printf("Compressed file size: %d bytes\n", compressedSize) fmt.Printf("Compressed file size: %s\n", spitfire.BytesToHumanReadable(compressedSize))
// Calculate and display compression efficiency
compressionRatio, efficiency := spitfire.CalculateCompressionEfficiency(uncompressedSize, compressedSize)
fmt.Printf("Compression ratio: %.2f:1\n", compressionRatio)
fmt.Printf("Compression efficiency: %.2f%%\n", efficiency)
// Handle uploading
if upload { if upload {
config, err := spitfire.LoadConfig() config, err := spitfire.LoadConfig()
if err != nil { if err != nil {

View file

@ -2,8 +2,9 @@ package spitfire
import ( import (
"archive/tar" "archive/tar"
"crypto/rand"
"compress/gzip" "compress/gzip"
"crypto/rand"
"encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
@ -11,7 +12,6 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"encoding/hex"
) )
// Config struct to hold SourceForge configurations // Config struct to hold SourceForge configurations
@ -244,3 +244,27 @@ func GetFileSize(filePath string) (int64, error) {
} }
return fileInfo.Size(), nil return fileInfo.Size(), nil
} }
// BytesToHumanReadable converts bytes to a human-readable format (e.g., MiB, GiB)
func BytesToHumanReadable(bytes int64) string {
const unit = 1024
if bytes < unit {
return fmt.Sprintf("%d B", bytes)
}
div, exp := int64(unit), 0
for n := bytes / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.2f %ciB", float64(bytes)/float64(div), "KMGTPE"[exp])
}
// CalculateCompressionEfficiency calculates the compression ratio and efficiency percentage
func CalculateCompressionEfficiency(uncompressed, compressed int64) (float64, float64) {
if uncompressed == 0 {
return 0, 0
}
compressionRatio := float64(uncompressed) / float64(compressed)
efficiency := 100 * (1 - float64(compressed)/float64(uncompressed))
return compressionRatio, efficiency
}