TweetFollow Us on Twitter

AS vs Frontier
Volume Number:12
Issue Number:1
Column Tag:Internet Development

CGI’s: AppleScript or Frontier?

Comparing scripting environments for CGI development

By Mason Hale

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

In a previous article, “Scripting the Web with Frontier”, I introduced you to writing CGI scripts using UserLand Frontier. In that article, I argued that Frontier was an excellent alternative for webmasters who felt forced to choose between the poor performance of AppleScript and the steep learning curve of C because Frontier is easier to use than C yet generally performs faster than AppleScript, especially when used to create CGI applications. That argument became even more true when a PowerPC-native Frontier was released for public beta-testing in late October. Like dropping a bigger engine into a hot rod, script execution instantly becomes much faster - in some cases up to six time faster. With this boost in speed on PowerPC machines, Frontier is closing the gap on C - offering both ease-of-use and excellent performance - while further increasing its lead on AppleScript.

Despite the proven benefits of using Frontier to write CGI applications, many webmasters still are developing their CGIs in AppleScript. This inspired me to look a bit more closely at the differences between CGI applications written in Frontier and those in AppleScript, primarily focusing on performance issues. In this article I will share the results of some of my recreational performance testing, and explain some of the different situations that affect performance.

Since I am the author of the Frontier CGI Framework, a set of scripts that enhance CGI development in Frontier, you could understandably question my objectivity in doing such a comparison. On the other hand, since I have done a great deal of CGI development in both environments, I am also one of the few people qualified to do such a comparison. In either case it is not my intent to discount AppleScript as a scripting environment. I think it is a great product and an important technology. I just don’t believe it is well-suited to the specific task of CGI development.

Performance

Performance is crucial to CGI applications running on busy servers. The more processing time a request takes, the more likely a user is to give up and move on to another site.

I’ve often been asked if Frontier is faster than AppleScript. The truth is, when comparing built-in verbs in AppleScript and the non-native version of Frontier, the performance is surprising similar. I ran a series of informal tests to compare the performance of AppleScript, non-native Frontier and native Frontier when running equivalent scripts. The scripts are based on the sample scripts from Frontier’s object database. All tests were run on a Power Macintosh 7200/75 with 16 MB RAM. Execution time is measured in ticks (sixtieths of seconds).

The first test script performs simple integer arithmetic using built-in commands in both the AppleScript and UserTalk versions. The actual scripts are functionally identical.

Test 1: Integer Arithmetic (AppleScript)
set x to 0
repeat with i from 1 to 1000
 set x to x + (12 + 99 - 37 / 84)
end

Test 1: Integer Arithmetic (UserTalk)
x = 0
for i = 1 to 1000 
 x = x + (12 + 99 - 37 / 84)

AppleScript took 111 ticks to complete the first test, while the non-native Frontier took 103 ticks. The PowerPC-native Frontier ran the same script in just 17 ticks. This first test really shows how close the non-native Frontier and AppleScript were - and the tremendous difference the native version makes.

The second test demonstrates the repeated calling of a local subroutine.

Test 2: Subroutine Call (AppleScript)
set y to 10
on moof (x)
 return (x * 2)
end
repeat with i from 1 to 1000
 set y to moof (y)
end

Test 2: Subroutine Call (UserTalk)
y = 10
on moof (x) 
 return (x * 2)
for i = 1 to 1000 
 y = moof (y)

AppleScript blew the doors of the non-native Frontier in the second test coming in at 85 ticks to Frontier’s 272 ticks. However, the PowerPC-native Frontier handily won with a time of 47 ticks.

The third script compares the performance of commands from an external Scripting Addition to a built-in Frontier verb. I compared the speed of Frontier’s built-in clock.now verb to the equivalent current date Scripting Addition.

Test 3: Built-in verb vs. Scripting Addition (UserTalk)
for i = 1 to 100 
 y = clock.now ()


Test 3: Built-in verb vs. Scripting Addition (AppleScript)

repeat with i from 1 to 100
 set y to current date
end repeat

It took AppleScript 113 ticks to complete this test, while the non-native Frontier took 17 ticks and the native Frontier took 5 ticks. This illustrates a crucial point in determining the speed of a script. Built-in commands are faster than commands loaded from external code fragments. Because AppleScript has few built-in verbs and relies heavily on Scripting Additions to extend the language, the use of external commands like “current date” is quite common.

In the fourth example the script checks the existence of a file. Frontier uses a built-in verb “file.exists”, while AppleScript communicates with the scriptable Finder via AppleEvents.

Test 4: Built-in verb vs. Apple Event (AppleScript)
set x to 0
tell application "Finder"
 repeat with i from 1 to 10
 if exists alias "Macintosh HD:SimpleText" then
 set x to x + 1
 end if
 end repeat
end tell

Test 4: Built-in verb vs. Apple Event (UserTalk)
local (x = 0)
for i = 1 to 10 
 if file.exists ("Macintosh HD:SimpleText") 
 x++

Inter-application communication can really slow things down. Each cross-application Apple Event adds approximately 1/4 second to the processing time of the script. Frontier suffers the same slowdowns when sending Apple Events to other applications, but because more commands are available, external applications are relied on less often.

