Skip to content

Commit

Permalink
style: supports percentage to jvm-class-adj
Browse files Browse the repository at this point in the history
  • Loading branch information
shihyuho committed Jan 19, 2024
1 parent 5969608 commit 9d2d48e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Non-Heap = Direct Memory + Metaspace + Reserved Code Cache + (Thread Stack * Thr
| 運行時將加載的 class 數量 | `--loaded-class-count` | `$BPL_JVM_LOADED_CLASS_COUNT` | 若沒提供, 則以 App 目錄, JVM class 數量, JVM class 數量調整, 於啟動時動態的計算出建議值 |
| App 目錄 | `--app-path` | `$BPI_APPLICATION_PATH` | `/app` |
| JVM class 數量 | `--jvm-class-count` | `$BPI_JVM_CLASS_COUNT` | 若沒提供, 則動態計算 `$JAVA_HOME` 下的 class 數量 |
| JVM class 數量調整 | `--jvm-class-adj` | `$BPL_JVM_CLASS_ADJUSTMENT` | `0`, 可以是絕對數字或百分比 (ex: `10%`) |
| JVM class 數量調整 | `--jvm-class-adj` | `$BPL_JVM_CLASS_ADJUSTMENT` | 無, 可接受數字 ( `1000`) 或百分比 (`150%`) |
| JVM CA 目錄 | `--jvm-cacerts` | `$BPI_JVM_CACERTS` | 若沒提供, 則試著使用 `$JAVA_HOME/lib/security/cacerts` |
| Java 啟動參數 | `--jvm-options` | `$JAVA_OPTS` | |
| 是否啟用 [JDWP](https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/introclientissues005.html) | `--enable-jdwp` | `$BPL_DEBUG_ENABLED` | `true` |
Expand Down
38 changes: 16 additions & 22 deletions calc/jvm_class_adj.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,43 @@
package calc

import (
"fmt"
"os"
"strconv"
)

const (
DefaultJVMClassAdj = JVMClassAdj(0)
DefaultJVMClassAdj = JVMClassAdj("")
FlagJVMClassAdj = "jvm-class-adj"
EnvJVMClassAdj = "BPL_JVM_CLASS_ADJUSTMENT"
UsageJVMClassAdj = "the adjustment for JVM classes number"
UsageJVMClassAdj = "the adjustment for the number or percentage of JVM classes"
)

type JVMClassAdj int
type JVMClassAdj string

func NewJVMClassAdj() *JVMClassAdj {
lcc := DefaultJVMClassAdj
j := DefaultJVMClassAdj
if val, ok := os.LookupEnv(EnvJVMClassAdj); ok {
f, _ := strconv.Atoi(val)
lcc = JVMClassAdj(f)
j = JVMClassAdj(val)
}
return &lcc
return &j
}

func (jca *JVMClassAdj) Set(s string) error {
f, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}

*jca = JVMClassAdj(f)
func (j *JVMClassAdj) Set(s string) error {
*j = JVMClassAdj(s)
return nil
}

func (jca *JVMClassAdj) Type() string {
return "int"
func (j *JVMClassAdj) String() string {
return fmt.Sprintf("%s", *j)
}

func (jca *JVMClassAdj) String() string {
return strconv.FormatInt(int64(*jca), 10)
func (j *JVMClassAdj) Type() string {
return "string"
}

func (jca *JVMClassAdj) Contribute() error {
if *jca > 0 {
if err := os.Setenv(EnvJVMClassAdj, jca.String()); err != nil {
func (j *JVMClassAdj) Contribute() error {
if s := j.String(); s != "" {
if err := os.Setenv(EnvJVMClassAdj, s); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions calc/jvm_class_adj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestNewJVMClassAdj_WithEnvVar(t *testing.T) {
}

func TestContribute_PositiveValue(t *testing.T) {
jca := JVMClassAdj(5)
jca := JVMClassAdj("5")
err := jca.Contribute()
defer os.Unsetenv(EnvJVMClassAdj)
if err != nil {
Expand All @@ -39,7 +39,7 @@ func TestContribute_PositiveValue(t *testing.T) {
}

func TestContribute_NonPositiveValue(t *testing.T) {
jca := JVMClassAdj(0)
jca := JVMClassAdj("")
err := jca.Contribute()
defer os.Unsetenv(EnvJVMClassAdj)
if err != nil {
Expand Down

0 comments on commit 9d2d48e

Please sign in to comment.