VLang for Automation Scripting
By: Jeremy Cheng
Tags: Automation, Vlang, Bash, Go, Python

VLang for Automation Scripting

If you have been in the tech industry long enough, regardless of your role, chances are, you have used Bash or Python more than once to automate something that you would otherwise need to manually do repetitively.

I am obviously no exception and have written automation scripts since the early 2000’s. I have done projects from writing scripts to update thousands of servers for myself, to streamlining development/deployment processes for engineers, all the way to building application/firmware installers for end users. From my experience, there are three main factors that determine what scripting language I chose for each project:

How simple is the syntax? How powerful is the language in handling complex automation issues? How fast does it execute?

In the past, if the automation project was straight forward with simple logic, I would always use bash scripts because it is relatively fast and the syntax is super straight forward. Automating a shell command is as simple as putting that command in a line of its own. For example, if I wanted the script to execute cat /etc/os-release all I had to do was something like this:

#!/bin/bash cat /etc/os-release

This saves a lot of time on development compared to a more complex language like Python. To do the same thing in Python, I would write:

#!/usr/bin/python import os res = os.system("cat /etc/os-release")

print(res)

As you can see from the code snippets, it’s pretty clear why Bash is just simpler to automate non-trivial tasks. However, there are some automation projects that are a bit more complex and beyond the power of bash scripts. I would in that case forego performance and choose Python to automate especially when it comes to things that require more computation or a GUI (Gooey) to interact with users.

In the recent years, GoLang (Go) has become more and more popular and I started using it to develop some of my software projects. So I also began to use Go for automation since as a compiled language with relatively simple syntax, execution time (performance) is way faster than Python. Also, using a language to automate things as the same language used for the software project provides some sense of consistency and in general, just easier on my brain. It’s like speaking Cantonese (a Chinese dialect) no matter who you are talking to instead of having to speak Cantonese with the sales team and Mandarin with the ops team and then end up finding yourself using weird terms and getting lost in translation deriving from having to rapidly jump from one dialect to another.

However, earlier this year, I discovered a very intriguing new cross-platform compiled language called VLang. This is not yet another language to be a one-for-one better version of something already there like Julia is to Python but rather, a language that was like a love child of all the most popular modern languages out there aimed at being small, fast, safe, and simple with a low learning curve all at the same time. It was well thought out and the concept was so compelling that the benefits amount to a long and distinguish list which I encourage for anyone interested to check the VLang site themselves to see what those benefits are. It’s really quite impressive. For now, here’s a direct sneak peak of some of the benefits/differentiating factors:

So how much faster is V while maintaining simple syntax? Let’s do a test by writing a script that just executes ls -lah and outputting the results with error handling. Here’s what the code looks like:

BASH

#!/bin/bash set -e

ls -lah

Python

#!/usr/bin/python3.8 import os try:

res = os.system("ls -lah")

print(res)

except:

print("Something went wrong...")

GoLang

package main import "fmt" import "bytes"

import "os/exec" func main() {

cmd := exec.Command("ls", "-lah")

var out, stderr bytes.Buffer

cmd.Stdout = &out

cmd.Stderr = &stderr

err := cmd.Run()

if err != nil {

fmt.Println(err)

}

fmt.Println(out.String()) }

VLang

import os fn main() {

results := os.exec('ls -lah') or { panic(err) }

println('$results.output')

}

As we can tell from the above code snippets, Bash is as expected the simplest. VLang comes in second with Python coming pretty close to it. In fact, if you didn’t want to do error handling on this command, Python could be simpler than VLang especially since there’s no option that I know of to remove error handling from VLang. However, if we removed error handling from the code, then the safe factor would be compromised which is not something that we want under any circumstances anyway. Lastly, GoLang looses pretty badly in this area to all.

Now let’s take a look at execution time (performance):

BASH

time ./ls-script.sh

... real 0m0.010s

user 0m0.005s

sys 0m0.004s

Python

time ./ls-script.py

... real 0m0.033s

user 0m0.027s

sys 0m0.006s

GoLang

time ./go-ls-script

... real 0m0.008s

user 0m0.004s

sys 0m0.005s

VLang

time ./v-ls-script

... real 0m0.008s

user 0m0.001s

sys 0m0.007s

I’ll first start by saying that a delta of 0.002s should not be used as a strong enough data point to confirm performance difference as Bash and VLang gets different results almost every time when run consecutively within a short period of time and most of the time, they are way faster than Go (up to 0.004s) while other times slower than Go but only by up to 0.002s. On the other hand, Go consistently gets pretty much the same time of completion on every run.

With that out of the way, both Go and VLang are comparable or faster than Bash in performance for single runs with Python being significantly slower. However, VLang’s completion time is most comparable to Bash over multiple consecutive runs if not faster and it wins the race against Bash with the record time of 0.004s (real). Perhaps what’s more awesome when Vlang is put head-to-head against Bash is that VLang is powerful enough for more complex computation and has an awesome native cross-platform GUI library just like Python and Go but increases the least syntax complexity and does not suffer any performance degradation.

In terms of storage consumption, compiled languages are expected to take up more storage with their binaries so let’s take Bash and Python (both 4K each in size) out of the equation for a moment and compare Go and VLang.

GoLang Compiled Binary: 2.3M

VLang Compiled Binary: 232K

Huge difference right?

Let’s take a look at the above results side-by-side:

* 1 as the simplest and 4 as the least simple

I think the numbers speak for themselves. Let’s summarize VLang against my earlier mentioned 3 main factors that help me determine what language to use for my projects:

Syntax is second to bash in simplicity while maintaining the “safe” factor It’s powerful enough to handle complex computations and has a nicely developed GUI library It’s either faster or does not compromise performance when executing simple system commands even when compared to Bash

This is why I feel VLang is a great drop-in replacement for Python and Go in terms of more complex automation and in many cases, could be replacing Bash for simple automation too. Sure, you may argue that VLang is quite new and doesn’t have as many libraries or as much community support(yet) but with how active the project is and the fast growing list of contributors/adopters, it’s very possible that given time, it will completely surpass Python and Go within the automation space.

Perhaps what’s missing in these tests now are computation speed comparisons which I will leave for another post at another time.

If I don’t write another post until January 1st, Happy New Year everyone! May 2021 be a much better year for the world than 2020!