The results of the fourth test bear this out. While the native and non-native Frontier applications finished in 3 ticks and 7 ticks respectively, AppleScript took 140 ticks to perform the same task using the scriptable Finder.

My final test was “real world” example, based on the “test.cgi” script that is distributed with MacHTTP. This script uses no scripting additions and doesn’t perform any cross-application communication. So it is a pretty good example of a common CGI script using built-in verbs.

Test 5: Test CGI (AppleScript)

property crlf : (ASCII character 13) & (ASCII character 10)

--this builds the normal HTTP header for regular access
property http_10_header : "HTTP/1.0 200 OK" & crlf & ¬
 "Server: MacHTTP" & crlf & "MIME-Version: 1.0" & ¬
 crlf & "Content-type: text/html" & crlf & crlf

on cgiScript (path_args, http_search_args, username, ¬
 password, from_user, client_address, server_name, ¬
 server_port, script_name, content_type, referer, ¬
 user_agent, action, action_path, post_args, method, ¬
 client_ip, full_request)
 
 try --wrap the whole script in an error handler
 return http_10_header & "<title>Test CGI</title>" & ¬
 "<h2>Test CGI</h2><u>CGI arguments sent:</u>" & ¬
 "<br><b>path:</b> " & path_args & ¬
 "<br><b>search:</b> " & http_search_args & ¬
 "<br><b>post_args:</b> " & post_args & ¬
 "<br><b>method:</b> " & method & ¬
 "<br><b>address:</b> " & client_address & ¬
 "<br><b>user:</b> " & username & ¬
 "<br><b>password:</b> " & password & ¬
 "<br><b>from:</b> " & from_user & ¬
 "<br><b>server_name:</b> " & server_name & ¬
 "<br><b>server_port:</b> " & server_port & ¬
 "<br><b>script_name:</b> " & script_name & ¬
 "<br><b>referer:</b> " & referer & ¬
 "<br><b>user agent:</b> " & user_agent & ¬
 "<br><b>content_type:</b> " & content_type & crlf
 on error msg number num
 return http_10_header & "Error " & num & ", " & msg
 end try
end caller

repeat with i from 1 to 10
cgiScript ("aaa", "bbb", "ccc", "ddd", "eee", ¬
 "fff", "ggg", "hhh", "iii", "jjj", "kkk", ¬
 "lll", "mmm", "nnn", "ooo", "ppp", "qqq", "rrr")
end repeat


Test 5: Test CGI (UserTalk)

on cgiScript (pathArgs, httpSearchArgs, username, \
 password, fromUser, clientAddress, serverName, \
 serverPort, scriptName, contentType, referer, \
 userAgent, action, actionPath, postArgs, method, \
 clientIp, fullRequest) 
 
 try 
 return (webServer.httpHeader () + \
 "<title>Test CGI</title><h2>Test CGI</h2>" + \
 "<u>CGI arguments sent:</u>" + \
 "<br><b>path:</b> " + pathArgs + \
 "<br><b>search:</b> " + httpSearchArgs + \
 "<br><b>post_args:</b> " + postArgs + \
 "<br><b>method:</b> " + method + \
 "<br><b>address:</b> " + clientAddress + \
 "<br><b>user:</b> " + username + \
 "<br><b>password:</b> " + password + \
 "<br><b>from:</b> " + fromUser + \
 "<br><b>server_name:</b> " + serverName + \
 "<br><b>server_port:</b> " + serverPort + \
 "<br><b>script_name:</b> " + scriptName + \
 "<br><b>referer:</b> " + referer + \
 "<br><b>user agent:</b> " + userAgent + \
 "<br><b>content_type:</b> " + contentType + cr + lf)
 else 
 return (webServer.httpHeader () + "Error " + tryError)
 
local (i)
for i = 1 to 10 
 cgiScript ("aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", \
 "hhh", "iii", "jjj", "kkk", "lll", "mmm", "nnn", "ooo", \
 "ppp", "qqq", "rrr")

As expected, the AppleScript and non-native version of Frontier performed similarly. The AppleScript CGI test finished in 69 ticks, beating out Frontier at 74 ticks. Native Frontier won again with 14 ticks. A summary of the timing results for all tests is shown in Table 1.


AppleScript Frontier 68K Frontier PPC

Integer Arithmetic 111 103 17

Subroutine Call 85 272 45

Built-in vs. OSAX 113 17 5

Built-in vs. AE 140 7 3

Test CGI 69 74 14

Table 1. Comparison of execution times (all times in 1/60 second)

Multi-threading

Beyond straight script execution, another factor can significantly affect the speed at which a given script runs: multi-threading. In the exponentially-growing world of the internet, it is common for the average web server to receives thousands of requests a day. It is also quite likely that two clients will request the exact same file at the exact same time. If the requested file happens to be your CGI script, it will have to deal with two concurrent requests.

AppleScript is not multi-threaded, and handles multiple concurrent requests by placing them into a queue. Unfortunately, AppleScript processes events on a last-in first-out basis - so the latest event received is the first to be processed. To put it another way, on a very busy server, the first person to call the CGI may very well be the last person to receive the results. This can result in every single request timing out if new requests keep forcing older ones further back in the queue.

