@@ -43,27 +43,39 @@ export interface IDebugInfo {
43
43
cpus : number ;
44
44
loadavg : number [ ] ;
45
45
} ;
46
- memoryStats : string ;
47
- memoryUsage : NodeJS . MemoryUsage ;
48
- cache ?: {
46
+ memory : {
47
+ stats : string ;
48
+ free : number ;
49
+ total : number ;
50
+ usage : NodeJS . MemoryUsage ;
51
+ } ;
52
+ resource : {
53
+ usage : NodeJS . ResourceUsage ;
54
+ } ;
55
+ lruCache ?: {
49
56
keys : number ;
50
57
sizeMB : number ;
51
58
ttlSec : number ;
52
59
} ;
53
- resourceUsage : NodeJS . ResourceUsage ;
54
60
sharp : ISharpInfo ;
55
61
}
56
62
57
- export default function debug ( cache ?: LRUCache < string , CacheObject > ) : IDebugInfo {
63
+ export default function debug ( lruCache ?: LRUCache < string , CacheObject > ) : IDebugInfo {
58
64
const ret : IDebugInfo = {
59
65
os : {
60
66
arch : os . arch ( ) ,
61
67
cpus : os . cpus ( ) . length ,
62
68
loadavg : os . loadavg ( ) ,
63
69
} ,
64
- memoryStats : `free: ${ fmtmb ( os . freemem ( ) ) } , total: ${ fmtmb ( os . totalmem ( ) ) } , usage ${ Math . round ( 100 * ( os . totalmem ( ) - os . freemem ( ) ) / os . totalmem ( ) ) / 100 } %` ,
65
- memoryUsage : process . memoryUsage ( ) ,
66
- resourceUsage : process . resourceUsage ( ) ,
70
+ memory : {
71
+ stats : `free: ${ formatBytes ( os . freemem ( ) ) } , total: ${ formatBytes ( os . totalmem ( ) ) } , usage ${ ( ( os . totalmem ( ) - os . freemem ( ) ) / os . totalmem ( ) * 100 ) . toFixed ( 2 ) } %` ,
72
+ free : os . freemem ( ) ,
73
+ total : os . totalmem ( ) ,
74
+ usage : process . memoryUsage ( ) ,
75
+ } ,
76
+ resource : {
77
+ usage : process . resourceUsage ( ) ,
78
+ } ,
67
79
sharp : {
68
80
cache : sharp . cache ( ) ,
69
81
simd : sharp . simd ( ) ,
@@ -72,17 +84,25 @@ export default function debug(cache?: LRUCache<string, CacheObject>): IDebugInfo
72
84
versions : sharp . versions ,
73
85
} ,
74
86
} ;
75
- if ( cache ) {
76
- ret . cache = {
77
- keys : cache . size ,
78
- sizeMB : Math . round ( cache . calculatedSize / 1048576 * 100 ) / 100 ,
79
- ttlSec : Math . round ( cache . ttl / 1000 ) ,
87
+ if ( lruCache ) {
88
+ ret . lruCache = {
89
+ keys : lruCache . size ,
90
+ sizeMB : Math . round ( b2mb ( lruCache . calculatedSize ) * 100 ) / 100 ,
91
+ ttlSec : Math . round ( lruCache . ttl / 1000 ) ,
80
92
} ;
81
93
}
82
94
return ret ;
83
95
}
84
96
85
- function fmtmb ( v : number ) {
86
- return ` ${ Math . round ( v / 1048576 * 100 ) / 100 } MB` ;
97
+ function b2mb ( v : number ) {
98
+ return v / 1048576 ;
87
99
}
88
100
101
+ function formatBytes ( bytes : number ) {
102
+ const units = [ 'B' , 'KB' , 'MB' , 'GB' , 'TB' ] ;
103
+ let i = 0 ;
104
+ for ( ; bytes >= 1024 && i < units . length - 1 ; i ++ ) {
105
+ bytes /= 1024 ;
106
+ }
107
+ return `${ bytes . toFixed ( 2 ) } ${ units [ i ] } ` ;
108
+ } ;
0 commit comments