Frontier is fully multi-threaded. Every new request spawns a new thread automatically. This means that incoming event are processed immediately and do not prevent processing of earlier requests.

A more subtle, but still important performance consideration is the fact that all Frontier-based CGI’s are hosted by a single application. In the cooperative multi-tasking Mac OS, applications must cooperatively share processing time. Adding a new, separate application for each CGI creates more overhead to manage the sharing of processor time among the competing applications and eventually slows down all the applications. Consolidating all CGI scripts into Frontier’s object database eliminates this overhead.

Conclusion

Being PowerPC-native and multi-threaded clearly gives Frontier the performance advantage over AppleScript. However, even if AppleScript were multi-threaded and native, its reliance on Scripting Additions and external applications to perform its functions severely limit performance, and thus limit its usefulness as a development environment for CGI applications.

URL’s

Native Frontier Public Beta release:

http://www.hotwired.com/ userland/yabbadabba/nativefrontierpublicbe_390.html

Frontier CGI Scripting: http://www.webedge.com/frontier/

Aretha Website: http://www.hotwired.com/userland/aretha/

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »
Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »
Marvel Future Fight celebrates nine year...
Announced alongside an advertising image I can only assume was aimed squarely at myself with the prominent Deadpool and Odin featured on it, Netmarble has revealed their celebrations for the 9th anniversary of Marvel Future Fight. The Countdown... | Read more »
HoYoFair 2024 prepares to showcase over...
To say Genshin Impact took the world by storm when it was released would be an understatement. However, I think the most surprising part of the launch was just how much further it went than gaming. There have been concerts, art shows, massive... | Read more »

Price Scanner via MacPrices.net

Apple Watch Ultra 2 now available at Apple fo...
Apple has, for the first time, begun offering Certified Refurbished Apple Watch Ultra 2 models in their online store for $679, or $120 off MSRP. Each Watch includes Apple’s standard one-year warranty... Read more
AT&T has the iPhone 14 on sale for only $...
AT&T has the 128GB Apple iPhone 14 available for only $5.99 per month for new and existing customers when you activate unlimited service and use AT&T’s 36 month installment plan. The fine... Read more
Amazon is offering a $100 discount on every M...
Amazon is offering a $100 instant discount on each configuration of Apple’s new 13″ M3 MacBook Air, in Midnight, this weekend. These are the lowest prices currently available for new 13″ M3 MacBook... Read more
You can save $300-$480 on a 14-inch M3 Pro/Ma...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
24-inch M1 iMacs available at Apple starting...
Apple has clearance M1 iMacs available in their Certified Refurbished store starting at $1049 and ranging up to $300 off original MSRP. Each iMac is in like-new condition and comes with Apple’s... Read more
Walmart continues to offer $699 13-inch M1 Ma...
Walmart continues to offer new Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBook for sale by... Read more
B&H has 13-inch M2 MacBook Airs with 16GB...
B&H Photo has 13″ MacBook Airs with M2 CPUs, 16GB of memory, and 256GB of storage in stock and on sale for $1099, $100 off Apple’s MSRP for this configuration. Free 1-2 day delivery is available... Read more
14-inch M3 MacBook Pro with 16GB of RAM avail...
Apple has the 14″ M3 MacBook Pro with 16GB of RAM and 1TB of storage, Certified Refurbished, available for $300 off MSRP. Each MacBook Pro features a new outer case, shipping is free, and an Apple 1-... Read more
Apple M2 Mac minis on sale for up to $150 off...
Amazon has Apple’s M2-powered Mac minis in stock and on sale for $100-$150 off MSRP, each including free delivery: – Mac mini M2/256GB SSD: $499, save $100 – Mac mini M2/512GB SSD: $699, save $100 –... Read more
Amazon is offering a $200 discount on 14-inch...
Amazon has 14-inch M3 MacBook Pros in stock and on sale for $200 off MSRP. Shipping is free. Note that Amazon’s stock tends to come and go: – 14″ M3 MacBook Pro (8GB RAM/512GB SSD): $1399.99, $200... Read more

Jobs Board

Nurse Anesthetist - *Apple* Hill Surgery Ce...
Nurse Anesthetist - Apple Hill Surgery Center Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
Housekeeper, *Apple* Valley Village - Cassi...
Apple Valley Village Health Care Center, a senior care campus, is hiring a Part-Time Housekeeper to join our team! We will train you for this position! In this role, Read more
Sublease Associate Optometrist- *Apple* Val...
Sublease Associate Optometrist- Apple Valley, CA- Target Optical Date: Apr 20, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92307 **Requisition Read more
*Apple* Systems Administrator - JAMF - Syste...
Title: Apple Systems Administrator - JAMF ALTA is supporting a direct hire opportunity. This position is 100% Onsite for initial 3-6 months and then remote 1-2 Read more
Relationship Banker - *Apple* Valley Financ...
Relationship Banker - Apple Valley Financial Center APPLE VALLEY, Minnesota **Job Description:** At Bank of America, we are guided by a common purpose to help Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.